How can I deploy my Flask app with debugger and autoreload? - python

I have built a simple Flask app and I want do deploy it on Digital Ocean. I tried gunicorn but debugger and auto reloader no longer works. I was a php developer and I like to save changes and reload to see changes and errors.

You can use gunicorn do deploy your Flask app as described in this tutorial
Gunicorn comes with the ability to log and reload the application.
Apparently you cannot use gunicorn for debug mode as described here

Related

Should i use gunicorn with a python flaks application if i will put in a docker and then use it in a cloud environment

should i use gunicorn with a python flaks application if i will put in a docker and then use it in a cloud environment.
i have seen a lot of tutorials on how to do a flask application and deploy to a cloud service as a docker Image,
make the flask application
make the last line in docker with:
CMD ["python", "my_app.py"]
push the image and let the cloud service like AWS or azure use their load balancer with the amount of cpu and memeory you want to set as rule before spining another instance.
but i got into this tutorial and is using gunicorn
https://levelup.gitconnected.com/usage-of-gunicorn-for-deploying-python-web-applications-1e296618e1ab
where all the steps are the same, just the last line of the docker would be
CMD ["python" "-m", "gunicorn", "my_app:app"]
now uses the gnunicorn wsgi
and same way push that image to be use in the web application of the cloud.
i understand gunicorn can add more workers and thread as arguments in the cli; what would be the better approach or is one better than other?
Thanks guys.
Yes, you should always use a production server. As covered the the Flask docs, Deploy to Production: Run with a Production Server:
you should not use the built-in development server (flask run). The development server is provided by Werkzeug for convenience, but is not designed to be particularly efficient, stable, or secure.
Using gunicorn (or any other production WSGI server) is a necessary practice for deploying to production for speed, stability, and security. These concerns still persist, even if your app is deployed behind a load-balancer or proxy (like AWS load balancer). Both are necessary (you don't want to expose your WSGI server directly to the open internet, either, it should be behind a proper load balancer / proxy).

My Python Flask-SocketIO webapp is not running on Heroku

I'm having some troubles deploying this app on Heroku. The code is on a folder, where i created a git using git init, added everything to my git and then pushed everything to Heroku before running it.
I generated my requirements.txtand my procfile looks like this:
test: python "application.py" heroku ps:scale web=1
But on my webpage nothing will load, i'm assuming because i'm only running the Python part. How can i deploy it so that Heroku will know to run not only my Python script, but also the frontend part with the javascript and index.html files?
For development, you can serve static files with Flask.
For production, use cloud CDN (like Amazon's or Google's) or add a web-server to your app (Nginx or Apache or whatever).

How to deploy a Django production server on IBM Cloud?

I want to know how to deploy a Django production server on IBM Cloud.
Right now I have a Cloud Foundry Python app running without problems. But I'm using python manage.py runserver to start the app on the cloud.
There is a way to run the app without using the django server? Like, how we do running that kind of apps using Apache o Nginx web servers.
UPDATE:
I tried using the Procfile and gunicorn, but it gives me an error when it starts gunicorn.
This is my Procfile.
web: gunicorn my_app.wsgi --workers 2
And the error
ERR Starting gunicorn 18.0
I am using python 3.6 and django 1.11
Cloud Foundry provides out-of-the-box production quality hosting. You don’t need to configure nginx or apache. For more information on how cloud foundry works under the hood, see here
You would however normally configure the method for starting your application. The python buildback documentation describes a few options, for example:
Specify a Start Command
The Python buildpack does not generate a
default start command for your applications.
To stage with the Python buildpack and start an application, do one of
the following:
Required for Cloud Foundry v245 only: Supply a Procfile.
For more
information about Procfiles, see the Configuring a Production Server
topic. The following example Procfile specifies gunicorn as the start
command for a web app running on Gunicorn:
web: gunicorn SERVER-NAME:APP-NAME
Specify a start command with -c.
The following
example specifies waitress-serve as the start command for a web app
running on Waitress:
$ cf push python-app -c "waitress-serve --port=$PORT DJANGO-WEB-APP.wsgi:MY-APP"
Specify a start command in the application manifest by setting the command attribute.
For more
information, see the Deploying with Application Manifests topic.

How is Deploying Flask on AWS Elastic Beanstalk different from running script?

What is the difference between deploying a Flask application on an ec2 instance (in other words running your script on any computer) and deploying a Flask application via AWS Elastic Beanstalk? The Flask deployment documentation says that:
While lightweight and easy to use, Flask’s built-in server is not
suitable for production as it doesn’t scale well and by default serves
only one request at a time. Some of the options available for properly
running Flask in production are documented here.
One of the deployment options they recommend is AWS Elastic Beanstalk. When I read through Amazon's explanation of how to deploy a Flask app, however, it seems like they are using the exact same server application as comes built-in to Flask, which for example is single threaded and so cannot handle simultaneous requests. I understand that Elastic Beanstalk allows you to deploy multiple copies, but it still seems to use the built-in Flask server application. What am I missing?
TL;DR Completely different - Elastic Beanstalk does use a sensible WSGI runner that's better than the Flask dev server!
When I read through Amazon's explanation of how to deploy a Flask app, however, it seems like they are using the exact same server application as comes built-in to Flask
Almost, but not quite.
You can confirm that this isn't the case by removing the run-with-built-in-server section yourself - i.e. the following from the example:
if __name__ == "__main__":
# Setting debug to True enables debug output. This line should be
# removed before deploying a production app.
application.debug = True
application.run()
You'll stop being able to run it yourself locally with python application.py but it'll still happily run on EB!
The EB Python platform uses its own WSGI server (Apache with mod_wsgi, last I looked) and some assumptions / config to find your WSGI callable:
From Configuring a Python project for Elastic Beanstalk:
By default, Elastic Beanstalk looks for a file called application.py to start your application. If this doesn't exist in the Python project that you've created, some adjustment of your application's environment is necessary.
If you check out the docs for the aws:elasticbeanstalk:container:python namespace you'll see you can configure it to look elsewhere for your WSGI application:
WSGIPath: The file that contains the WSGI application. This file must have an "application" callable. Default: application.py
Elastic compute resources (AWS and others) generally allow for dynamic load balancing, and start more compute resources as they are needed.
If you deploy on a single ec2 instance, and this instance reaches capacity, your users will experience poor performance. If you deploy elastically, new resources are dynamically added as to ensure smooth performance.

How to deploy Flask Web app in Production server from PyCharm

I am new to Flask Web with Python. I developed a simple Flask with Pycharm. Now I want to deploy it on my college server. I remember when I develop php web app I used to copy it to www folder in var and run it from there.
Can someone explain me how do I deploy python web app in Linux production server.
First, you need to figure out what your server provides from its sysadmin. There are tens of ways to do this, and all of them have different answers.
Given that you have used php on the system before, I'll assume you are using Apache httpd.
First, you would need to have a wsgi provider installed (e.g. mod_wsgi) , and then wire your application into the wsgi provider.
See the mod_wsgi page and flask docs for more information on how to precisely do this.
https://code.google.com/p/modwsgi/
http://flask.pocoo.org/docs/0.10/deploying/
Another option is to have python bring its own webserver and optionally have a proxy redirect / load balance.

Categories

Resources