Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Python scripts in Meteor

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.

like image 404
ldh Avatar asked Jan 03 '15 21:01

ldh


People also ask

Can we call Python script from JavaScript?

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.

Can you call a Python script from HTML?

We can use PHP or Hypertext Preprocessor to run Python scripts in HTML.


2 Answers

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:

  • Flask API (https://flask-restful.readthedocs.org/en/0.3.1/quickstart.html#a-minimal-api)
  • The Meteor HTTP package (http://docs.meteor.com/#/full/http_call)

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

like image 81
e h Avatar answered Nov 07 '22 06:11

e h


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.

like image 37
Anzel Avatar answered Nov 07 '22 06:11

Anzel