Python web service with React/Node Application - python

I have the bulk of my web application in React (front-end) and Node (server), and am trying to use Python for certain computations. My intent is to send data from my Node application to a Python web service in JSON format, do the calculations in my Python web service, and send the data back to my Node application.
Flask looks like a good option, but I do not intend to have any front-end usage for my Python web service. Would appreciate any thoughts on how to do this.

In terms of thoughts:
1) You can build a REST interface to your python code using Flask. Make REST calls from your nodejs.
2) You have to decide if your client will wait synchronously for the result. If it takes a relatively long time you can use a web hook as a callback for the result.

Related

How to communicate data from ReactJS and Python?

I'm building an app in which I need to communicate data from and to ReactJS and Python. It needs to go both ways - but I'm more concerned about the React part right now. At first I considered JSON, but couldn't find any resources/ libraries to update JSON files, and people said I should stay away from that. Other than literally creating a text file with data in it, what are my options? The application has to do with getting stock data from a python API, doing calculations on it, and sending the data to ReactJS to render on a webpage. I also need ReactJS to send account data back to Python where we do our MySQL. Any suggestions?
Since the ReactJS app is a front-end application, your only real choice to ensure security is exposing an API on the python app which the ReactJS app talks to (via Websockets for example since you're mentioning a bidirectional communication). Maybe take a look at something like Flask with the flask-socketio package.

How to Pass Variables data from python application to apache webserver

I'm trying to build a webserver using Apache and I want to establish two-way of communication between my python application and webserver where I can listen for requests from apache and return data back, in the same time I want the client to interact only with the webserver.
In my python program I'm using python python-twisted and pymodbus (MODBUS-TCPIP) to get some data from a PLC.
I have already configured a websocket in python using
from autobahn.twisted.websocket import WebSocketServerProtocol, \
WebSocketServerFactory
I'm thinking to configure a intermediate web which will be initiated upon http request of the main website and open websocket connection with python and start receiving data, where the clients (browsers) can still request data using jquery.
I don't know if this is the right way to go. Do you guys have better way to pass data variable between python and apache back and forth?
If I understood correctly, you want to make PLC data accessible on a web page.
The right way to develop a web application in Python is to use a web framework like Django, or (if you want something lighter) bottle
With this, you can develop a web page where you connect to your PLC over Modbus, query some values, disconnect and return a response with data as json.
But I think, this is not the best architecture because if you have several people accessing your web page, it will cause several connections to your PLC and you may have poor performance for your web app.
I think you had better develop a modbus program which is reading the data from the PLC and store it into a database. Then your web app just gets the data from the database.
I hope it helps

Dynamically updating a web interface from a Python daemon

I'll briefly explain what I'm trying to achieve: We have a lot of servers behind ipvsadm VIPs (LVS load balancing) and we regularly move servers in/out of VIPs manually. To reduce risk (junior ops make mistakes...) I'd like to abstract it to a web interface.
I have a Python daemon which repeatedly runs "ipvsadm -l" to get a list of servers and statistics, then creates JSON from this output. What I'd now like to do is server this JSON, and have a web interface that can pass commands. For example, selecting a server in a web UI and pressing remove triggers an ipvsadm -d <server>... command. I'd also like the web UI to update every 10 seconds or so with the statistics from the list command.
My current Python daemon just outputs to a file. Should I somehow have this daemon also be a web server and serve its file and accept POST requests with command identifiers/arguments? Or a second daemon for the web UI? My only front end experience is with basic Bootstrap and jQuery usually backed by Laravel, so I'm not sure if there's a better way of doing this with sockets and some fancy JS modern-ism.
If there is a more appropriate place for this post, please move it if possible or let me know where to re-post.
You don't need fancy js application. To take the path of least resistance, I would create some extra application - if you like python, I recommend flask for this job. If you prefer php, then how about slim?
In your web application, if you want to make it fast and easy, you can even implement ajax mechanism fetching results based on interval to refresh servers' data every 10 seconds. You will fetch it from json served by independent, already existing deamon.
Running commands clicked on Web UI can be done by your web application.
Your web application is something extra and I find it nice to be separated from deamon which fetch data about servers and save it as json. Anytime you can turn off the page, but all statistics will be still fetching and available for console users in json format.

Long Polling System on Server side

I need a sort of consultation. I am building a web app in django(hosted in heroku) which need to communicate with 100 of embedded devices(writing in C++/C). The embedded devices send data(50kb) to the web app and the web app present this information in a form of graphs.
My concern is , is it wise to build a python polling system(Socket communication) in the server side ?
Or is it Error-prone and I should use services like CloudMQTT?
Thank you in advance for your answers.
Unless you wanna have another project on your hands, it'd be best to use a dedicated library.
This isn't python specific, but I feel like you could achieve your goal relatively easy with a simple ajax request.
Check out The WebSocket API.
EDIT:
Upon further reading I found the Python port of websockets

What Python Web Framework should I use with GWT to stream KML from Python Back-end?

I have a long-running process written in Python 2.7 that I would like to send KML files to my GWT application asynchronously as the KML files are generated.
I have been trying to determine what Python web framework I could use as the back-end with the Python process that could possibly allow the webapp to be hosted on Google AppEngine.
I was able to write a simple python webserver using Cherrypy that sent the kml using JSON from the back-end to GWT using an http request; however, I would like the files to be sent to GWT as they are generated since it may be several minutes between each one. What would be a relatively simple but effective way to achieve this? (Comet? Long-polling? Websockets?)
After researching more python web frameworks, I started experimenting with Tornado because it is non-blocking and seems like it could return data as it is generated possibly using long-polling as mentioned in this answer. However, it looks like GAE requires WSGI which would not allow a Tornado webserver to be non-blocking.
I have read answers to similar questions such as this one. However, I am not sure if updates in web frameworks, GWT, or GAE has changed what is the best option today, or whether some of these answers apply to my case.
What Python web framework would you recommend I use to send data to my asynchronous GWT app using long-polling or another method relatively simply? Could I use this web framework with GAE, or would I need to use something else?
If I understood the problem correctly you might don't need any special framework and you can solve it with what you have: Tasks API and Channel API.
With Tasks API you can perform long task and when the task is complete you can get a notification. You can combine it with the Channel API to push messages directly to the client when a particular task is complete.
You could use also the deferred library to simplify your life with tasks and maybe even using the PubNub for your push notifications, since the setup is easier and you can have many subscribers at the same time.

Categories

Resources