Prepare your application for deployment
Development and production do not imply the same security, reliability and performance level. These requirements imply specific tools that will impact the code structure.
A basic application as an example¶
Create a file main.py, put the following content inside, then install Taipy with pip install taipy
:
import taipy as tp
gui = tp.Gui(page="# Getting started with *Taipy*")
tp.run(gui, title="Taipy Demo")
You can run this application with python main.py
and access it from your browser.
If you look at the console, you will see the following output:
* Server starting on http://127.0.0.1:5000
* Serving Flask app ‘Taipy’ (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Server starting on http://127.0.0.1:5000
Flask generates this warning because it is a development and not a production server.
Gunicorn¶
Following the recommendation of Flask we will use Gunicorn for production. Gunicorn will run your application differently than Python does. You should update the application to:
import taipy as tp
gui = tp.Gui(page="# Getting started with *Taipy*")
# Here we are defining the `app` variable.
# _Gunicorn_ will use this variable to serve your application.
app = tp.run(gui, title="Taipy Demo", run_server=False)
The application is now ready to be served by Gunicorn.
Note
Click here to obtain more information on the run_server
parameters.
Running the application with Gunicorn¶
First, install Gunicorn and Gevent. Gevent will be used by Gunicorn as a worker:
pip install gunicorn gevent
Note
If you want more information about running Gunicorn with Gevent, have a look at the official documentation.
Now start Gunicorn by running:
gunicorn -k gevent -w 1 --bind=0.0.0.0:5000 --timeout 1800 main:app
Your application is now responding, and you can access it from your browser.
Using the same code in development and production¶
Debugging with Gunicorn is not as easy as debugging with the python
command. To be able to switch
from debugging to ready to production, you should follow this structure:
import taipy as tp
gui = tp.Gui(page="# Getting started with *Taipy*")
if __name__ == '__main__':
# Execute by the _Python_ interpretor, for debug only.
tp.run(gui, title="Taipy Demo")
else:
# Execute by _Gunicorn_, for production environment.
app = tp.run(gui, title="Taipy Demo", run_server=False)
You can now run your application with both commands.
For production:
gunicorn -k gevent -w 1 --bind=0.0.0.0:5000 --timeout 1800 main:app
For development:
python main.py