Running a Flask App with Environment Variables
Hi everyone, this is Abid from Pintinist. I hope you are doing well. In our previous video, we introduced the philosophy and built a most simple Flask application. Let's continue the journey in this video.
So now we want to run this application we created in our previous video. There are two possible ways we have: exporting an environment variable or programmatically. First, let's take a look at how we can run it by exporting the environment variable.
Flask application include a development web server that can be started with the flask run command. This command looks for the name of the Python script it contains the application instance in the FLASK_APP environment variable. To start our application, first make sure the virtual environment you created earlier is activated. Then define the environment variable as export FLASK_APP=app.py. You must replace app.py with your file name and make sure that you are inside the right directory. If you are on Windows, you can export that variable by using the command as set FLASK_APP=app.py.
Once the server starts up, it goes into a loop that accepts a request and services them. This loop continues until you stop the application by pressing Ctrl+C. With the server running, open your web browser and type localhost:5000. Great, you can see our application is working successfully. If you type anything else after the base URL, the application will not know how to handle it and will return an error code 404 to the browser, the familiar error that you get when you navigate to a web page that does not exist.
Running a Flask App Programmatically
Now let's try another way to run this application. The Flask development web server can also be started programmatically by invoking the app.run() method. Older versions of Flask, they didn't have the flask command, required the server to be started by running the application's main script which had to include the main section as if __name__ == "__main__":. Then inside this section we have to call app.run() method.
Now if we run this file as python app.py, you can see your application is working in the same way as it did by using the environment variable. While the flask run command makes this practice unnecessary, the app.run() method can still be useful on certain occasions such as unit testing. Awesome.
Understanding Dynamic Routing
You may have seen URLs where we pass some parameters and get the result accordingly. This kind of URLs is called dynamic routing in the context of Flask. So let me define another route in our application. So I will say @app.route('/<name>'). Then I will define the view function as print_name, and inside this function we will simply greet this name and return it as a response. But we need to pass the name parameter to this view function to access it inside the function. Great.
To test the dynamic route, make sure the server is running, then navigate to localhost:5000/your_name. I'll pass Abdo. The application will respond with a personalized greeting using the name dynamic argument. Try using different names in the URL to see how the view function always generates the response based on the name given.
There's a lot more in the routing because it's an important part of API's implementation, so we will take a detailed look at it in our later videos. So leave it for now.
Introduction to Flask's Debug Mode
And let's jump to the debugging in Flask. Flask applications can optionally be executed in debug mode. In this mode, two very convenient modules of the development server, called the reloader and the debugger, are enabled. By default, when the reloader is enabled, Flask watches all the source code files of your project and automatically restarts the server when any of the files are modified. Having a server running with a reloader enabled is extremely useful during development because every time you modify and save a source file, the server automatically restarts and picks up the change.
While a debugger is a web-based tool that appears in your browser when your application raises an unhandled exemption. The web browser window transforms into an interactive stack trace that allows you to inspect source code and evaluate expressions in any place in the call stack.
How to Enable Debug Mode
So we can enable this module according to the method we choose to run our application. If you're running your application by exporting the FLASK_APP environment variable, we have to export another variable as FLASK_DEBUG=1. But if you're running your application programmatically, we can pass debug=True in our app.run() call.
Great. If we run our application, it's inside the debug mode. And if I make a change in my code, it will restart the server, picks the change automatically by using the reloader module. And if we make a mistake and it throws an unhandled exception, it will provide an interactive stack trace to trace the error by using the debugger module. Awesome.
Exploring Flask Command-Line Options
Now let's talk about the last thing we have to discuss in this video, and it's the command-line options. The flask command supports a number of options. To see what's available, you can run flask --help or just write flask without any arguments. You can see it brings all the commands, options, examples, and description.
The flask shell command is used to start a Python shell in the context of this application. You can use this session to run maintenance tasks or to debug issues. We will use it in our later videos while implementing APIs.
The --host argument is particularly useful because it tells the web server what network interface to listen to for connections from clients. By default, Flask's development web server listens for connections on localhost, so only connections originating from the computer running the server are accepted. But if we run the command as flask run --host and pass an IP address, the webserver should now be accessible from any computer in the network at that particular IP address.
The reload, no-reload, debugger, and no-debugger options provide a great degree of control on top of the debug mode setting. For example, if debug mode is enabled, no-debugger can be used to turn off the debugger while keeping debug mode and the reloader enabled.
Conclusion and Recap
I think that's enough for this video. We have learned a lot in this video. We have talked about how to run a flask application, how the dynamic routing works in Flask, and we explored the debug mode for better development experience. And finally, we talked about different command-line options. That's great.
If you like the content, thumbs up, and be sure to subscribe to my channel and hit the bell icon so you will never miss any fantastic video in the future. Thanks for watching.