5

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.

2 Answers 2

8

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");

Sign up to request clarification or add additional context in comments.

1 Comment

I tried to use Meteor and ZeroRPC and just couldn't get the latter installed using Meteor 1.3 and the command meteor npm install --save zerorpc. Meteor 1.3 uses Node 0.10.43 which has some issues installing libmsg, a dependency of ZeroRPC. This solution looks a lot simpler as it uses a native Meteor package. Thanks!
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.