0
0
Flaskframework~30 mins

Environment-based configuration in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Environment-based configuration
📖 Scenario: You are building a simple Flask web application that needs to behave differently depending on the environment it runs in, such as development or production. This is common in real projects where you want to enable debugging in development but disable it in production for security.
🎯 Goal: Create a Flask app that reads its configuration from an environment variable called FLASK_ENV. The app should set debug mode to true if FLASK_ENV is development, and false otherwise.
📋 What You'll Learn
Create a Flask app instance named app
Read the environment variable FLASK_ENV to determine the environment
Set app.config['DEBUG'] to true if FLASK_ENV is development
Set app.config['DEBUG'] to false if FLASK_ENV is not development
Add a simple route / that returns 'Hello, Flask!'
💡 Why This Matters
🌍 Real World
Many web applications need to behave differently in development and production environments. Using environment variables to control configuration is a common and secure way to manage this.
💼 Career
Understanding environment-based configuration is essential for deploying Flask apps safely and effectively in real-world jobs.
Progress0 / 4 steps
1
Create the Flask app instance
Import Flask from flask and create a Flask app instance called app.
Flask
Need a hint?

Use Flask(__name__) to create the app instance.

2
Read the environment variable FLASK_ENV
Import os and create a variable called env that reads the environment variable FLASK_ENV using os.getenv.
Flask
Need a hint?

Use os.getenv('FLASK_ENV') to get the environment variable.

3
Set app.config['DEBUG'] based on environment
Use an if statement to set app.config['DEBUG'] = True if env == 'development', otherwise set app.config['DEBUG'] = False.
Flask
Need a hint?

Use a simple if and else to set the debug config.

4
Add a route that returns a greeting
Add a route to app for the path '/' using @app.route('/'). Define a function called home that returns the string 'Hello, Flask!'.
Flask
Need a hint?

Use @app.route('/') decorator and define a function that returns the greeting.