Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask production and development mode

I have developed an application with flask, and I want to publish it for production, but I do not know how to make a separation between the production and development environment (database and code), have you documents to help me or code. I specify in the config.py file the two environment but I do not know how to do with.

class DevelopmentConfig(Config):
    """
    Development configurations
    """
    DEBUG = True
    SQLALCHEMY_ECHO = True
    ASSETS_DEBUG = True
    DATABASE = 'teamprojet_db'
    print('THIS APP IS IN DEBUG MODE. YOU SHOULD NOT SEE THIS IN PRODUCTION.')


class ProductionConfig(Config):
    """
    Production configurations
    """
    DEBUG = False
    DATABASE = 'teamprojet_prod_db'
like image 572
Abdellah ALAOUI ISMAILI Avatar asked Jul 06 '17 14:07

Abdellah ALAOUI ISMAILI


People also ask

Can Flask be used for production?

Although Flask has a built-in web server, as we all know, it's not suitable for production and needs to be put behind a real web server able to communicate with Flask through a WSGI protocol. A common choice for that is Gunicorn—a Python WSGI HTTP server.

How do I run a Flask in developer mode?

Command LineThe flask run CLI command is the recommended way to run the development server. Use the --app option to point to your application, and the --debug option to enable debug mode.

What is Flask software development?

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.


2 Answers

One convention used is to specify an environment variable before starting your application.

For example

$ ENV=prod; python run.py

In your app, you check the value of that environment variable to determine which config to use. In your case:

run.py

import os
if os.environ['ENV'] == 'prod':
    config = ProductionConfig()
else:
    config = DevelopmentConfig()

It is also worth noting that the statement

print('THIS APP IS IN DEBUG MODE. YOU SHOULD NOT SEE THIS IN PRODUCTION.')

prints no matter which ENV you set since the interpreter executes all the code in the class definitions before running the rest of the script.

like image 180
Daniel Corin Avatar answered Sep 30 '22 13:09

Daniel Corin


To add onto Daniel's answer:

Flask has a page in its documentation that discusses this very issue.

Since you've specified your configuration in classes, you would load your configuration with app.config.from_object('configmodule.ProductionConfig')

like image 36
Nathan Wailes Avatar answered Sep 30 '22 14:09

Nathan Wailes