In this topic, we will discuss how to configure an application. Imagine you've written the code, and everything works fine. Is this really the end? Unfortunately, no. You also need to configure the application. It's very important, as you need to indicate the limits of what the app can do. For example, you can allow the server to update the app automatically without restarting it. You can also configure the protection of the data and server from hackers. Let's begin!
General settings
First, let's talk about the development mode. If you remember, when you start a server, it prints several lines to the console:
Take a look at * Debug mode: off. It states that we are not in the development mode right now. The development mode basically provides you with a built-in debugger. It can point out your mistakes and show a detailed traceback in the browser. There are several ways to enable this mode; one of them is to use app.config. The config attribute of a Flask app is a dictionary. You can enable the development mode like this:
app.config['DEBUG'] = True
Familiar, isn't it? Like with dictionaries, we use a key, DEBUG in this case, and set a value. A simple tool conceals the huge potential for project development.
Let's also talk about the Environment: production line. It tells us that the Flask is running as a server. Actually, that's why it warns us not to use the current configuration — we are working with a test web server that is not protected. We'll talk about how to switch to an operational web server a little later.
Changing the settings
To make your config better, let's see how you can make several changes in the settings of your application at once:
app.config.update({
'DEBUG': True,
'TESTING': True
})
In the example above, we have enabled both debug and test modes. So, you can set only one specific setting or multiple values at once.
Now, let's see what else we can do. There are several other settings that we can configure besides the debug mode. First, let's deal with the secret key, which will keep our app users' privacy. It can be created by using the os module:
import os
key = os.urandom(24) # specify the length in brackets
app.config['SECRET_KEY'] = key
Alternatively, you can use the same app.config.update method to put the key in the settings and become a valiant defender of personal data. Instead of using the os module, we can hit the keyboard 3 times and store the result as a secret key. It is, in fact, an ordinary string, a code word. However, os.urandom is more practical.
We will cover the secret key purpose in the upcoming topics, but it's good to know how to configure it since it's one of the most important settings.
Exception distinction
Next comes a rather handy and intriguing setting that provides us with the ability to switch exceptions. This means that we can run our server and tell it, for example, to show specific errors instead of showing Internal Server Error only. We can switch the exception handling method. Sometimes, it can be useful when you need to make a web application that works at all costs and does not crash with an error. For this purpose, Flask has a separate setting that is called PROPAGATE_EXCEPTIONS. You can turn it on in the following way:
app.config['PROPAGATE_EXCEPTIONS'] = True
In other words, we can tell Flask to blend all exceptions into one. There are a few other settings that we will discover in the future. For the moment, we don't need them.
Configuration files
We have figured out how to use a couple of commands to customize an application. Imagine a situation when we have a project with several applications. This approach will no longer be convenient, as you'll need to re-set each setting for each application or copy them from the previous one. We can use configuration files instead. We'll create a Python file that contains our settings just like regular variables:
# inside file 'settings.py'
DEBUG = True
Now, we need to upload this file to our project and force the applications to use it. We can do it by this:
from flask import Flask
app = Flask('main')
app.config.from_object('settings')
@app.route('/')
def index():
return 'Wow! Debugging now!'
app.run()
Here is your simple application with a couple of changes. Specifically, we use the app.config.from_object('settings') method to load it to the app. Now, you can start it and check:
Environment variables
In addition to Python configuration files, we can store settings in system variables! We need to create them first; there are several ways to do this. We will use the command line for it.
Linux:
$ export FLASK_CONF_VAR=path/to/settings.py
Windows:
> set FLASK_CONF_VAR=path/to/settings.py
The next step is to show our program that there are settings to adopt from a system variable. To do this, use the from_envvar method. The rest of the code remains:
from flask import Flask
app = Flask('main')
app.config.from_envvar('FLASK_CONF_VAR')
@app.route('/')
def index():
return 'Wow! Debugging now!'
app.run()
If you launch the application again, you'll see the same output as before. It means that our program absorbed the settings and successfully launched the debug mode that we have set in the file.
Conclusion
In this topic, we have talked about general Flask settings and how we can apply them. One of the most important things in Flask is the developer mode. It provides a detailed log of the program errors. You can turn it on manually in the console, store the settings in a file or a special system variable. They speed things up terrifically. Good luck with the code challenges!