pythonruby-on-rails-3.2ruby-1.9.2

Run python from a click event in ruby


I would like to be able to click an "Alert" button on my rails application that then calls python to run a specific alert script.

My specific question is how do I get the click event to call the python script?


Solution

  • Check this SO post: Call python script from ruby

    ruby provides a variety of methods, exec, system that all take the path to the ,py file along with arguments. Something like

    result = exec("python /path/to/script/script.py params")
    

    Or

    system 'python /path/to/script/script.py', params1, params2
    

    Or even backquotes

    `python /path/to/script/script.py foo bar`
    

    Now, the question is where to put this? I am assuming you have a controller that handles clicks, and it is this controller where you put this code. Check this link out How to integrate a standalone Python script into a Rails application?

    Now details depend a lot on your python script. By 'alert script', do you mean a Javascript alert() dialog, a flash msg or anything else? It's always a good idea to refactor your python code to return a string result and the use that in ruby-on-rails. Even better, as far as possible, write the alert-creating-script in ruby itself! It might not be possible in a 3rd-party library setting, but for a simple stand-alone script, it's worth the effort!