How to set up production environment for your Flask project on Repl.it
Flask development server is good during the development cycle, but not in production work. As the Flask documentation says:
Flask’s built-in server is not suitable for production
So, we need a production server for Flask app after the end of project development. Based on this I prepare this template for set up production environment for Repl.it without using Flask (more correctly Werkzeug) development server. We will use Gevent as webserver and Flask-Compress for compressing static files because Repl.it not support gzip/deflate for response content
- First we need to create sample Flask app from examples
- Next we add Gevent and Flask-Compress packages to project
Add import WSGI server from Gevent package
- Setup params for Repl.it and start Gevent WSGI server with our Flask app for serving forever
- Now we need to do "monkey-patching" standart Python library for work with async Gevent module. For this import
on the first line of your
main.py file before other imports and expressions. On the next line paste
This aplly runtime patches to synchoronous functions in standart Python library make it "gevent-friendly". For details see Gevent monkey module docs.
Now we have Flask app run on production-ready Gevent WSGI server, next we need enable compressing for static content such as HTML, CSS and JS files
- Add import Flask-Compress
- Init compress with default params for our Flask app
- If we already don't need debug capabilities for our project add secret with key
production, otherwise set value to
Now we have Flask app with production-ready Gevent WSGI server and compress for text-based static files using gzip/deflate/brotli algorithms. For test this create
template folder with
index.html file, import
hello_world function to
and paste some boilerplate HTML on your choice into
After start your app you may see in network tab browser's dev tools that your web app serving with gzip/deflate/brotli compressing in dependence of your browser support.
Thanks so much for this, I had no idea you could run a production server in python, and instead resorted to hosting my own server with apache, which got way to complex for my liking. Your method is way faster to setup on both replit and on my own.