This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 4 years ago.
I'm running my flask web app on local from command line using python __init__.py command. I want to run/deploy flask web without using any command. Like spring web app.
init.py
from flask import Flask, url_for
from flask import jsonify
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware
import db_config
app = Flask(__name__)
app.config['APPLICATION_ROOT'] = '/my-web-app'
def simple(env, resp):
resp(b'200 OK', [(b'Content-Type', b'text/plain')])
return [b'Hello my app User']
#app.route("/")
def hello():
return "Hello world"
#app.route('/test', methods=['GET'])
def get_tasks():
db = db_config.getconnection();
cur = db.cursor()
cur.execute("SELECT * FROM emp")
items = cur.fetchall()
print(items)
db.commit()
cur.close()
db.close()
return jsonify({'tasks': items})
app.wsgi_app = DispatcherMiddleware(simple, {'/my-web-app': app.wsgi_app})
if __name__ == "__main__":
app.run()
Is there any way to create .WAR file of flask web app and deploy it on server? And start web app when we give request.
Is there any way to create a Java .WAR file from a Flask app? Possibly, but I really wouldn't recommend it. Python is not Java.
Is there any way to deploy a Flask application on a server? Oh yes, lots of ways. The Deployment Options page in the Flask documentation lists several options, including:
Hosted options: Deploy Flask on Heroku, OpenShift, Webfaction, Google App Engine, AWS Elastic Beanstalk, Azure, PythonAnywhere
Self-hosted options: Run your own server using a standalone WSGI container (Gunicorn, uWSGI, Gevent, Twisted), using Apache's mod_wsgi, using FastCGI, or using old-school vanilla CGI.
Of the self-hosted options, I would definitely recommend nginx rather than Apache as a front-end web server, and nginx has built-in support for the uWSGI protocol, which you can then use to run your app using the uwsgi command.
But I would definitely recommend looking into one of the hosted options rather than being in the business of running your own web server, especially if you're just starting out. Google App Engine and AWS Elastic Beanstalk are both good platforms to start out with as a beginner in running a cloud web application.
Related
I am using flask socket.io configured with eventlet on a free heroku instance. According to the flask socket.io documentation: https://flask-socketio.readthedocs.io/en/latest/ running socketio.run(app) will start a webserver if eventlet is installed.
I have a local react web app attempting to establish a WebSocket connection with this app engine instance. The web app uses socket.io and initially defaults to polling HTTP requests. These requests all timeout-- I'm unable to establish a web socket connection. I'm not sure what might be causing my issue, whether it is my app engine configuration or my flask socket.io setup.
Here is my initialization code for flask socket.io:
app = Flask(__name__)
socketio = SocketIO(app)
socketio.init_app(app, cors_allowed_origins="*")
..
..
if __name__ == '__main__':
socketio.run(app, debug=True)
Here is my Procfile:
web: python3 server.py
Here is the web app code attempting to set up the connection:
import io from 'socket.io-client'
const socketURL = "<app-engine-instance-url>:5000"
const socket = io.connect(socketURL)
You mentioned Heroku, then switched to App Engine. I assume you still meant Heroku?
The application running on Heroku must honor the $PORT environment variable and put the website on that port. Try this:
socketio.run(app, port=int(os.environ.get('PORT', '5000')))
Then from your client application connect to the Heroku URL for your app.
I have developed a Machine learning model in anaconda prompt using Python3 so to deploy it and to run it anywhere dynamically on GitHub or as a webservice what is the way?
I have already used Pickle command but it didn't work for me.
import pickle
file_name = "cancerdetection.ipynb"
It didn't deploy my model as webservice.
You can try using flask. It uses python and deploys as a web application. Here's some sample code you can try.
from flask import Flask, render_template, request
app = Flask(__name__, static_url_path='/static')
#app.route('/')
def upload_file():
return "hello world"
if __name__ == '__main__':
app.run(host="0.0.0.0")
By setting
host="0.0.0.0"
you're deploying your application on the net by using your local machine as a server.
I am new to GCP App engine. I am trying to make web server using flask on App engine. I tested the version of my code on localhost and it is working fine. But when I am trying to deploy it in the App Engine of GCP it is giving me this strange error "app logs".
error logs
Here is my code for the flask
app.run(threaded = True, host='127.0.0.1', port=80)
Thanks!!
You don't need to call app.run() in your main.py file -- App Engine will do that for you. Simply initialize the app variable in this file instead.
This must be really trivial, but i just cannot find a way to run a flask web service on a windows server (win server 2008). I can run it manually but how dot i get it to startup as a service so i can consume the service it code exposes.
Here is a simple example i am trying to deploy to a windows server:
from flask import Flask, request
from flask_restful import Resource, Api
from flask_cors import CORS
app = Flask(__name__);
CORS(app);
api = Api(app);
class Root(Resource):
def get(self):
return {'hello': 'world Root'}
api.add_resource(Root, '/');
if __name__ == '__main__':
app.run(debug=True)
The easiest way I found to install a python program as a windows service is by using NSSM
Run "nssm install " and in the fields for:
Path: <path to python.exe>
Arguments: <path to your python file>
Once this is done you will have a windows service installed and then you can start that and this will stay running all the time hosting your flask app
I've got a website written in bottle and I'd like to deploy it via Amazon's Elastic Beanstalk. I followed the tutorial for deploying flask which I hoped would be similar.
I tried to adapt the instructions to bottle by making the requirements.txt this:
bottle==0.11.6
and replaced the basic flask version of the application.py file with this:
from bottle import route, run
#route('/')
def hello():
return "Hello World!"
run(host='0.0.0.0', debug=True)
I updated to this version as instructed in the tutorial, and when I wrote eb status it says it's green, but when I go to the URL nothing loads. It just hangs there. I tried the run() method at the end as it is shown above and also how it is written in the bottle hello world application (ie run(host='localhost', port=8080, debug=True)) and neither seemed to work. I also tried both #route('/hello') as well as the #route('/').
I went and did it with flask instead (ie exactly like the Amazon tutorial says) and it worked fine. Does that mean I can't use bottle with elastic beanstalk? Or is there something I can do to make it work?
Thanks a lot,
Alex
EDIT:
With aychedee's help, I eventually got it to work using the following application file:
from bottle import route, run, default_app
application = default_app()
#route('/')
def hello():
return "Hello bottle World!"
if __name__ == '__main__':
application.run(host='0.0.0.0', debug=True)
Is it possible that the WSGI server is looking for application variable inside application.py? That's how I understand it works for Flask.
application = bottle.default_app()
The application variable here is a WSGI application as specified in PEP 333. It's a callable that takes the environment and a start_response function. So the Flask, and Bottle WSGI application have exactly the same interface.
Possibly... But then I'm confused as to why you need that and the call to run.