1
00:00:00,330 --> 00:00:03,630
Now that we've understood a little bit more about the terminal commands,

2
00:00:03,960 --> 00:00:08,960
let's come back to our Flask code and understand what the code is doing in a

3
00:00:09,300 --> 00:00:14,220
little bit more detail. For example, what is this name?

4
00:00:14,460 --> 00:00:18,870
Because we're creating this app from this Flask class

5
00:00:19,230 --> 00:00:23,310
and in order to initialize a new Flask application,

6
00:00:23,700 --> 00:00:27,600
there is only one required input,

7
00:00:28,020 --> 00:00:30,210
and that is the import_name.

8
00:00:31,380 --> 00:00:33,390
Let's go ahead and print it out.

9
00:00:33,720 --> 00:00:38,720
So if we print this __name__ and comment out the rest of our

10
00:00:39,600 --> 00:00:42,480
code and hit run,

11
00:00:43,800 --> 00:00:48,000
then you can see that it prints out __main_

12
00:00:48,030 --> 00:00:48,863
_.

13
00:00:49,320 --> 00:00:54,320
This name is one of the special attributes that's built into Python.

14
00:00:56,550 --> 00:00:57,990
At any given point,

15
00:00:58,170 --> 00:01:03,170
you could tap into the name to find out what is the current class, function,

16
00:01:06,030 --> 00:01:08,460
method, or descriptor's name.

17
00:01:09,210 --> 00:01:12,360
And when we get main, what it's telling us is

18
00:01:12,500 --> 00:01:17,000
basically we're executing the code in a particular module.

19
00:01:17,510 --> 00:01:22,510
So that means it's run as a script or from an interactive prompt,

20
00:01:23,930 --> 00:01:27,330
but it's not run from an imported module.

21
00:01:28,190 --> 00:01:31,790
It gives us this code here, if the name is equal to main,

22
00:01:32,090 --> 00:01:37,090
then execute something only if it's run as a script.

23
00:01:38,240 --> 00:01:40,730
This also takes us to

24
00:01:40,730 --> 00:01:45,440
one of the common ways that you'll see people run Flask apps.

25
00:01:45,830 --> 00:01:50,830
You'll see if __name__ is double equal to _

26
00:01:52,460 --> 00:01:55,250
_main__ as a string,

27
00:01:55,250 --> 00:01:59,570
so exactly what was printed just now when we printed out this name,

28
00:02:00,050 --> 00:02:01,400
well, if this is the case,

29
00:02:01,400 --> 00:02:05,810
then that means we're running the code from within this current file.

30
00:02:05,990 --> 00:02:09,199
We're running hello.py. So in that case,

31
00:02:09,229 --> 00:02:13,100
we're going to tap into our app and we're going to call the run method.

32
00:02:13,700 --> 00:02:15,260
Now this app.run

33
00:02:15,260 --> 00:02:20,060
basically does exactly the same thing as when we went into the terminal and

34
00:02:20,060 --> 00:02:25,040
we said flask run. But notice when we say flask run,

35
00:02:25,100 --> 00:02:28,580
firstly, we have to provide the FLASK_APP environment variable

36
00:02:29,060 --> 00:02:33,260
and secondly, we have to stop the code using control + c

37
00:02:33,680 --> 00:02:36,680
instead of using our normal run and stop.

38
00:02:37,010 --> 00:02:41,480
But if we use app.run, we can now use our standard controls.

39
00:02:41,540 --> 00:02:45,050
So I can simply hit run to run this hello.py,

40
00:02:46,070 --> 00:02:51,070
and it will start serving up our Flask app at this address just as it did

41
00:02:51,980 --> 00:02:56,750
before, when we did flask run. And instead of using control + c to quit,

42
00:02:56,870 --> 00:03:01,870
we can simply use the stop to stop our Flask application,

43
00:03:02,500 --> 00:03:04,240
making it a lot easier.

44
00:03:05,230 --> 00:03:10,180
Basically by providing the name to Flask, Flask 

45
00:03:10,180 --> 00:03:15,180
will check that this is the current file where the app code is located.

46
00:03:16,870 --> 00:03:21,760
And we're not in fact using an imported module. For example,

47
00:03:21,760 --> 00:03:26,760
if I was to import the random module and I was to print the random.name,

48
00:03:32,080 --> 00:03:36,880
then you can see the first thing that gets printed is the name of that module.

49
00:03:37,240 --> 00:03:39,880
But the name that's printed from hello.py

50
00:03:39,900 --> 00:03:44,470
which is the file that's being run is simply __main__.

51
00:03:44,740 --> 00:03:49,030
So this basically denotes the file that is currently being run

52
00:03:49,480 --> 00:03:52,600
and you say, Run 'hello' Well then inside hello

53
00:03:52,660 --> 00:03:54,760
name is going to be equal to main.

54
00:03:58,170 --> 00:03:58,260
Right.

