Hello! Welcome back to our final module, Deploying the Web Application.
In our last lesson, we established the crucial difference between a development environment (your local computer) and a production environment (the live server where users access your app). You learned that while the development environment is for experimenting and building, the production environment prioritizes stability, performance, and security.
Today, we'll put that knowledge into practice. We will take the AI Writing Assistant app you've built and configure it so it's ready for the production environment. This is a critical step before we can put it online. Our goal is to configure the Flask application for a production environment, which mainly involves disabling debug mode and using a more robust server.
1. The Problem with Our Current Setup
So far, you've been running your app using app.run(debug=True). This command is fantastic for development, but it's dangerous for a live application. It does two things we must change for production:
- It enables Debug Mode: This mode provides detailed error pages and an interactive debugger.
- It uses Flask's built-in development server: This server is not designed to handle real-world traffic.
Let's look at why these are problems.
The Danger of Debug Mode
When debug=True is active, and your application crashes, Flask displays a detailed error page. While this is helpful for you as a developer, it can leak sensitive information to the public, such as parts of your source code, configuration values, and the internal structure of your application. An attacker could use this information to find vulnerabilities.
This article explains the risks in more detail.
Flask Debug Mode Enabled in Production
The article 'Flask Debug Mode Enabled in Production' from Sourcery.ai clearly explains the security risks of leaving debug mode on.
Please read the 'What it is' section to understand the kind of sensitive information debug mode can expose.
The warning message you've been seeing in your terminal this whole time is there for a reason!

2. Using a Production-Ready Server
The second issue is the server itself. Flask's built-in server is great for development because it's simple and automatically reloads when you change your code. However, it's not built to be efficient or secure enough to handle multiple users at once on a live website.
For production, we need to use a Production WSGI Server. WSGI (Web Server Gateway Interface) is a standard for Python web applications. Think of it as a more robust and professional engine to run your Flask app.
There are several popular WSGI servers, but we'll use one called Waitress. It's easy to set up and works well on different operating systems.
Let's watch a video that explains why the development server isn't enough and how to use Waitress instead.
This video, 'Waitress as Flask server WSGI', introduces the concept of a WSGI server and demonstrates how to integrate Waitress into a Flask app.
Please watch the following two segments: Why use a WSGI server? (0:33 - 1:32): This part explains why the default Flask server is not suitable for production. How to use Waitress (1:55 - 3:50): This shows you how to install Waitress and replace app.run() with the serve() function from Waitress.
As the video shows, switching to Waitress is straightforward:
- You install it with
pip install waitress. - You import
servefrom thewaitresslibrary. - You call
serve(app, ...)instead ofapp.run(...).
3. Managing Two Configurations
We now face a new challenge. We need debug mode and the development server when we are working on the app, but we need debug mode off and the Waitress server for production. How can we easily switch between these two setups?
We can make our app smart enough to know which environment it's in. A common way to do this is by checking an environment variable. We've used environment variables before to store our Gemini API key securely. We can use another one to tell our app whether to run in "development" or "production" mode.
The video you just watched offers a great, simple technique for this.
Let's return to the 'Waitress as Flask server WSGI' video to see a practical way to manage both development and production configurations in the same file.
Watch the segment from 3:50 to 5:48. It demonstrates how to use a simple if/else statement to switch between app.run() for development and serve() for production based on a variable.
4. Let's Update Your Code
Now it's time to apply these changes to your app.py file.
Step 1: Install Waitress
Open your terminal (making sure your virtual environment is active) and run the following command:
pip install waitress
Step 2: Modify app.py
We will update the bottom of your app.py file to implement the logic for switching between environments. We'll use the os module to read an environment variable called FLASK_ENV. If it's set to 'production', we'll use Waitress. Otherwise, we'll default to the development server.
Replace the entire if __name__ == '__main__': block at the end of your app.py file with the following code:
import os # Make sure this is at the top of your file
from waitress import serve # Add this import with your other imports
# ... (keep all your existing Flask app code and routes here) ...
if __name__ == '__main__':
# Determine the mode (development or production)
# Render, our deployment platform, will set this to 'production' automatically.
mode = os.environ.get('FLASK_ENV', 'development')
if mode == 'production':
# Production mode: use Waitress server
print("Running in production mode.")
serve(app, host='0.0.0.0', port=5000)
else:
# Development mode: use the default Flask server with debug mode
print("Running in development mode.")
app.run(host='0.0.0.0', port=5000, debug=True)
Why host='0.0.0.0'?
You might notice we added host='0.0.0.0'. This tells the server to be accessible from any network interface, not just from your local machine. This is necessary for cloud deployment platforms like the one we'll use in the next lesson.
How to Test It:
-
Development Mode (Default): Simply run your app as usual:
python app.py. You should see the message "Running in development mode." and your app will behave as it always has, with debug mode active. -
Production Mode (Simulation): To test the production setup, you can temporarily set the environment variable in your terminal before running the script.
- On macOS/Linux:
FLASK_ENV=production python app.py - On Windows:
set FLASK_ENV=productionand then on a new linepython app.py
You should now see the message "Running in production mode." and notice that the terminal output looks different. You will no longer see the debug mode warnings.
- On macOS/Linux:
Test your understanding!
After making the code changes, a colleague asks you why you added the if/else block at the end of app.py. How would you explain its purpose in one or two sentences?
Show answer
This code checks an environment variable to see if the app should run in 'development' or 'production' mode. It allows us to use the convenient Flask debug server while we are coding, but automatically switches to the secure and efficient Waitress server when the app is deployed live.
5. A More Advanced Approach (For Future Reference)
For larger applications, developers often manage configurations in a more structured way using a separate config.py file and Python classes. This keeps your main app.py file cleaner.
You don't need to implement this for our current project, but it's a valuable concept to be aware of as you continue your programming journey.
The article 'Configuring Your Flask App' from Dev.to explains this professional approach. It's a great resource to bookmark for the future.
Skim through the sections 'Configuring Flask From Class Objects' and 'Configuration Variables'. Notice how it defines a ProdConfig and DevConfig class to hold different settings. This is the pattern you'll see in many professional Flask projects.
Conclusion
Great work! You've just taken a massive step in turning your project from a local script into a production-ready web application. This process of hardening your app for a live environment is a vital skill for any web developer.
Key Takeaways:
- Debug mode must be turned off in production to prevent security vulnerabilities.
- The built-in Flask server is for development only. For production, you must use a WSGI server like Waitress.
- We can use an environment variable (like
FLASK_ENV) to allow our application to behave differently in development versus production. - Your application is now configured to run securely and efficiently on a public server.
Now that our application code is ready, the next logical step is to find it a home on the internet.
In the next lesson, we will begin the actual deployment process. You will learn how to create a new web service on a cloud platform and prepare to upload your code.