0

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.

Anil Jagtap
  • 1,740
  • 4
  • 27
  • 44

1 Answers1

4

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.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • I'm running as a beginner. Can you refer me any example to run my app using **nginx **? – Anil Jagtap Feb 20 '19 at 11:17
  • @AnilJagtap: The page in the Flask documentation about [deploying using uWSGI and nginx](http://flask.pocoo.org/docs/1.0/deploying/uwsgi/) provides a sample nginx configuration. Is that what you're looking for? – Daniel Pryden Feb 20 '19 at 13:16
  • Yes i'm looking for same. And I want to deploy my app on production too. – Anil Jagtap Feb 21 '19 at 04:20