Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
00:00:05,000 --> 00:00:08,337
DOUG LLOYD: In a different video, we introduced you to Python and its syntax
1
00:00:08,337 --> 00:00:10,920
and we talked all about how it can be used at the command line
2
00:00:10,920 --> 00:00:14,097
to write programs that you would run from your terminal.
3
00:00:14,097 --> 00:00:15,930
There's a lot more to Python than just that.
4
00:00:15,930 --> 00:00:19,650
We can also use it to write web applications.
5
00:00:19,650 --> 00:00:22,040
It is a really common use case to have website back
6
00:00:22,040 --> 00:00:24,450
ends that are written in Python because Python contains
7
00:00:24,450 --> 00:00:28,920
a lot more native functionality to support things like networking,
8
00:00:28,920 --> 00:00:31,770
opening socket connections, maintaining connections between two
9
00:00:31,770 --> 00:00:34,600
different locations on the internet.
10
00:00:34,600 --> 00:00:39,400
And C can do these things too, but in C, it's really, really difficult
11
00:00:39,400 --> 00:00:41,160
and really a lot of code.
12
00:00:41,160 --> 00:00:44,220
In Python, because it is a more modern language that abstracts away
13
00:00:44,220 --> 00:00:48,450
some of these kind of boring, tedious details of C,
14
00:00:48,450 --> 00:00:50,280
it thus becomes a lot more straightforward
15
00:00:50,280 --> 00:00:54,720
and a lot more tenable to write more complex web apps in Python
16
00:00:54,720 --> 00:00:56,940
instead of using C.
17
00:00:56,940 --> 00:01:00,330
In addition, there are a lot of what are called web frameworks that
18
00:01:00,330 --> 00:01:03,870
make this even easier because they take some of the Python things
19
00:01:03,870 --> 00:01:06,346
that we would have to do and abstract those away.
20
00:01:06,346 --> 00:01:08,220
So basically you can think of a web framework
21
00:01:08,220 --> 00:01:11,160
sort of like a library that's written in Python,
22
00:01:11,160 --> 00:01:13,590
that is specialized in taking advantage and doing
23
00:01:13,590 --> 00:01:17,130
some of the things in Python for you that you would otherwise
24
00:01:17,130 --> 00:01:18,420
have to do yourself.
25
00:01:18,420 --> 00:01:22,200
Creating a web server in Python is not all that many lines of code
26
00:01:22,200 --> 00:01:24,420
to do it in just raw Python.
27
00:01:24,420 --> 00:01:25,590
But it's a little tedious.
28
00:01:25,590 --> 00:01:30,420
Not nearly as tedious as C. But using one of these different web frameworks.
29
00:01:30,420 --> 00:01:34,980
For example, Django, Pyramid, or what we use in CS50, which is Flask,
30
00:01:34,980 --> 00:01:36,420
makes it a lot easier.
31
00:01:36,420 --> 00:01:38,730
Now, those frameworks are all great.
32
00:01:38,730 --> 00:01:40,950
We're talking about Flask in CS50 specifically
33
00:01:40,950 --> 00:01:44,490
because it is very lightweight and still quite feature-rich,
34
00:01:44,490 --> 00:01:47,670
which means that it will work well on CS50 IDE
35
00:01:47,670 --> 00:01:50,440
where you are using a cloud-based infrastructure,
36
00:01:50,440 --> 00:01:55,980
and the amount of space and memory you have is limited by design.
37
00:01:55,980 --> 00:02:00,940
Using the lightest weight web framework possible was really important.
38
00:02:00,940 --> 00:02:03,720
So we've talked about HTML in the past and we know
39
00:02:03,720 --> 00:02:06,330
that we can use HTML to build websites.
40
00:02:06,330 --> 00:02:10,200
But there is one major limitation that HTML websites suffer from
41
00:02:10,200 --> 00:02:12,360
and that is that they are static.
42
00:02:12,360 --> 00:02:14,669
So imagine we want to create a website, for example,
43
00:02:14,669 --> 00:02:19,340
that displays the current time, just the time in Cambridge, Mass,
44
00:02:19,340 --> 00:02:22,110
displaying it to the latest minute, let's say.
45
00:02:22,110 --> 00:02:25,270
This is some HTML that would do exactly that.
46
00:02:25,270 --> 00:02:28,500
So the current time in Cambridge is 14:08.
47
00:02:28,500 --> 00:02:30,330
So 2:08 PM.
48
00:02:30,330 --> 00:02:33,850
This is great for a minute.
49
00:02:33,850 --> 00:02:37,570
But at 2:09, it becomes not accurate anymore.
50
00:02:37,570 --> 00:02:41,370
So if you're the webmaster and you have a page that's written entirely in HTML
51
00:02:41,370 --> 00:02:45,660
and your job is to present an accurate website that
52
00:02:45,660 --> 00:02:48,690
reflects the current time in Cambridge, every minute
53
00:02:48,690 --> 00:02:52,560
you have to go through and update this page so that it is always
54
00:02:52,560 --> 00:02:54,930
consistent and so that whenever a user visits the site,
55
00:02:54,930 --> 00:02:57,600
they're getting the actual correct time.
56
00:02:57,600 --> 00:02:59,410
This is awful.
57
00:02:59,410 --> 00:03:00,660
You would not want to do this.
58
00:03:00,660 --> 00:03:02,460
And if you were actually a webmaster and this was your job,
59
00:03:02,460 --> 00:03:04,794
you probably wouldn't last too long because you'd
60
00:03:04,794 --> 00:03:06,210
be literally just changing things.
61
00:03:06,210 --> 00:03:08,918
And imagine if we were going to the nearest second or the nearest
62
00:03:08,918 --> 00:03:12,240
millisecond, you wouldn't be able to actually even do it.
63
00:03:12,240 --> 00:03:14,460
The only way to update our source in HTML
64
00:03:14,460 --> 00:03:17,550
is to literally go in and edit our HTML.
65
00:03:17,550 --> 00:03:21,930
If we incorporate Python into our code, though, we can be a lot more flexible
66
00:03:21,930 --> 00:03:25,440
and we can allow our pages to become dynamic
67
00:03:25,440 --> 00:03:27,500
without requiring any human intervention.
68
00:03:27,500 --> 00:03:31,620
And that's ultimately our goal here, is to automate these processes for us
69
00:03:31,620 --> 00:03:34,260
so that we don't have to think about them day
70
00:03:34,260 --> 00:03:37,860
to day, getting buried in that minutia.
71
00:03:37,860 --> 00:03:40,200
So we're going to do this specifically in Flask.
72
00:03:40,200 --> 00:03:43,680
And here's an example of a web application in Flask
73
00:03:43,680 --> 00:03:46,830
that would display the current not only time in Cambridge
74
00:03:46,830 --> 00:03:50,280
but it would also display the current date, day of the week, year,
75
00:03:50,280 --> 00:03:54,150
and it would display that time to the nearest ten thousandth of a second.
76
00:03:54,150 --> 00:03:56,862
Just a couple of lines of code from Flask.
77
00:03:56,862 --> 00:03:59,070
Import Flask, we'll talk about that in just a second.
78
00:03:59,070 --> 00:04:01,500
Here, I'm just importing a couple of modules
79
00:04:01,500 --> 00:04:03,350
that are also available in Python.
80
00:04:03,350 --> 00:04:05,550
So Flask is one module available in Python.
81
00:04:05,550 --> 00:04:10,920
Here, I'm importing the datetime module and specifically I'm
82
00:04:10,920 --> 00:04:14,390
pulling one function from it, also called datetime.
83
00:04:14,390 --> 00:04:18,390
And from the pytz or py timezone module, I'm
84
00:04:18,390 --> 00:04:20,820
pulling one function called time zone.
85
00:04:20,820 --> 00:04:23,670
Then I'm initiating my Flask application.
86
00:04:23,670 --> 00:04:28,940
And then I'm basically using the datetime and timezone functions
87
00:04:28,940 --> 00:04:33,080
from those two modules that I imported to figure out what the current time is
88
00:04:33,080 --> 00:04:35,094
in the time zone America/New York.
89
00:04:35,094 --> 00:04:37,010
New York is in the same time zone as Cambridge
90
00:04:37,010 --> 00:04:41,070
and that's the standard time zone name.
91
00:04:41,070 --> 00:04:43,110
So I'm just going to use that one.
92
00:04:43,110 --> 00:04:46,030
So I'm figuring out exactly what the current date and time is
93
00:04:46,030 --> 00:04:49,220
and assigning that to a variable in Python called now.
94
00:04:49,220 --> 00:04:54,170
And then I'm returning some string where I'm plugging in whatever now is.
95
00:04:54,170 --> 00:04:56,750
And apparently I'm doing that to app.route/.
96
00:04:56,750 --> 00:04:58,400
We'll talk about all that in a second.
97
00:04:58,400 --> 00:05:04,370
But basically I wrote this on a web page in Python in something
98
00:05:04,370 --> 00:05:06,470
called application.py, specifically.
99
00:05:06,470 --> 00:05:09,572
And then I ran this application and visited
100
00:05:09,572 --> 00:05:11,780
the page that was generated from it, it would tell me
101
00:05:11,780 --> 00:05:14,814
the current date and time to the nearest ten thousandth of a second.
102
00:05:14,814 --> 00:05:16,730
If I refreshed, it would update it every time.
103
00:05:16,730 --> 00:05:18,690
It wouldn't be static, it would become dynamic
104
00:05:18,690 --> 00:05:20,690
and I wouldn't have to do any more intervention.
105
00:05:20,690 --> 00:05:23,240
This would work for as long as my computer worked,
106
00:05:23,240 --> 00:05:25,370
as long as the web server was running worked
107
00:05:25,370 --> 00:05:26,990
to give me the current date and time.
108
00:05:26,990 --> 00:05:31,220
And I, as the webmaster, can focus on much more fulfilling and fun things
109
00:05:31,220 --> 00:05:36,150
than updating my HTML source every 60 seconds.
110
00:05:36,150 --> 00:05:39,520
So getting started using Flask is really easy within CS50 IDE.
111
00:05:39,520 --> 00:05:42,290
We first open up a Python file and in general, we're
112
00:05:42,290 --> 00:05:44,004
going to call it application.py.
113
00:05:44,004 --> 00:05:46,670
But you can call it whatever you want but in all these examples,
114
00:05:46,670 --> 00:05:49,640
we're going to be referring to it as application.py.
115
00:05:49,640 --> 00:05:52,760
The very first line of application.py is going to be the following.
116
00:05:52,760 --> 00:05:55,580
From Flask, import capital F, Flask.
117
00:05:55,580 --> 00:05:59,060
So flask with a lowercase f is the name of a module.
118
00:05:59,060 --> 00:06:03,084
Capital F Flask is going to be the name of a function within that module.
119
00:06:03,084 --> 00:06:04,750
In particular, it's going to be a class.
120
00:06:04,750 --> 00:06:07,910
Generally, you may recall from our video on Python syntax,
121
00:06:07,910 --> 00:06:10,700
that we indicate classes with a capital letter.
122
00:06:10,700 --> 00:06:14,420
That's what we're doing here, we're importing the Flask class.
123
00:06:14,420 --> 00:06:17,780
After we import it, we then need to initialize a Flask application.
124
00:06:17,780 --> 00:06:21,150
You saw that on the previous code example as well.
125
00:06:21,150 --> 00:06:25,257
App equals flask parentheses underscore underscore name underscore underscore.
126
00:06:25,257 --> 00:06:27,590
Underscore underscore name underscore underscore is just
127
00:06:27,590 --> 00:06:29,340
the name of the file.
128
00:06:29,340 --> 00:06:31,700
So basically this is creating a flask application
129
00:06:31,700 --> 00:06:35,450
based on whatever file this line of code appears in.
130
00:06:35,450 --> 00:06:38,060
Which, again, is always going to be generally application.py.
131
00:06:38,060 --> 00:06:41,090
So it's basically creating an app, a Flask application
132
00:06:41,090 --> 00:06:44,830
from the application.py file.
133
00:06:44,830 --> 00:06:46,770
And then from there, we just have to write
134
00:06:46,770 --> 00:06:49,200
functions, which we're familiar with doing in Python,
135
00:06:49,200 --> 00:06:51,250
to define the behavior of our application
136
00:06:51,250 --> 00:06:52,780
when we do different things.
137
00:06:52,780 --> 00:06:56,310
So here, for example, are two functions that I might write.
138
00:06:56,310 --> 00:06:59,020
And you'll notice that this is not new.
139
00:06:59,020 --> 00:07:00,450
This is just writing in Python.
140
00:07:00,450 --> 00:07:02,910
We're not writing in anything special here.
141
00:07:02,910 --> 00:07:05,250
I'm defining a function called index, whose sole purpose
142
00:07:05,250 --> 00:07:08,040
is to return you are at the index page.
143
00:07:08,040 --> 00:07:10,830
And I'm defining another function called sample,
144
00:07:10,830 --> 00:07:14,580
whose sole purpose is to return the string you are on the sample page.
145
00:07:14,580 --> 00:07:18,375
Now, how do I associate these functions with my site?
146
00:07:18,375 --> 00:07:20,520
Well, what I can do is something like this.
147
00:07:20,520 --> 00:07:24,380
This is called applying a decorator, and I'll talk about this in just a second.
148
00:07:24,380 --> 00:07:28,680
But basically what it means here is if I go to my web applications home
149
00:07:28,680 --> 00:07:32,157
page, which you may recall from our video on HTTP, is just slash.
150
00:07:32,157 --> 00:07:33,990
So if I don't type anything else, it's going
151
00:07:33,990 --> 00:07:37,180
to default to index.HTML or nothing.
152
00:07:37,180 --> 00:07:39,720
It will show me you are at the index page.
153
00:07:39,720 --> 00:07:45,137
If, however, I go to whatever my web applications address is slash sample,
154
00:07:45,137 --> 00:07:47,970
it will not show me the index page, it will show me the sample page.
155
00:07:47,970 --> 00:07:50,730
So basically what I'm doing here is I'm defining the behavior
156
00:07:50,730 --> 00:07:56,040
that I want to appear on different pages within my flask application.
157
00:07:56,040 --> 00:07:58,130
So again, these are called decorators.
158
00:07:58,130 --> 00:08:02,610
And in Flask context, we're basically just associating a particular function
159
00:08:02,610 --> 00:08:06,495
with executing when you visit a particular URL.
160
00:08:06,495 --> 00:08:09,120
Decorators are actually not something that are native to Flask,
161
00:08:09,120 --> 00:08:10,500
they're native to Python more generally.
162
00:08:10,500 --> 00:08:13,080
But that goes beyond the scope of what we cover in the class.
163
00:08:13,080 --> 00:08:16,320
But generally they are used to modify the behavior of a function
164
00:08:16,320 --> 00:08:19,020
or associate a function with something.
165
00:08:19,020 --> 00:08:22,080
But in Flask, we're going to use it specifically to associate functions
166
00:08:22,080 --> 00:08:24,430
with visiting certain domains.
167
00:08:24,430 --> 00:08:26,700
OK?
168
00:08:26,700 --> 00:08:27,780
We also then need to--
169
00:08:27,780 --> 00:08:31,020
So we've now written some code, the index and sample functions.
170
00:08:31,020 --> 00:08:34,470
Now we want to run our Flask application.
171
00:08:34,470 --> 00:08:39,210
We do that in three steps, the first two of which you only have to run once.
172
00:08:39,210 --> 00:08:41,700
First thing you have to do is export the Flask app.
173
00:08:41,700 --> 00:08:44,789
Flask app is a system variable and it's going
174
00:08:44,789 --> 00:08:49,320
to become stored in the memory of your IDE specifically,
175
00:08:49,320 --> 00:08:51,870
so that if you ever run an application again,
176
00:08:51,870 --> 00:08:54,480
it knows exactly which application to run.
177
00:08:54,480 --> 00:08:58,800
So we're basically just saving in memory somewhere the location
178
00:08:58,800 --> 00:09:00,060
of our Flask application.
179
00:09:00,060 --> 00:09:02,610
It's being stored in a system variable.
180
00:09:02,610 --> 00:09:04,900
So export flask app equals application.py,
181
00:09:04,900 --> 00:09:06,400
because that's the name of our file.
182
00:09:06,400 --> 00:09:09,690
The second line is technically optional but it's recommended particularly
183
00:09:09,690 --> 00:09:10,680
as you get started.
184
00:09:10,680 --> 00:09:12,580
Export flask debug equals 1.
185
00:09:12,580 --> 00:09:14,360
1 here just basically means true.
186
00:09:14,360 --> 00:09:16,110
So basically you're going to be running it
187
00:09:16,110 --> 00:09:19,620
so that when you run your flask application in your IDE,
188
00:09:19,620 --> 00:09:23,205
you are seeing all of the things that it's doing in debug mode.
189
00:09:23,205 --> 00:09:26,080
So if something goes wrong, it will get printed out to your terminal,
190
00:09:26,080 --> 00:09:26,640
you'll see it.
191
00:09:26,640 --> 00:09:29,400
It's not like your site will just crash and you won't know what happened.
192
00:09:29,400 --> 00:09:32,233
And then after executing those two lines of code, again, in our IDE,
193
00:09:32,233 --> 00:09:35,880
just executing those two commands, rather, in our IDE, flask run.
194
00:09:35,880 --> 00:09:38,070
And then what will happen specifically in your IDE,
195
00:09:38,070 --> 00:09:40,655
is it will take about a second to spin up
196
00:09:40,655 --> 00:09:42,780
and then it'll give you a URL that you can click on
197
00:09:42,780 --> 00:09:46,230
and visit, which will be the home page of your application.
198
00:09:46,230 --> 00:09:48,521
Again, after the first time you get configured
199
00:09:48,521 --> 00:09:50,520
with the particular application you want to run,
200
00:09:50,520 --> 00:09:53,270
you can leave these two blue lines out, you won't need them again.
201
00:09:53,270 --> 00:09:56,070
You can just continue to type flask run and it
202
00:09:56,070 --> 00:09:58,560
will continue to run that same application over and over
203
00:09:58,560 --> 00:10:00,090
until you no longer want to run it.
204
00:10:00,090 --> 00:10:03,214
And then you would export a different application, export a different debug
205
00:10:03,214 --> 00:10:05,530
mode, and so on.
206
00:10:05,530 --> 00:10:09,210
So in addition to just running code as we
207
00:10:09,210 --> 00:10:12,669
did before, just having it print out you are at the index
208
00:10:12,669 --> 00:10:14,460
and you are at the sample page, we can also
209
00:10:14,460 --> 00:10:17,730
pass data in to our Flask applications.
210
00:10:17,730 --> 00:10:22,077
We can do this using HTTP get, you may recall, by passing in data via URL.
211
00:10:22,077 --> 00:10:24,910
So I might write a function that looks a little something like this.
212
00:10:24,910 --> 00:10:28,320
It's a function called show and instead of taking
213
00:10:28,320 --> 00:10:31,290
no parameters like index and sample did earlier,
214
00:10:31,290 --> 00:10:34,200
I'm accepting a parameter here called number.
215
00:10:34,200 --> 00:10:37,560
And I'm returning you passed in, and then I'm
216
00:10:37,560 --> 00:10:41,730
apparently going interpolate or stick in some number.
217
00:10:41,730 --> 00:10:43,240
Where does that number come from?
218
00:10:43,240 --> 00:10:44,700
Well, look at my decorator here.
219
00:10:44,700 --> 00:10:48,600
@app.route slash show slash.
220
00:10:48,600 --> 00:10:51,450
And those angle brackets are actually important,
221
00:10:51,450 --> 00:10:54,450
we actually have to leave those in, number.
222
00:10:54,450 --> 00:10:59,670
So that if I visited my application slash show slash 10,
223
00:10:59,670 --> 00:11:01,950
the page would show you passed in 10.
224
00:11:01,950 --> 00:11:05,730
If I went to slash show slash 50, it would say you passed in 50.
225
00:11:05,730 --> 00:11:10,170
So it allows me to collect information using HTTP get
226
00:11:10,170 --> 00:11:14,150
by having the user supply it via a URL.
227
00:11:14,150 --> 00:11:18,680
We can also pass in data via HTML forms, which are transmitted not by get
228
00:11:18,680 --> 00:11:20,540
but by something called post.
229
00:11:20,540 --> 00:11:23,090
Now, by default, Flask is configured to only accept
230
00:11:23,090 --> 00:11:26,180
HTTP get requests or information via the URL
231
00:11:26,180 --> 00:11:29,720
so we have to modify a couple of things to make sure that Flask will also
232
00:11:29,720 --> 00:11:32,720
respond to HTTP post requests, for example,
233
00:11:32,720 --> 00:11:35,750
when data is submitted via a form.
234
00:11:35,750 --> 00:11:41,242
So if you get to problem set 7 in CS50, which is CS50 finance,
235
00:11:41,242 --> 00:11:42,950
you'll see that in our distribution code,
236
00:11:42,950 --> 00:11:45,050
we have something that looks a little similar to this.
237
00:11:45,050 --> 00:11:46,920
So this is where this is coming from if you want to take a look.
238
00:11:46,920 --> 00:11:49,460
I've taken out a couple of things just to fit this four
239
00:11:49,460 --> 00:11:52,160
lines of code on the slide here.
240
00:11:52,160 --> 00:11:54,380
But this is some of the sample code that we give you
241
00:11:54,380 --> 00:11:58,970
that is checking to see whether the user has provided
242
00:11:58,970 --> 00:12:02,190
a user name in a particular field.
243
00:12:02,190 --> 00:12:06,847
So notice that in addition in my decorator to saying where
244
00:12:06,847 --> 00:12:09,680
the function should be associated, so in this case with slash login,
245
00:12:09,680 --> 00:12:11,730
I'm also saying not only that, but you should
246
00:12:11,730 --> 00:12:15,110
be accepting data via the get method or via the post method.
247
00:12:15,110 --> 00:12:17,200
So that's how I explicitly call it out.
248
00:12:17,200 --> 00:12:19,190
Then I'm defining my login function.
249
00:12:19,190 --> 00:12:25,730
And I'm saying if not request.form.get username.
250
00:12:25,730 --> 00:12:30,860
Using get here is unfortunately a side effect of Python.
251
00:12:30,860 --> 00:12:33,590
That does not mean we're using the get method here.
252
00:12:33,590 --> 00:12:36,690
Basically what I'm asking for is go and retrieve,
253
00:12:36,690 --> 00:12:38,500
literally using get in that context.
254
00:12:38,500 --> 00:12:43,670
Go and retrieve from the form the field called username.
255
00:12:43,670 --> 00:12:47,107
And here I'm saying if it doesn't exist, apologize to the user.
256
00:12:47,107 --> 00:12:48,440
Say they need supply a username.
257
00:12:48,440 --> 00:12:51,050
So basically this is asking the question is the user name
258
00:12:51,050 --> 00:12:57,217
field of whatever form the user submitted to get here, is that blank?
259
00:12:57,217 --> 00:13:00,300
If it is, we need to apologize to the user and have them return something.
260
00:13:00,300 --> 00:13:02,800
If not, we'll go to some more lines of code down below
261
00:13:02,800 --> 00:13:04,591
and you're going to see those lines of code
262
00:13:04,591 --> 00:13:08,310
in the distribution code for PSet 7, if you want to take a look.
263
00:13:08,310 --> 00:13:10,440
Now, we can also vary the behavior of a function
264
00:13:10,440 --> 00:13:13,590
depending on which type of HTTP request we got.
265
00:13:13,590 --> 00:13:15,990
We can do one thing if we got a get request
266
00:13:15,990 --> 00:13:18,060
and we can do one thing if we got a post request.
267
00:13:18,060 --> 00:13:22,354
For example, if we got a get request, we might show them the form.
268
00:13:22,354 --> 00:13:24,270
But if we got a post request, then we're going
269
00:13:24,270 --> 00:13:27,930
to be expecting that they have provided some data via the forms.
270
00:13:27,930 --> 00:13:30,780
We can use one URL to do two different things
271
00:13:30,780 --> 00:13:32,347
depending on how the data came to us.
272
00:13:32,347 --> 00:13:34,680
And that actually is what we do, that some of the code I
273
00:13:34,680 --> 00:13:36,750
took out on the previous page.
274
00:13:36,750 --> 00:13:41,340
But this code also appears in the login function for PSet 7.
275
00:13:41,340 --> 00:13:43,740
If the request method was post, that was then
276
00:13:43,740 --> 00:13:46,710
where I was checking to see whether the fields were all filled in.
277
00:13:46,710 --> 00:13:49,374
Otherwise, what I actually do here, which an else here
278
00:13:49,374 --> 00:13:51,540
implies if the request method is get, because I only
279
00:13:51,540 --> 00:13:54,810
have two methods this function accepts, get and post.
280
00:13:54,810 --> 00:13:56,910
Otherwise, what I'm doing is actually displaying
281
00:13:56,910 --> 00:14:00,106
the form in the first place, the form that I expect them to type into.
282
00:14:00,106 --> 00:14:01,980
Which submits to this page again in the hopes
283
00:14:01,980 --> 00:14:04,530
that they will have submitted data that I could use.
284
00:14:04,530 --> 00:14:07,950
And I would process in the post part of this function as opposed
285
00:14:07,950 --> 00:14:11,100
to in the get part of this function.
286
00:14:11,100 --> 00:14:16,250
So if request.method basically is allowing me to check how I got data.
287
00:14:16,250 --> 00:14:18,530
Request is something that is part of Flask
288
00:14:18,530 --> 00:14:21,350
and it will let me see whether I got data via get or via post
289
00:14:21,350 --> 00:14:25,040
and allow me to do different things depending on which it was.
290
00:14:25,040 --> 00:14:27,350
There are a lot of functions within the Flask module
291
00:14:27,350 --> 00:14:30,210
that will be useful for you as you continue to develop applications.
292
00:14:30,210 --> 00:14:32,300
So in addition to saying from Flask import
293
00:14:32,300 --> 00:14:34,310
Flask at the beginning of your files, you
294
00:14:34,310 --> 00:14:38,290
might say from Flask import Flask comma any of these.
295
00:14:38,290 --> 00:14:40,970
And you'll leave out the parentheses when you're doing that part
296
00:14:40,970 --> 00:14:43,678
but you'll use the parentheses when you're calling the functions.
297
00:14:43,678 --> 00:14:45,680
URL underscore for.
298
00:14:45,680 --> 00:14:47,780
So this allows me to define a function and have
299
00:14:47,780 --> 00:14:49,190
a decorator associated with it.
300
00:14:49,190 --> 00:14:51,530
And maybe that decorator specifies that the URL here
301
00:14:51,530 --> 00:14:56,170
is really long or, for whatever reason, not that helpful.
302
00:14:56,170 --> 00:14:59,660
What I can do here is instead of specifying
303
00:14:59,660 --> 00:15:03,050
the exact URL I want to go to, I can say what
304
00:15:03,050 --> 00:15:06,770
I want you to do here is go to the URL for whatever function it is.
305
00:15:06,770 --> 00:15:09,230
So say my login function was associated with some URL that
306
00:15:09,230 --> 00:15:10,759
was like 50 characters long.
307
00:15:10,759 --> 00:15:12,800
Here, I could just pass in, instead of typing out
308
00:15:12,800 --> 00:15:15,530
that 50 character long URL where I wanted to use it,
309
00:15:15,530 --> 00:15:18,620
I could just say URL underscore for login.
310
00:15:18,620 --> 00:15:20,780
And because I associate it with a decorator,
311
00:15:20,780 --> 00:15:22,280
it'll just plug it in there instead.
312
00:15:22,280 --> 00:15:25,780
So it's a way to kind of shorthand a couple of things.
313
00:15:25,780 --> 00:15:28,900
Redirect, as you might expect, is Flask's function
314
00:15:28,900 --> 00:15:31,900
that redirects you from one page to another.
315
00:15:31,900 --> 00:15:34,722
Session is useful for HTTP session data.
316
00:15:34,722 --> 00:15:37,930
As we've talked about get and post, you may recall that there's also a third.
317
00:15:37,930 --> 00:15:40,540
And there are a few others but there's another thing
318
00:15:40,540 --> 00:15:42,460
we would use called a session variable.
319
00:15:42,460 --> 00:15:45,501
It's not necessarily an HTTP thing but it's usually stored in the headers
320
00:15:45,501 --> 00:15:46,640
there as well.
321
00:15:46,640 --> 00:15:49,690
And we can use that, for example, to track that a user is logged in.
322
00:15:49,690 --> 00:15:51,523
And constantly check that they are logged in
323
00:15:51,523 --> 00:15:54,310
even if they're going to different pages on our site.
324
00:15:54,310 --> 00:15:56,950
So a session is used sort of like a global variable.
325
00:15:56,950 --> 00:15:59,020
It's accessible by all pages.
326
00:15:59,020 --> 00:16:02,200
If you establish a session, you can store data in the session for the user
327
00:16:02,200 --> 00:16:03,560
as well.
328
00:16:03,560 --> 00:16:08,470
Render template is used to create pages on your site that
329
00:16:08,470 --> 00:16:12,690
mix HTML and Python together.
330
00:16:12,690 --> 00:16:14,440
So I might render a template, for example,
331
00:16:14,440 --> 00:16:16,270
where that template is the form that I want
332
00:16:16,270 --> 00:16:19,300
to show to the user, the form I was talking about on an earlier slide.
333
00:16:19,300 --> 00:16:23,920
I might have an HTML template where I've written the general form there.
334
00:16:23,920 --> 00:16:27,580
And I might render a template and reference in the parentheses
335
00:16:27,580 --> 00:16:31,422
there the HTML for the form that I was just talking about.
336
00:16:31,422 --> 00:16:33,130
And you can do some other cool things too
337
00:16:33,130 --> 00:16:35,976
where you mix HTML and Python together using
338
00:16:35,976 --> 00:16:37,850
a language called Jinja, which is going to be
339
00:16:37,850 --> 00:16:39,640
beyond the scope of this particular video.
340
00:16:39,640 --> 00:16:41,080
But if you want to get more information about what
341
00:16:41,080 --> 00:16:44,329
you can do in the Flask quickstart guide and you really can get started pretty
342
00:16:44,329 --> 00:16:45,907
quickly with it, the URL is there.
343
00:16:45,907 --> 00:16:47,740
And if you want to learn about Jinja and how
344
00:16:47,740 --> 00:16:53,332
you can mix HTML and Python together to create basic templates where
345
00:16:53,332 --> 00:16:55,540
you can maybe substitute a couple of different things
346
00:16:55,540 --> 00:16:59,050
in depending on what the user has submitted via a get or post request.
347
00:16:59,050 --> 00:17:00,940
You can get some information on Jinja there.
348
00:17:00,940 --> 00:17:03,590
Jinja is Python-inspired syntax.
349
00:17:03,590 --> 00:17:06,490
So some of the things that you'll be familiar with from Python syntax
350
00:17:06,490 --> 00:17:07,780
will be relevant there.
351
00:17:07,780 --> 00:17:10,510
But there are a couple of little twists as well.
352
00:17:10,510 --> 00:17:14,040
And you can learn more about Jinja at that URL there as well.
353
00:17:14,040 --> 00:17:15,210
I'm Doug Lloyd.
354
00:17:15,210 --> 00:17:17,010
This is CS50.
355
00:17:17,010 --> 00:17:18,820
30420
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.