Now that we've understood a little bit more about the terminal commands,
let's come back to our Flask code and understand what the code is doing in a
little bit more detail. For example, what is this name?
Because we're creating this app from this Flask class
and in order to initialize a new Flask application,
there is only one required input,
and that is the import_name.
Let's go ahead and print it out.
So if we print this __name__ and comment out the rest of our
code and hit run,
then you can see that it prints out __main_
_.
This name is one of the special attributes that's built into Python.
At any given point,
you could tap into the name to find out what is the current class, function,
method, or descriptor's name.
And when we get main, what it's telling us is
basically we're executing the code in a particular module.
So that means it's run as a script or from an interactive prompt,
but it's not run from an imported module.
It gives us this code here, if the name is equal to main,
then execute something only if it's run as a script.
This also takes us to
one of the common ways that you'll see people run Flask apps.
You'll see if __name__ is double equal to _
_main__ as a string,
so exactly what was printed just now when we printed out this name,
well, if this is the case,
then that means we're running the code from within this current file.
We're running hello.py. So in that case,
we're going to tap into our app and we're going to call the run method.
Now this app.run
basically does exactly the same thing as when we went into the terminal and
we said flask run. But notice when we say flask run,
firstly, we have to provide the FLASK_APP environment variable
and secondly, we have to stop the code using control + c
instead of using our normal run and stop.
But if we use app.run, we can now use our standard controls.
So I can simply hit run to run this hello.py,
and it will start serving up our Flask app at this address just as it did
before, when we did flask run. And instead of using control + c to quit,
we can simply use the stop to stop our Flask application,
making it a lot easier.
Basically by providing the name to Flask, Flask
will check that this is the current file where the app code is located.
And we're not in fact using an imported module. For example,
if I was to import the random module and I was to print the random.name,
then you can see the first thing that gets printed is the name of that module.
But the name that's printed from hello.py
which is the file that's being run is simply __main__.
So this basically denotes the file that is currently being run
and you say, Run 'hello' Well then inside hello
name is going to be equal to main.
Right.