WEBVTT

0
00:00.330 --> 00:03.630
Now that we've understood a little bit more about the terminal commands,

1
00:03.960 --> 00:08.960
let's come back to our Flask code and understand what the code is doing in a

2
00:09.300 --> 00:14.220
little bit more detail. For example, what is this name?

3
00:14.460 --> 00:18.870
Because we're creating this app from this Flask class

4
00:19.230 --> 00:23.310
and in order to initialize a new Flask application,

5
00:23.700 --> 00:27.600
there is only one required input,

6
00:28.020 --> 00:30.210
and that is the import_name.

7
00:31.380 --> 00:33.390
Let's go ahead and print it out.

8
00:33.720 --> 00:38.720
So if we print this __name__ and comment out the rest of our

9
00:39.600 --> 00:42.480
code and hit run,

10
00:43.800 --> 00:48.000
then you can see that it prints out __main_

11
00:48.030 --> 00:48.863
_.

12
00:49.320 --> 00:54.320
This name is one of the special attributes that's built into Python.

13
00:56.550 --> 00:57.990
At any given point,

14
00:58.170 --> 01:03.170
you could tap into the name to find out what is the current class, function,

15
01:06.030 --> 01:08.460
method, or descriptor's name.

16
01:09.210 --> 01:12.360
And when we get main, what it's telling us is

17
01:12.500 --> 01:17.000
basically we're executing the code in a particular module.

18
01:17.510 --> 01:22.510
So that means it's run as a script or from an interactive prompt,

19
01:23.930 --> 01:27.330
but it's not run from an imported module.

20
01:28.190 --> 01:31.790
It gives us this code here, if the name is equal to main,

21
01:32.090 --> 01:37.090
then execute something only if it's run as a script.

22
01:38.240 --> 01:40.730
This also takes us to

23
01:40.730 --> 01:45.440
one of the common ways that you'll see people run Flask apps.

24
01:45.830 --> 01:50.830
You'll see if __name__ is double equal to _

25
01:52.460 --> 01:55.250
_main__ as a string,

26
01:55.250 --> 01:59.570
so exactly what was printed just now when we printed out this name,

27
02:00.050 --> 02:01.400
well, if this is the case,

28
02:01.400 --> 02:05.810
then that means we're running the code from within this current file.

29
02:05.990 --> 02:09.200
We're running hello.py. So in that case,

30
02:09.230 --> 02:13.100
we're going to tap into our app and we're going to call the run method.

31
02:13.700 --> 02:15.260
Now this app.run

32
02:15.260 --> 02:20.060
basically does exactly the same thing as when we went into the terminal and

33
02:20.060 --> 02:25.040
we said flask run. But notice when we say flask run,

34
02:25.100 --> 02:28.580
firstly, we have to provide the FLASK_APP environment variable

35
02:29.060 --> 02:33.260
and secondly, we have to stop the code using control + c

36
02:33.680 --> 02:36.680
instead of using our normal run and stop.

37
02:37.010 --> 02:41.480
But if we use app.run, we can now use our standard controls.

38
02:41.540 --> 02:45.050
So I can simply hit run to run this hello.py,

39
02:46.070 --> 02:51.070
and it will start serving up our Flask app at this address just as it did

40
02:51.980 --> 02:56.750
before, when we did flask run. And instead of using control + c to quit,

41
02:56.870 --> 03:01.870
we can simply use the stop to stop our Flask application,

42
03:02.500 --> 03:04.240
making it a lot easier.

43
03:05.230 --> 03:10.180
Basically by providing the name to Flask, Flask 

44
03:10.180 --> 03:15.180
will check that this is the current file where the app code is located.

45
03:16.870 --> 03:21.760
And we're not in fact using an imported module. For example,

46
03:21.760 --> 03:26.760
if I was to import the random module and I was to print the random.name,

47
03:32.080 --> 03:36.880
then you can see the first thing that gets printed is the name of that module.

48
03:37.240 --> 03:39.880
But the name that's printed from hello.py

49
03:39.900 --> 03:44.470
which is the file that's being run is simply __main__.

50
03:44.740 --> 03:49.030
So this basically denotes the file that is currently being run

51
03:49.480 --> 03:52.600
and you say, Run 'hello' Well then inside hello

52
03:52.660 --> 03:54.760
name is going to be equal to main.

53
03:58.170 --> 03:58.260
<v 1>Right.</v>