Flask Configuration

Configuration

We run basic flask app in How to create a website with Python Flask and we set ENV config value (environment) to development. In flask, we can set different values (setting) base on environment (ENV). Default ENV is production. Later we will need to set debug mode, secret key , database url and add other new values to flask. Then we deploy to server, we need to set ENV to production and change the values again. So we need to prepare to change the values base on application environment.

Print all config values in your flask app

from flask import Flaskapp = Flask(__name__)
print(app.config)
@app.route("/")
def home():
return "This is Home Page"
if __name__ == "__main__":
app.run(debug=True)

Add new config value

ap.config['SECRET'] = "secret"
print(app.config)

ENV config

Production

ENV config value is which environment you app is running. Default ENV is production. Run production server when deploy

app.config['ENV'] = 'production'

Development

development mode reload the code when it is changed.

When you run flask you will see above warning. Then set below code

app.config['ENV'] = 'development'

Set configuration values depend on application environment

  • create config.py
class Config(object):
DEBUG = False
TESTING = False
class ProductionConfig(Config):
ENV = 'production'
class DevelopmentConfig(Config):
ENV = 'development'
DEBUG = True

means that in production server ENV is production. In development server ENV is development and debug mode is true.

You can set more config values.

  • in main.py
from flask import Flaskapp = Flask(__name__)
app.config.from_object("config.DevelopmentConfig")
@app.route('/')
def home():
return "home"
if __name__ == "__main__":
app.run()

--

--

--

Learning javascript and web-development

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building a website + blog with Gridsome, Tailwindcss, and Contentful Pt. 2 of 2

CLOUD COMPUTING

Things to consider while building a highly entertaining app like Netflix

Web Performance Essentials

A Snips-Operated Home with Android Voice Panel

CERN ROOT/RooFit Makefile structure on macOS and Linux

Logs and Traces: Two Houses Unalike in Dignity — Honeycomb

Two red houses with fence separating them from body of water.

How to Create Table Using Laravel Migration?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cho Zin Thet

Cho Zin Thet

Learning javascript and web-development

More from Medium

How to Run Hadoop on macOS (Apple M1 and Intel CPU) by One Command

PostgreSQL and PostGIS installation guide…

Introduction to CI pipelines on Github

Two people looking at code on a Silver Macbook Pro https://www.pexels.com/@divinetechygirl?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels

Finding Curve Inflection Points in PostGIS