For example, I can use Python scripts in PHP like there:
exec("python script.py params",$result);
where "script.py" - script name and variable $result
save output data.
How I can make it with Ruby? I mean, call Python scripts from Ruby.
PyCall: Calling Python functions from the Ruby language. This library provides the features to directly call and partially interoperate with Python from the Ruby language. You can import arbitrary Python modules into Ruby modules, call Python functions with automatic type conversion from Ruby to Python.
Using Ruby with Python - a powerful combination If you want to build a web app that looks great, delights users, and handles Big Data or uses Machine Learning, joining the forces of Python and Ruby is a very good idea.
To do so, you would need to make your script into a proper Python module, start the bridge when the Rails app starts, then use RubyPython. import to get a reference to your module into your Ruby code. The examples on the gem's GitHub page are quite simple, and should be sufficient for your purpose.
You can shell-out to any shell binary and capture the response with backticks:
result = `python script.py params`
One way would be exec.
result = exec("python script.py params")
Another way to do the same thing would be,
system 'python script.py', params1, params2
I used the following python call from a ruby module.
In my case using exec()
didn't work because it interrupts the current process and caused the simulation I was running to fail.
# run python script
`python #{py_path_py} #{html_path_py} #{write_path_py}`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With