1
00:00:05,350 --> 00:00:06,190
Welcome back.

2
00:00:06,190 --> 00:00:07,530
Let's get started with flasks.

3
00:00:07,540 --> 00:00:13,890
By going through a very simple Hello World site example we're going create our first website flask and

4
00:00:13,890 --> 00:00:17,580
this is essentially the simplest site possible with a flask web framework.

5
00:00:17,670 --> 00:00:23,040
We're simply going to just return a single string Hello puppy onto a web page just in case you have

6
00:00:23,040 --> 00:00:23,750
installed the app.

7
00:00:23,760 --> 00:00:28,530
Make sure you've either done Condon's flask or pitmen small flask at your command line.

8
00:00:28,530 --> 00:00:33,690
Let's go through the code and then we'll jump to the text editor and actually write it out ourselves.

9
00:00:33,720 --> 00:00:37,590
Here we see the code that we're going to be using to create our simple web page.

10
00:00:37,650 --> 00:00:41,640
Let's go line by line and discuss the details of all this and then we'll head over to the text editor

11
00:00:41,640 --> 00:00:43,050
and type it out.

12
00:00:43,050 --> 00:00:48,090
So first off for saying from that flask package import the flask class.

13
00:00:48,090 --> 00:00:51,350
So notice here the capitalization is very easy to make a typo there.

14
00:00:51,420 --> 00:00:57,130
That flask class has upper case f the package itself is lowercase f.

15
00:00:57,270 --> 00:00:58,240
Then we have this line.

16
00:00:58,260 --> 00:01:00,480
We're saying app is equal to flask.

17
00:01:00,510 --> 00:01:04,090
And we're passing an underscore underscore name underscore underscore.

18
00:01:04,380 --> 00:01:09,120
Essentially what this line is doing is it's creating an application object over on the left hand side

19
00:01:09,390 --> 00:01:16,500
as an instance of the class flask imported from the Flast package from the first line and the name variable

20
00:01:16,560 --> 00:01:19,700
that's being passed into that flask class call.

21
00:01:19,860 --> 00:01:21,920
That's a python pre-define a variable.

22
00:01:21,960 --> 00:01:27,480
As we've previously talked about which is then set to the name of the module in which it is used essentially

23
00:01:27,810 --> 00:01:32,430
answering the question Are we running this script that correctly and flask is able to use the location

24
00:01:32,430 --> 00:01:38,120
of the module that we pass in as a starting point when it needs to load other resources or other files.

25
00:01:38,130 --> 00:01:42,560
Those are things such as template files which were going to be discussing later on.

26
00:01:42,570 --> 00:01:48,130
So once you've created that application where we can do is add pages to it and in this case let's jump

27
00:01:48,130 --> 00:01:54,830
over past that route function or route decorator and go down here to where we see Death Index return.

28
00:01:54,940 --> 00:01:55,970
Hello puppy.

29
00:01:56,110 --> 00:02:02,290
What this function is doing is defining a page index with the function and then it's going to return

30
00:02:02,440 --> 00:02:08,710
the string later on we're going to return the ability to actually render an entire template of HTML

31
00:02:08,710 --> 00:02:09,270
code.

32
00:02:09,490 --> 00:02:14,230
But it's that all we're doing right now is just returning a single string and it actually has h tim

33
00:02:14,230 --> 00:02:15,090
l code in it.

34
00:02:15,290 --> 00:02:20,800
It's saying hello Papy as a header one which means when we actually call that index page we're going

35
00:02:20,800 --> 00:02:21,990
to return that string.

36
00:02:22,000 --> 00:02:27,430
The browser is going to read that in as h HTML code and we should see help Papy in strong large bold

37
00:02:27,490 --> 00:02:29,020
text.

38
00:02:29,030 --> 00:02:35,520
Now the decorator above that function directly links that page to whatever route on your web application.

39
00:02:35,540 --> 00:02:40,460
It should be at for instance right now we're just saying a single forward slash which is essentially

40
00:02:40,460 --> 00:02:43,310
the same thing as your home page or your domain.

41
00:02:43,310 --> 00:02:49,850
So if you're launching this site at WW I love puppies dot com then if you went to AILA politico.com

42
00:02:49,910 --> 00:02:51,840
that single home page you would see.

43
00:02:51,980 --> 00:02:53,080
Hello puppy.

44
00:02:53,120 --> 00:02:57,150
Later on we're going to show you how you can add multiple pages through routing.

45
00:02:57,260 --> 00:03:03,470
But right now all this is saying is with our application route to this your L and in this case since

46
00:03:03,470 --> 00:03:04,520
it's a single slash.

47
00:03:04,640 --> 00:03:10,000
This is your home page and then return back that function which is the index page and it returns back.

48
00:03:10,010 --> 00:03:11,660
Hello puppy.

49
00:03:11,750 --> 00:03:16,250
At the very end we have this line which as we've already discussed in the Python section where we say

50
00:03:16,340 --> 00:03:22,070
if name is equal to main essentially if you're running the script then run your application and later

51
00:03:22,070 --> 00:03:25,860
on also see how he can set debug equal to true inside of this run call.

