About a local connection between Flutter and Python - python

I would like to connect Flutter with a Python backend. Several tutorials such as ML models using flask or online tutorials suggest to use the REST-API of FLASK to connect Flutter and Python.
Usually, a localhost is created with FLASK and the flutter app gets the information via the http package.
I need to create a flutter-python connection, that is very secure and local, that means, that when the App is released, the Python function may not work on a server, but on the phone.
Is is possible to realize the procedure of running the Flutter frontend initializing the Flask backend on a phone?

I did some research and found an answer to hopefully point you on the right track. The solution looks similar. Let me know!
https://stackoverflow.com/a/62308032/14514188
https://github.com/rikulo/socket.io-client-dart#usage-flutter
In Flutter env. it only works with
dart:io websocket,
not with
dart:html websocket,
so in this case you have to add 'transports': ['websocket'] when creates the socket instance.
IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
'transports': ['websocket'],
'extraHeaders': {'foo': 'bar'} // optional
});
The default Flask development server doesn't support websockets so you'll need to use another server. Thankfully it's simple to get eventlet working with Flask. All you should have to do is install the eventlet package using pip.
pip install eventlet
Once eventlet is installed socketio will detect and use it when running the server.
You can use chrome to double check what transport method is being used. Open your chrome dev tools Ctrl+Shift+I in Windows and go to the Network tab. On each network request you should see either transport=polling or transport=websocket
https://stackoverflow.com/a/62308032/14514188

Related

Setup websockets with Python/Django

Is there some way I could setup my architecture like this for my web application:
Backend - Django + DRF (Rest API)
Frontend - React
And on the backend setup a websocket Client to listen to an external websocket Server and then forward the data from the Client to a new Server that I will create. So in React I could listen to this websocket Server that I have created?
I tried implementing this in React to listen to an external websocket Server, but it just gave me headaches trying to use proxy to avoid CORS problems.
How should I approach this? Am I thinking straight here?
Hello Marcus C and welcome to StackOverflow!
Since you didn't post any code yourself I can't give you any concrete examples, but I can point you in the right direction. As you said yourself trying to use an external WebSockets server (such as Node.js with socket.io) is a pain. For this purpose the Django Channels library exists. It is really useful as it allows direct access to database and other Django-related stuff.
If you run Django in a Docker container, the best way to use Channels is to run two separate containers, one with say gunicorn or uWSGI servers for the synchronous part and another with Channels' recommended Daphne server for asynchronous part, both proxied by nginx. Standard (or rather common) way is to use /ws path prefix for the asynchronous endpoints.

How to deploy python flask application on another windows 10?

I have develop python flask application(REST API). Now I want to deploy this application on client system(Windows 10 Professional ). My client dont have any internet service.
Previously, I done in java that time I make a .war file and deployed in tomcat on client system. He was able to access REST API.
Now I want know any similar way to deploy python app on client system, on start system his able to access my REST API
use PyInstaller.
pip install pyinstaller
go to project dir
cd C:\Users\sandip\Desktop\MyPython
use
pyinstaller --onefile HelloFlask.py
If you just want to make your rest APIs accessible by other users in same network, you can simply do it without installing anything on client side by replacing the app.run() in your code to app.run(host= '0.0.0.0'). By default flask app runs on localhost, by changing it to latter causes it to run on your machines IP address, thus making it accessible by all the users under same network. You can read more on flask's documentation under the heading Externally Visible Server.
To deploy your app in production, you need a WSGI server, you can read about deployment of flask app here

Is it possible to use python tornado framework with phonegap?

I'm developing an multi-page web application using python tornado framework (includes API), and because the application is said to be used in mobile I decided to use phonegap to test on mobile.
Now the question, Is it possible to make tornado application listen to the phonegap (from mobile) ?
basically you want to install Tornado app in some/local server server, and create API calls from Phonegap -> Tornado server ?
It's possible, if you're developing locally - you have a local address that you define ( 127.0.0.1:8080 or whatever )
Then from phonegap you can generate Ajax calls towards your server.
The best approach in such case, is to not use Tornado with HTML files, with API work always with JSON outputs ....

flask websockets app without serving webpage

I have an existing Python flask app that uses blueprints for routing. The app doesn't serve webpages, it just provides REST API for the client. Now I'm trying to add WebSockets support to this flask app to push some notifications to the client (they should originate on the server, not respond to client requests). I found Flask-SocketIO library, but it says that:
"The application must serve a page to the client that loads the
Socket.IO library and establishes a connection."
Is there a way to use WebSockets in a flask app without serving webpages?
You can use the Flask-Sockets library that can be installed with pip. Just follow the documentation and examples shown in this page https://github.com/heroku-python/flask-sockets. It is fairly easy to use and does not need to have a page served by flask. Though you still need a page for your client's browser to originate from somewhere.
If you only want to use WebSockets you can also use the WebSockets library: https://pypi.org/project/websockets/. Also easy to use and not linked to flask.

Hosting Python Twilio server online?

I'm trying to complete an iOS Swift app that send SMS with custom body. I found the following tutorial and it worked for me, however, it requires having a running python server. Is there any site that you recommend where I can host the python server and access it remotely through the web? Are there any alternatives for doing this by skipping the python server and directly sending an HTTP request from the app?
Note: This is in iOS 11 and Xcode 9 with latest version of Swift. I am also a beginner with HTTP requests and server stuff.
Your best bet is to host it through heroku (initially). the pricing structure is built on activity (great if you are testing and need hosting to be as inexpensive as possible), auto recognition of python code, and they give you a url that you can access remotely (i.e. someapp.heroku.com) that should allow you to access in your app.

Categories