Recently, when I rebuilt the Flask project, I found that the environment variables of the project were extremely chaotic, which was very difficult to manage. And, more importantly, I need to run both the Devlopment and Production project environment with custom commands.

Custom command tool — flask- CLI

Flask-cli was already supported in Flask 1.0+, but after looking through the flask-CLI documentation, I found that the method constraints of the custom commands provided in the documentation were a bit too many and not very detailed. Flask-cli was later developed based on Click by looking at the source code for flask-CLI.

Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as The “Command Line Interface Creation Kit”, runs without any control system and operates without sensible signals of the box.

Click is a Python package that uses clean code and composition methods to create beautiful command-line commands. It is also a highly configurable command line tool creation kit with reasonable default Settings.

Manage environment variables — python-dotenv

In order to avoid the need to introduce environment variables before flask run each time, flask-CLI is used. Env and.flaskenv files need to be created in the project.

Variables set on the command line override variables in.env, and variables in.env override variables in.flaskenv. .flaskenv should be used for public variables such as FLASK_APP and.env should be used for private variables and not committed to the repository.

Before using python-dotenv, you also need to install dependencies via PIP (I’m using Pipenv here) :

pipenv install python-dotenv
Copy the code

Env files are usually private variables or variables that involve privacy, even if they are not uploaded to Git.

Here are a few variables in.flaskenv:

#. Flaskenv FLASK_APP = "FLASK_ENV" FLASK_RUN_HOST = '0.0.0.0' FLASK_RUN_PORT = '5000'Copy the code

Reading environment variables

Here’s how to read variables in a project:

import os
from dotenv import load_dotenv
Env. Override specifies whether an environment variable can be overridden. The default is False
load_dotenv(dotenv_path='.flaskenv',override=True)
FLASK_ENV is read by importing the field name in the os.getenv() method
env = os.getenv("FLASK_ENV")
print(env)
Copy the code

Custom commands

Here the entry file for my project is app.py:

# app.py
import click
import os
from dotenv import load_dotenv
Load the variable file
load_dotenv(dotenv_path='.flaskenv')

The following code executes different commands based on the value of dev passed in on the command line, resulting in a command that distinguishes development from production.
@click.command()
@click.option('--dev', default=os.getenv("FLASK_ENV"),help='environment variable')
def runserver(dev):
    os.system("FLASK_ENV=%s flask run" % (dev))

if __name__ == '__main__':
    runserver()
Copy the code

Run code:

# Development environmentPy or python app.py --dev=developmentCopy the code
# Production environment
python app.py --dev=production
Copy the code

So far, so much has been done in the first stage, but there are still many deficiencies. I hope you can help me put forward some suggestions. I will have more practices in the future and update these contents as soon as possible.