52
00:03:25,970 --> 00:03:31,610
So we can debug our application in real time if we're ever actually serving this to the Web and other

53
00:03:31,610 --> 00:03:32,840
people are going to be using it.

54
00:03:32,900 --> 00:03:36,250
You want to make sure you set debug equal to false which is the default.

55
00:03:36,350 --> 00:03:37,770
So that's all the code.

56
00:03:37,940 --> 00:03:42,260
Let's go ahead and go back to our text editor and type this out and see if it works.

57
00:03:42,300 --> 00:03:43,850
Lori here I am at my desktop.

58
00:03:43,940 --> 00:03:50,100
I'm have a flask underscore examples folder and underneath the folder I'm going to create a new file.

59
00:03:50,240 --> 00:03:52,640
And I'm just going to call it basic dot PI.

60
00:03:52,670 --> 00:03:56,240
You can technically call it whatever you want just do not call it flasks that pi.

61
00:03:56,270 --> 00:03:57,800
Otherwise Python's going to confuse.

62
00:03:58,040 --> 00:03:59,950
When you try to import things from flask.

63
00:03:59,960 --> 00:04:03,320
So call it essentially anything you want that's not flasks of Pi.

64
00:04:03,680 --> 00:04:05,150
OK so basic that pie.

65
00:04:05,210 --> 00:04:15,170
And what I want to do now is say from flask import flask that I will create my flask application by

66
00:04:15,170 --> 00:04:23,710
saying grab an instance of this flask class and then pass in my predefined name variable then when I'm

67
00:04:23,710 --> 00:04:29,410
going to do is grab my application and I'm going to set a route.

68
00:04:29,410 --> 00:04:32,790
And the first route is just going to be to the home page or the domain name.

69
00:04:32,920 --> 00:04:37,900
So I just have a single forward slash later on I'll be more clear what this line is doing when we actually

70
00:04:37,900 --> 00:04:40,720
link it to other pages in our application.

71
00:04:40,720 --> 00:04:45,540
But right now this is just a single page application and below this we're going to have the function

72
00:04:45,540 --> 00:04:47,410
that defines what is actually shown.

73
00:04:47,490 --> 00:04:53,120
When you go to that specific route on your application in this case the home page and we're going to

74
00:04:53,120 --> 00:05:00,720
return the string to the browser that says Hello puppy or whatever we want.

75
00:05:02,080 --> 00:05:05,260
And it's going to be a better one.

76
00:05:05,340 --> 00:05:11,460
And then finally here at the bottom we're going to say if name is equal to Main take my application

77
00:05:12,180 --> 00:05:20,190
and run it I'm going to say this and let's run now this python script at our command line will call

78
00:05:20,250 --> 00:05:21,640
Python basic thought pie.

79
00:05:21,690 --> 00:05:27,840
And when you run this expand this just a little bit you should notice now it says running at HTP 1 2

80
00:05:27,840 --> 00:05:29,980
7 0 0 0 1 5000.

81
00:05:30,090 --> 00:05:34,430
If you're running multiple flask examples for instance you have multiple tabs open.

82
00:05:34,590 --> 00:05:37,400
You may see this last four digit number change a little bit.

83
00:05:37,470 --> 00:05:40,610
We're going to do is we're going to highlight this and copy it.

84
00:05:40,620 --> 00:05:45,930
Be careful not to use Control-C copy because that actually quits the server and let's type this or paste

85
00:05:45,930 --> 00:05:48,450
this into our browser and check it out.

86
00:05:48,480 --> 00:05:50,380
I'm going to drag over my browser now.

87
00:05:50,520 --> 00:05:51,040
Here it is.

88
00:05:51,060 --> 00:05:52,440
And it says hello puppy.

89
00:05:52,440 --> 00:05:54,410
So you can zoom in here and check out.

90
00:05:54,430 --> 00:05:55,250
Hello puppy.

91
00:05:55,260 --> 00:05:57,050
You should see it as a header one.

92
00:05:57,170 --> 00:06:03,390
And if I right click and say view page source I get to see the actual source that I returned back H-1.

93
00:06:03,390 --> 00:06:04,340
Hello puppy.

94
00:06:04,340 --> 00:06:09,480
And as I mentioned later on we're actually going to pasan for HTML files to our flask application.

95
00:06:09,480 --> 00:06:14,370
But notice here it's just at the home page of this local Your L or IP address we have.

96
00:06:14,370 --> 00:06:15,760
Hello puppy.

97
00:06:15,810 --> 00:06:16,050
All right.

98
00:06:16,050 --> 00:06:17,030
That's the very basics.

99
00:06:17,070 --> 00:06:20,010
You've now successfully ran your first flask application.

100
00:06:20,010 --> 00:06:21,090
Not so bad.

101
00:06:21,300 --> 00:06:25,920
Later on we're going to see how we can expand off of this to have multiple pages sync up if a database

102
00:06:25,920 --> 00:06:28,210
have interactivity with users and so on.

103
00:06:28,440 --> 00:06:34,740
But with just 10 lines of code and you already have a full application working great We'll see you in

104
00:06:34,740 --> 00:06:35,420
the next lecture.
