What is the best way to have my meteor
app call a python
script that resides on the same machine as the meteor server-side code? All I want to do is have meteor pass a string to a function in python and have python return a string to meteor.
I was thinking that I could have python monitor mongodb and extract values and write them back to mongodb once computed, but it seems much cleaner to have meteor call the function in python directly.
I am new to DDP and was not able to get very far with python-meteor (https://github.com/hharnisc/python-meteor).
Is ZeroRPC (http://zerorpc.dotcloud.com/) a good way to do it?
Thanks.
Python can call JavaScript and JavaScript can call Python. DOM events can use Python functions as callbacks. This article covers Python calling JavaScript functions and how to pass and receive data using Proxy. In another article, I cover calling Python from JavaScript and how to use Python in events.
We can use PHP or Hypertext Preprocessor to run Python scripts in HTML.
Great question.
I have looked at using DDP and ZeroRPC and even having Python write directly to Mongo.
For me, the easiest way to have Meteor and Python talk was to set up the python script as a flask app and then add an API to the flask app and have Meteor talk to Python through the API.
To get this setup working I used:
To test it you can build something basic like this (python script converts text to upper case):
from flask import Flask
from flask.ext import restful
app = Flask(__name__)
api = restful.Api(app)
class ParseText(restful.Resource):
def get(self, text):
output = text.upper()
return output
api.add_resource(ParseText, '/<string:text>')
if __name__ == '__main__':
app.run(debug=True) # debug=True is for testing to see if calls are working.
Then in Meteor use HTTP.get
to test calling the API.
If you are running everything locally then the call from Meteor would probably look something like: Meteor.http.get("http://127.0.0.1:5000/test");
I have experience in the past in implementing somehting similar by using RestFul approach.
By triggering observeChanges from Meteor, sending a http request to Python restful api endpoints (in Flask) from server, then Flask handling the requests in calling the relevant Python scripts/functions, with the return response, Meteor then handle the callback accordingly.
There are of course many other approaches you can consider, like using DDP, child_process etc. I have also considered using python-meteor before however after taking into accounts that RestFul approach is more portable and scalable (both in the same machine, or even in different machines... you can expand your servers to handle more requests etc. you get the idea).
Everyone's use case is different, and I found RestFul appoach is the best fit for my use case. I hope you find my answer useful and expand your choices of consideration and pick one which is best for your case. Good luck.
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