Learn

We’ll now break down each step in creating a minimal Flask app. The Python module that contains all the classes and functions needed for building a Flask app is called flask.

We can begin building our app by importing the Flask class, which is needed to create the main application object, from the flask module:

from flask import Flask

Now, we can create an instance of the Flask class. Let’s save the application object in a variable called app:

app = Flask(__name__)

When creating a Flask object, we need to pass in the name of the application. In this case, because we are working with a single module, we can use the special Python variable, __name__.

The value of __name__ depends on how the Python script is executed. If we run a Python script directly, such as with python app.py in the terminal, then __name__ is equal to the string '__main__'. On the other hand, if the script is being imported as a module into another Python script, then __name__ would be equal to its filename.

As we’ll see in the next exercise, this distinction can be useful when we have code that we want to be run only if the script is executed a particular way.

Instructions

1.

Import the Flask class from the flask module at the top of app.py.

2.

Create an instance of the Flask class, passing in __name__, and save the object to a variable called app.

3.

At the bottom of the script, try printing __name__. Then, run app.py.

What is the value of __name__?

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?