WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000 00:00:00.000 --> 00:00:03.337 DOUG LLOYD: In a different video, we introduced you to Python and its syntax 00:00:03.337 --> 00:00:05.920 and we talked all about how it can be used at the command line 00:00:05.920 --> 00:00:09.097 to write programs that you would run from your terminal. 00:00:09.097 --> 00:00:10.930 There's a lot more to Python than just that. 00:00:10.930 --> 00:00:14.650 We can also use it to write web applications. 00:00:14.650 --> 00:00:17.040 It is a really common use case to have website back 00:00:17.040 --> 00:00:19.450 ends that are written in Python because Python contains 00:00:19.450 --> 00:00:23.920 a lot more native functionality to support things like networking, 00:00:23.920 --> 00:00:26.770 opening socket connections, maintaining connections between two 00:00:26.770 --> 00:00:29.600 different locations on the internet. 00:00:29.600 --> 00:00:34.400 And C can do these things too, but in C, it's really, really difficult 00:00:34.400 --> 00:00:36.160 and really a lot of code. 00:00:36.160 --> 00:00:39.220 In Python, because it is a more modern language that abstracts away 00:00:39.220 --> 00:00:43.450 some of these kind of boring, tedious details of C, 00:00:43.450 --> 00:00:45.280 it thus becomes a lot more straightforward 00:00:45.280 --> 00:00:49.720 and a lot more tenable to write more complex web apps in Python 00:00:49.720 --> 00:00:51.940 instead of using C. 00:00:51.940 --> 00:00:55.330 In addition, there are a lot of what are called web frameworks that 00:00:55.330 --> 00:00:58.870 make this even easier because they take some of the Python things 00:00:58.870 --> 00:01:01.346 that we would have to do and abstract those away. 00:01:01.346 --> 00:01:03.220 So basically you can think of a web framework 00:01:03.220 --> 00:01:06.160 sort of like a library that's written in Python, 00:01:06.160 --> 00:01:08.590 that is specialized in taking advantage and doing 00:01:08.590 --> 00:01:12.130 some of the things in Python for you that you would otherwise 00:01:12.130 --> 00:01:13.420 have to do yourself. 00:01:13.420 --> 00:01:17.200 Creating a web server in Python is not all that many lines of code 00:01:17.200 --> 00:01:19.420 to do it in just raw Python. 00:01:19.420 --> 00:01:20.590 But it's a little tedious. 00:01:20.590 --> 00:01:25.420 Not nearly as tedious as C. But using one of these different web frameworks. 00:01:25.420 --> 00:01:29.980 For example, Django, Pyramid, or what we use in CS50, which is Flask, 00:01:29.980 --> 00:01:31.420 makes it a lot easier. 00:01:31.420 --> 00:01:33.730 Now, those frameworks are all great. 00:01:33.730 --> 00:01:35.950 We're talking about Flask in CS50 specifically 00:01:35.950 --> 00:01:39.490 because it is very lightweight and still quite feature-rich, 00:01:39.490 --> 00:01:42.670 which means that it will work well on CS50 IDE 00:01:42.670 --> 00:01:45.440 where you are using a cloud-based infrastructure, 00:01:45.440 --> 00:01:50.980 and the amount of space and memory you have is limited by design. 00:01:50.980 --> 00:01:55.940 Using the lightest weight web framework possible was really important. 00:01:55.940 --> 00:01:58.720 So we've talked about HTML in the past and we know 00:01:58.720 --> 00:02:01.330 that we can use HTML to build websites. 00:02:01.330 --> 00:02:05.200 But there is one major limitation that HTML websites suffer from 00:02:05.200 --> 00:02:07.360 and that is that they are static. 00:02:07.360 --> 00:02:09.669 So imagine we want to create a website, for example, 00:02:09.669 --> 00:02:14.340 that displays the current time, just the time in Cambridge, Mass, 00:02:14.340 --> 00:02:17.110 displaying it to the latest minute, let's say. 00:02:17.110 --> 00:02:20.270 This is some HTML that would do exactly that. 00:02:20.270 --> 00:02:23.500 So the current time in Cambridge is 14:08. 00:02:23.500 --> 00:02:25.330 So 2:08 PM. 00:02:25.330 --> 00:02:28.850 This is great for a minute. 00:02:28.850 --> 00:02:32.570 But at 2:09, it becomes not accurate anymore. 00:02:32.570 --> 00:02:36.370 So if you're the webmaster and you have a page that's written entirely in HTML 00:02:36.370 --> 00:02:40.660 and your job is to present an accurate website that 00:02:40.660 --> 00:02:43.690 reflects the current time in Cambridge, every minute 00:02:43.690 --> 00:02:47.560 you have to go through and update this page so that it is always 00:02:47.560 --> 00:02:49.930 consistent and so that whenever a user visits the site, 00:02:49.930 --> 00:02:52.600 they're getting the actual correct time. 00:02:52.600 --> 00:02:54.410 This is awful. 00:02:54.410 --> 00:02:55.660 You would not want to do this. 00:02:55.660 --> 00:02:57.460 And if you were actually a webmaster and this was your job, 00:02:57.460 --> 00:02:59.794 you probably wouldn't last too long because you'd 00:02:59.794 --> 00:03:01.210 be literally just changing things. 00:03:01.210 --> 00:03:03.918 And imagine if we were going to the nearest second or the nearest 00:03:03.918 --> 00:03:07.240 millisecond, you wouldn't be able to actually even do it. 00:03:07.240 --> 00:03:09.460 The only way to update our source in HTML 00:03:09.460 --> 00:03:12.550 is to literally go in and edit our HTML. 00:03:12.550 --> 00:03:16.930 If we incorporate Python into our code, though, we can be a lot more flexible 00:03:16.930 --> 00:03:20.440 and we can allow our pages to become dynamic 00:03:20.440 --> 00:03:22.500 without requiring any human intervention. 00:03:22.500 --> 00:03:26.620 And that's ultimately our goal here, is to automate these processes for us 00:03:26.620 --> 00:03:29.260 so that we don't have to think about them day 00:03:29.260 --> 00:03:32.860 to day, getting buried in that minutia. 00:03:32.860 --> 00:03:35.200 So we're going to do this specifically in Flask. 00:03:35.200 --> 00:03:38.680 And here's an example of a web application in Flask 00:03:38.680 --> 00:03:41.830 that would display the current not only time in Cambridge 00:03:41.830 --> 00:03:45.280 but it would also display the current date, day of the week, year, 00:03:45.280 --> 00:03:49.150 and it would display that time to the nearest ten thousandth of a second. 00:03:49.150 --> 00:03:51.862 Just a couple of lines of code from Flask. 00:03:51.862 --> 00:03:54.070 Import Flask, we'll talk about that in just a second. 00:03:54.070 --> 00:03:56.500 Here, I'm just importing a couple of modules 00:03:56.500 --> 00:03:58.350 that are also available in Python. 00:03:58.350 --> 00:04:00.550 So Flask is one module available in Python. 00:04:00.550 --> 00:04:05.920 Here, I'm importing the datetime module and specifically I'm 00:04:05.920 --> 00:04:09.390 pulling one function from it, also called datetime. 00:04:09.390 --> 00:04:13.390 And from the pytz or py timezone module, I'm 00:04:13.390 --> 00:04:15.820 pulling one function called time zone. 00:04:15.820 --> 00:04:18.670 Then I'm initiating my Flask application. 00:04:18.670 --> 00:04:23.940 And then I'm basically using the datetime and timezone functions 00:04:23.940 --> 00:04:28.080 from those two modules that I imported to figure out what the current time is 00:04:28.080 --> 00:04:30.094 in the time zone America/New York. 00:04:30.094 --> 00:04:32.010 New York is in the same time zone as Cambridge 00:04:32.010 --> 00:04:36.070 and that's the standard time zone name. 00:04:36.070 --> 00:04:38.110 So I'm just going to use that one. 00:04:38.110 --> 00:04:41.030 So I'm figuring out exactly what the current date and time is 00:04:41.030 --> 00:04:44.220 and assigning that to a variable in Python called now. 00:04:44.220 --> 00:04:49.170 And then I'm returning some string where I'm plugging in whatever now is. 00:04:49.170 --> 00:04:51.750 And apparently I'm doing that to app.route/. 00:04:51.750 --> 00:04:53.400 We'll talk about all that in a second. 00:04:53.400 --> 00:04:59.370 But basically I wrote this on a web page in Python in something 00:04:59.370 --> 00:05:01.470 called application.py, specifically. 00:05:01.470 --> 00:05:04.572 And then I ran this application and visited 00:05:04.572 --> 00:05:06.780 the page that was generated from it, it would tell me 00:05:06.780 --> 00:05:09.814 the current date and time to the nearest ten thousandth of a second. 00:05:09.814 --> 00:05:11.730 If I refreshed, it would update it every time. 00:05:11.730 --> 00:05:13.690 It wouldn't be static, it would become dynamic 00:05:13.690 --> 00:05:15.690 and I wouldn't have to do any more intervention. 00:05:15.690 --> 00:05:18.240 This would work for as long as my computer worked, 00:05:18.240 --> 00:05:20.370 as long as the web server was running worked 00:05:20.370 --> 00:05:21.990 to give me the current date and time. 00:05:21.990 --> 00:05:26.220 And I, as the webmaster, can focus on much more fulfilling and fun things 00:05:26.220 --> 00:05:31.150 than updating my HTML source every 60 seconds. 00:05:31.150 --> 00:05:34.520 So getting started using Flask is really easy within CS50 IDE. 00:05:34.520 --> 00:05:37.290 We first open up a Python file and in general, we're 00:05:37.290 --> 00:05:39.004 going to call it application.py. 00:05:39.004 --> 00:05:41.670 But you can call it whatever you want but in all these examples, 00:05:41.670 --> 00:05:44.640 we're going to be referring to it as application.py. 00:05:44.640 --> 00:05:47.760 The very first line of application.py is going to be the following. 00:05:47.760 --> 00:05:50.580 From Flask, import capital F, Flask. 00:05:50.580 --> 00:05:54.060 So flask with a lowercase f is the name of a module. 00:05:54.060 --> 00:05:58.084 Capital F Flask is going to be the name of a function within that module. 00:05:58.084 --> 00:05:59.750 In particular, it's going to be a class. 00:05:59.750 --> 00:06:02.910 Generally, you may recall from our video on Python syntax, 00:06:02.910 --> 00:06:05.700 that we indicate classes with a capital letter. 00:06:05.700 --> 00:06:09.420 That's what we're doing here, we're importing the Flask class. 00:06:09.420 --> 00:06:12.780 After we import it, we then need to initialize a Flask application. 00:06:12.780 --> 00:06:16.150 You saw that on the previous code example as well. 00:06:16.150 --> 00:06:20.257 App equals flask parentheses underscore underscore name underscore underscore. 00:06:20.257 --> 00:06:22.590 Underscore underscore name underscore underscore is just 00:06:22.590 --> 00:06:24.340 the name of the file. 00:06:24.340 --> 00:06:26.700 So basically this is creating a flask application 00:06:26.700 --> 00:06:30.450 based on whatever file this line of code appears in. 00:06:30.450 --> 00:06:33.060 Which, again, is always going to be generally application.py. 00:06:33.060 --> 00:06:36.090 So it's basically creating an app, a Flask application 00:06:36.090 --> 00:06:39.830 from the application.py file. 00:06:39.830 --> 00:06:41.770 And then from there, we just have to write 00:06:41.770 --> 00:06:44.200 functions, which we're familiar with doing in Python, 00:06:44.200 --> 00:06:46.250 to define the behavior of our application 00:06:46.250 --> 00:06:47.780 when we do different things. 00:06:47.780 --> 00:06:51.310 So here, for example, are two functions that I might write. 00:06:51.310 --> 00:06:54.020 And you'll notice that this is not new. 00:06:54.020 --> 00:06:55.450 This is just writing in Python. 00:06:55.450 --> 00:06:57.910 We're not writing in anything special here. 00:06:57.910 --> 00:07:00.250 I'm defining a function called index, whose sole purpose 00:07:00.250 --> 00:07:03.040 is to return you are at the index page. 00:07:03.040 --> 00:07:05.830 And I'm defining another function called sample, 00:07:05.830 --> 00:07:09.580 whose sole purpose is to return the string you are on the sample page. 00:07:09.580 --> 00:07:13.375 Now, how do I associate these functions with my site? 00:07:13.375 --> 00:07:15.520 Well, what I can do is something like this. 00:07:15.520 --> 00:07:19.380 This is called applying a decorator, and I'll talk about this in just a second. 00:07:19.380 --> 00:07:23.680 But basically what it means here is if I go to my web applications home 00:07:23.680 --> 00:07:27.157 page, which you may recall from our video on HTTP, is just slash. 00:07:27.157 --> 00:07:28.990 So if I don't type anything else, it's going 00:07:28.990 --> 00:07:32.180 to default to index.HTML or nothing. 00:07:32.180 --> 00:07:34.720 It will show me you are at the index page. 00:07:34.720 --> 00:07:40.137 If, however, I go to whatever my web applications address is slash sample, 00:07:40.137 --> 00:07:42.970 it will not show me the index page, it will show me the sample page. 00:07:42.970 --> 00:07:45.730 So basically what I'm doing here is I'm defining the behavior 00:07:45.730 --> 00:07:51.040 that I want to appear on different pages within my flask application. 00:07:51.040 --> 00:07:53.130 So again, these are called decorators. 00:07:53.130 --> 00:07:57.610 And in Flask context, we're basically just associating a particular function 00:07:57.610 --> 00:08:01.495 with executing when you visit a particular URL. 00:08:01.495 --> 00:08:04.120 Decorators are actually not something that are native to Flask, 00:08:04.120 --> 00:08:05.500 they're native to Python more generally. 00:08:05.500 --> 00:08:08.080 But that goes beyond the scope of what we cover in the class. 00:08:08.080 --> 00:08:11.320 But generally they are used to modify the behavior of a function 00:08:11.320 --> 00:08:14.020 or associate a function with something. 00:08:14.020 --> 00:08:17.080 But in Flask, we're going to use it specifically to associate functions 00:08:17.080 --> 00:08:19.430 with visiting certain domains. 00:08:19.430 --> 00:08:21.700 OK? 00:08:21.700 --> 00:08:22.780 We also then need to-- 00:08:22.780 --> 00:08:26.020 So we've now written some code, the index and sample functions. 00:08:26.020 --> 00:08:29.470 Now we want to run our Flask application. 00:08:29.470 --> 00:08:34.210 We do that in three steps, the first two of which you only have to run once. 00:08:34.210 --> 00:08:36.700 First thing you have to do is export the Flask app. 00:08:36.700 --> 00:08:39.789 Flask app is a system variable and it's going 00:08:39.789 --> 00:08:44.320 to become stored in the memory of your IDE specifically, 00:08:44.320 --> 00:08:46.870 so that if you ever run an application again, 00:08:46.870 --> 00:08:49.480 it knows exactly which application to run. 00:08:49.480 --> 00:08:53.800 So we're basically just saving in memory somewhere the location 00:08:53.800 --> 00:08:55.060 of our Flask application. 00:08:55.060 --> 00:08:57.610 It's being stored in a system variable. 00:08:57.610 --> 00:08:59.900 So export flask app equals application.py, 00:08:59.900 --> 00:09:01.400 because that's the name of our file. 00:09:01.400 --> 00:09:04.690 The second line is technically optional but it's recommended particularly 00:09:04.690 --> 00:09:05.680 as you get started. 00:09:05.680 --> 00:09:07.580 Export flask debug equals 1. 00:09:07.580 --> 00:09:09.360 1 here just basically means true. 00:09:09.360 --> 00:09:11.110 So basically you're going to be running it 00:09:11.110 --> 00:09:14.620 so that when you run your flask application in your IDE, 00:09:14.620 --> 00:09:18.205 you are seeing all of the things that it's doing in debug mode. 00:09:18.205 --> 00:09:21.080 So if something goes wrong, it will get printed out to your terminal, 00:09:21.080 --> 00:09:21.640 you'll see it. 00:09:21.640 --> 00:09:24.400 It's not like your site will just crash and you won't know what happened. 00:09:24.400 --> 00:09:27.233 And then after executing those two lines of code, again, in our IDE, 00:09:27.233 --> 00:09:30.880 just executing those two commands, rather, in our IDE, flask run. 00:09:30.880 --> 00:09:33.070 And then what will happen specifically in your IDE, 00:09:33.070 --> 00:09:35.655 is it will take about a second to spin up 00:09:35.655 --> 00:09:37.780 and then it'll give you a URL that you can click on 00:09:37.780 --> 00:09:41.230 and visit, which will be the home page of your application. 00:09:41.230 --> 00:09:43.521 Again, after the first time you get configured 00:09:43.521 --> 00:09:45.520 with the particular application you want to run, 00:09:45.520 --> 00:09:48.270 you can leave these two blue lines out, you won't need them again. 00:09:48.270 --> 00:09:51.070 You can just continue to type flask run and it 00:09:51.070 --> 00:09:53.560 will continue to run that same application over and over 00:09:53.560 --> 00:09:55.090 until you no longer want to run it. 00:09:55.090 --> 00:09:58.214 And then you would export a different application, export a different debug 00:09:58.214 --> 00:10:00.530 mode, and so on. 00:10:00.530 --> 00:10:04.210 So in addition to just running code as we 00:10:04.210 --> 00:10:07.669 did before, just having it print out you are at the index 00:10:07.669 --> 00:10:09.460 and you are at the sample page, we can also 00:10:09.460 --> 00:10:12.730 pass data in to our Flask applications. 00:10:12.730 --> 00:10:17.077 We can do this using HTTP get, you may recall, by passing in data via URL. 00:10:17.077 --> 00:10:19.910 So I might write a function that looks a little something like this. 00:10:19.910 --> 00:10:23.320 It's a function called show and instead of taking 00:10:23.320 --> 00:10:26.290 no parameters like index and sample did earlier, 00:10:26.290 --> 00:10:29.200 I'm accepting a parameter here called number. 00:10:29.200 --> 00:10:32.560 And I'm returning you passed in, and then I'm 00:10:32.560 --> 00:10:36.730 apparently going interpolate or stick in some number. 00:10:36.730 --> 00:10:38.240 Where does that number come from? 00:10:38.240 --> 00:10:39.700 Well, look at my decorator here. 00:10:39.700 --> 00:10:43.600 @app.route slash show slash. 00:10:43.600 --> 00:10:46.450 And those angle brackets are actually important, 00:10:46.450 --> 00:10:49.450 we actually have to leave those in, number. 00:10:49.450 --> 00:10:54.670 So that if I visited my application slash show slash 10, 00:10:54.670 --> 00:10:56.950 the page would show you passed in 10. 00:10:56.950 --> 00:11:00.730 If I went to slash show slash 50, it would say you passed in 50. 00:11:00.730 --> 00:11:05.170 So it allows me to collect information using HTTP get 00:11:05.170 --> 00:11:09.150 by having the user supply it via a URL. 00:11:09.150 --> 00:11:13.680 We can also pass in data via HTML forms, which are transmitted not by get 00:11:13.680 --> 00:11:15.540 but by something called post. 00:11:15.540 --> 00:11:18.090 Now, by default, Flask is configured to only accept 00:11:18.090 --> 00:11:21.180 HTTP get requests or information via the URL 00:11:21.180 --> 00:11:24.720 so we have to modify a couple of things to make sure that Flask will also 00:11:24.720 --> 00:11:27.720 respond to HTTP post requests, for example, 00:11:27.720 --> 00:11:30.750 when data is submitted via a form. 00:11:30.750 --> 00:11:36.242 So if you get to problem set 7 in CS50, which is CS50 finance, 00:11:36.242 --> 00:11:37.950 you'll see that in our distribution code, 00:11:37.950 --> 00:11:40.050 we have something that looks a little similar to this. 00:11:40.050 --> 00:11:41.920 So this is where this is coming from if you want to take a look. 00:11:41.920 --> 00:11:44.460 I've taken out a couple of things just to fit this four 00:11:44.460 --> 00:11:47.160 lines of code on the slide here. 00:11:47.160 --> 00:11:49.380 But this is some of the sample code that we give you 00:11:49.380 --> 00:11:53.970 that is checking to see whether the user has provided 00:11:53.970 --> 00:11:57.190 a user name in a particular field. 00:11:57.190 --> 00:12:01.847 So notice that in addition in my decorator to saying where 00:12:01.847 --> 00:12:04.680 the function should be associated, so in this case with slash login, 00:12:04.680 --> 00:12:06.730 I'm also saying not only that, but you should 00:12:06.730 --> 00:12:10.110 be accepting data via the get method or via the post method. 00:12:10.110 --> 00:12:12.200 So that's how I explicitly call it out. 00:12:12.200 --> 00:12:14.190 Then I'm defining my login function. 00:12:14.190 --> 00:12:20.730 And I'm saying if not request.form.get username. 00:12:20.730 --> 00:12:25.860 Using get here is unfortunately a side effect of Python. 00:12:25.860 --> 00:12:28.590 That does not mean we're using the get method here. 00:12:28.590 --> 00:12:31.690 Basically what I'm asking for is go and retrieve, 00:12:31.690 --> 00:12:33.500 literally using get in that context. 00:12:33.500 --> 00:12:38.670 Go and retrieve from the form the field called username. 00:12:38.670 --> 00:12:42.107 And here I'm saying if it doesn't exist, apologize to the user. 00:12:42.107 --> 00:12:43.440 Say they need supply a username. 00:12:43.440 --> 00:12:46.050 So basically this is asking the question is the user name 00:12:46.050 --> 00:12:52.217 field of whatever form the user submitted to get here, is that blank? 00:12:52.217 --> 00:12:55.300 If it is, we need to apologize to the user and have them return something. 00:12:55.300 --> 00:12:57.800 If not, we'll go to some more lines of code down below 00:12:57.800 --> 00:12:59.591 and you're going to see those lines of code 00:12:59.591 --> 00:13:03.310 in the distribution code for PSet 7, if you want to take a look. 00:13:03.310 --> 00:13:05.440 Now, we can also vary the behavior of a function 00:13:05.440 --> 00:13:08.590 depending on which type of HTTP request we got. 00:13:08.590 --> 00:13:10.990 We can do one thing if we got a get request 00:13:10.990 --> 00:13:13.060 and we can do one thing if we got a post request. 00:13:13.060 --> 00:13:17.354 For example, if we got a get request, we might show them the form. 00:13:17.354 --> 00:13:19.270 But if we got a post request, then we're going 00:13:19.270 --> 00:13:22.930 to be expecting that they have provided some data via the forms. 00:13:22.930 --> 00:13:25.780 We can use one URL to do two different things 00:13:25.780 --> 00:13:27.347 depending on how the data came to us. 00:13:27.347 --> 00:13:29.680 And that actually is what we do, that some of the code I 00:13:29.680 --> 00:13:31.750 took out on the previous page. 00:13:31.750 --> 00:13:36.340 But this code also appears in the login function for PSet 7. 00:13:36.340 --> 00:13:38.740 If the request method was post, that was then 00:13:38.740 --> 00:13:41.710 where I was checking to see whether the fields were all filled in. 00:13:41.710 --> 00:13:44.374 Otherwise, what I actually do here, which an else here 00:13:44.374 --> 00:13:46.540 implies if the request method is get, because I only 00:13:46.540 --> 00:13:49.810 have two methods this function accepts, get and post. 00:13:49.810 --> 00:13:51.910 Otherwise, what I'm doing is actually displaying 00:13:51.910 --> 00:13:55.106 the form in the first place, the form that I expect them to type into. 00:13:55.106 --> 00:13:56.980 Which submits to this page again in the hopes 00:13:56.980 --> 00:13:59.530 that they will have submitted data that I could use. 00:13:59.530 --> 00:14:02.950 And I would process in the post part of this function as opposed 00:14:02.950 --> 00:14:06.100 to in the get part of this function. 00:14:06.100 --> 00:14:11.250 So if request.method basically is allowing me to check how I got data. 00:14:11.250 --> 00:14:13.530 Request is something that is part of Flask 00:14:13.530 --> 00:14:16.350 and it will let me see whether I got data via get or via post 00:14:16.350 --> 00:14:20.040 and allow me to do different things depending on which it was. 00:14:20.040 --> 00:14:22.350 There are a lot of functions within the Flask module 00:14:22.350 --> 00:14:25.210 that will be useful for you as you continue to develop applications. 00:14:25.210 --> 00:14:27.300 So in addition to saying from Flask import 00:14:27.300 --> 00:14:29.310 Flask at the beginning of your files, you 00:14:29.310 --> 00:14:33.290 might say from Flask import Flask comma any of these. 00:14:33.290 --> 00:14:35.970 And you'll leave out the parentheses when you're doing that part 00:14:35.970 --> 00:14:38.678 but you'll use the parentheses when you're calling the functions. 00:14:38.678 --> 00:14:40.680 URL underscore for. 00:14:40.680 --> 00:14:42.780 So this allows me to define a function and have 00:14:42.780 --> 00:14:44.190 a decorator associated with it. 00:14:44.190 --> 00:14:46.530 And maybe that decorator specifies that the URL here 00:14:46.530 --> 00:14:51.170 is really long or, for whatever reason, not that helpful. 00:14:51.170 --> 00:14:54.660 What I can do here is instead of specifying 00:14:54.660 --> 00:14:58.050 the exact URL I want to go to, I can say what 00:14:58.050 --> 00:15:01.770 I want you to do here is go to the URL for whatever function it is. 00:15:01.770 --> 00:15:04.230 So say my login function was associated with some URL that 00:15:04.230 --> 00:15:05.759 was like 50 characters long. 00:15:05.759 --> 00:15:07.800 Here, I could just pass in, instead of typing out 00:15:07.800 --> 00:15:10.530 that 50 character long URL where I wanted to use it, 00:15:10.530 --> 00:15:13.620 I could just say URL underscore for login. 00:15:13.620 --> 00:15:15.780 And because I associate it with a decorator, 00:15:15.780 --> 00:15:17.280 it'll just plug it in there instead. 00:15:17.280 --> 00:15:20.780 So it's a way to kind of shorthand a couple of things. 00:15:20.780 --> 00:15:23.900 Redirect, as you might expect, is Flask's function 00:15:23.900 --> 00:15:26.900 that redirects you from one page to another. 00:15:26.900 --> 00:15:29.722 Session is useful for HTTP session data. 00:15:29.722 --> 00:15:32.930 As we've talked about get and post, you may recall that there's also a third. 00:15:32.930 --> 00:15:35.540 And there are a few others but there's another thing 00:15:35.540 --> 00:15:37.460 we would use called a session variable. 00:15:37.460 --> 00:15:40.501 It's not necessarily an HTTP thing but it's usually stored in the headers 00:15:40.501 --> 00:15:41.640 there as well. 00:15:41.640 --> 00:15:44.690 And we can use that, for example, to track that a user is logged in. 00:15:44.690 --> 00:15:46.523 And constantly check that they are logged in 00:15:46.523 --> 00:15:49.310 even if they're going to different pages on our site. 00:15:49.310 --> 00:15:51.950 So a session is used sort of like a global variable. 00:15:51.950 --> 00:15:54.020 It's accessible by all pages. 00:15:54.020 --> 00:15:57.200 If you establish a session, you can store data in the session for the user 00:15:57.200 --> 00:15:58.560 as well. 00:15:58.560 --> 00:16:03.470 Render template is used to create pages on your site that 00:16:03.470 --> 00:16:07.690 mix HTML and Python together. 00:16:07.690 --> 00:16:09.440 So I might render a template, for example, 00:16:09.440 --> 00:16:11.270 where that template is the form that I want 00:16:11.270 --> 00:16:14.300 to show to the user, the form I was talking about on an earlier slide. 00:16:14.300 --> 00:16:18.920 I might have an HTML template where I've written the general form there. 00:16:18.920 --> 00:16:22.580 And I might render a template and reference in the parentheses 00:16:22.580 --> 00:16:26.422 there the HTML for the form that I was just talking about. 00:16:26.422 --> 00:16:28.130 And you can do some other cool things too 00:16:28.130 --> 00:16:30.976 where you mix HTML and Python together using 00:16:30.976 --> 00:16:32.850 a language called Jinja, which is going to be 00:16:32.850 --> 00:16:34.640 beyond the scope of this particular video. 00:16:34.640 --> 00:16:36.080 But if you want to get more information about what 00:16:36.080 --> 00:16:39.329 you can do in the Flask quickstart guide and you really can get started pretty 00:16:39.329 --> 00:16:40.907 quickly with it, the URL is there. 00:16:40.907 --> 00:16:42.740 And if you want to learn about Jinja and how 00:16:42.740 --> 00:16:48.332 you can mix HTML and Python together to create basic templates where 00:16:48.332 --> 00:16:50.540 you can maybe substitute a couple of different things 00:16:50.540 --> 00:16:54.050 in depending on what the user has submitted via a get or post request. 00:16:54.050 --> 00:16:55.940 You can get some information on Jinja there. 00:16:55.940 --> 00:16:58.590 Jinja is Python-inspired syntax. 00:16:58.590 --> 00:17:01.490 So some of the things that you'll be familiar with from Python syntax 00:17:01.490 --> 00:17:02.780 will be relevant there. 00:17:02.780 --> 00:17:05.510 But there are a couple of little twists as well. 00:17:05.510 --> 00:17:09.040 And you can learn more about Jinja at that URL there as well. 00:17:09.040 --> 00:17:10.210 I'm Doug Lloyd. 00:17:10.210 --> 00:17:12.010 This is CS50.