1 00:00:00,650 --> 00:00:03,080 We call our Flask script an app. 2 00:00:03,080 --> 00:00:05,480 The app is where all of the requests come to. 3 00:00:05,480 --> 00:00:09,170 The app then sends those requests to the correct function or view. 4 00:00:09,170 --> 00:00:10,740 It finds this function through the route. 5 00:00:10,740 --> 00:00:13,030 And, you know what, that's way too many words. 6 00:00:13,030 --> 00:00:13,820 Bring on the graphics. 7 00:00:15,160 --> 00:00:18,419 [SOUND] Okay, so the app gets all of the requests. 8 00:00:18,419 --> 00:00:21,740 Requests are just people on the Internet asking for information from our app. 9 00:00:22,880 --> 00:00:26,066 Once they get to our app, [SOUND] the app looks at all the routes and 10 00:00:26,066 --> 00:00:27,895 figures out which function to call. 11 00:00:27,895 --> 00:00:30,426 A lot of people call these functions [SOUND] controllers, but 12 00:00:30,426 --> 00:00:32,532 I prefer the Django way and call [SOUND] them views. 13 00:00:32,532 --> 00:00:36,974 The [SOUND] view then sends back a response, which can be HTML, or JSON, or 14 00:00:36,974 --> 00:00:39,640 XML, or whatever your specific needs are. 15 00:00:39,640 --> 00:00:41,780 We call that the request-response cycle. 16 00:00:41,780 --> 00:00:46,010 If it doesn't make much sense now, don't worry, we'll be going over it repeatedly. 17 00:00:46,010 --> 00:00:49,220 Let's go make our app, and a simple function or view. 18 00:00:49,220 --> 00:00:53,910 Since Flask is already installed on Workspaces, as you can see here, 19 00:00:53,910 --> 00:00:58,100 I don't need to install it and I can immediately start using it. 20 00:00:58,100 --> 00:01:07,420 So, I'm gonna make a file up here, and I'm gonna call it simple_app.py. 21 00:01:07,420 --> 00:01:10,438 And this is going to hold my app. 22 00:01:10,438 --> 00:01:12,290 So, let's make an app, right? 23 00:01:12,290 --> 00:01:14,020 We're gonna make a nice, simple little app. 24 00:01:14,020 --> 00:01:16,660 What we have to do first, though, is we have to import Flask. 25 00:01:16,660 --> 00:01:23,930 So, from flask, with a lowercase f, we want to import Flask with an uppercase F. 26 00:01:23,930 --> 00:01:26,256 If you remember, in Python, 27 00:01:26,256 --> 00:01:31,660 we always name classes with an uppercase letter, or almost always. 28 00:01:31,660 --> 00:01:33,320 You're supposed to always. 29 00:01:33,320 --> 00:01:36,675 Anyway, so Flask here with a capital F is a class. 30 00:01:36,675 --> 00:01:37,460 That's how we know that. 31 00:01:38,540 --> 00:01:41,110 So now we need to make an app instance. 32 00:01:41,110 --> 00:01:44,520 So, what we're gonna do is we're gonna make it to where our app is named, 33 00:01:44,520 --> 00:01:46,570 whatever our current namespace is. 34 00:01:46,570 --> 00:01:51,960 And namespaces are a somewhat complicated area in Python, or they can be. 35 00:01:51,960 --> 00:01:56,622 What we're gonna do, though, is we're gonna do this app equals flask and 36 00:01:56,622 --> 00:02:01,736 we're gonna pass in this magic variable called dunder name, __name__. 37 00:02:01,736 --> 00:02:04,000 Dunders on both sides of the name. 38 00:02:04,000 --> 00:02:08,590 And what this means is use whatever our current namespace is. 39 00:02:08,590 --> 00:02:13,365 So, if we run this file with like Python simple_app.py, 40 00:02:13,365 --> 00:02:18,300 then the namespace is dunder main, which means it's being run directly. 41 00:02:18,300 --> 00:02:21,635 If we were to import simple_app.py into another file, 42 00:02:21,635 --> 00:02:24,510 then our namespace will be simple_app. 43 00:02:24,510 --> 00:02:27,480 So, it's just a way of mean, saying, always refer to yourself. 44 00:02:28,580 --> 00:02:31,360 Then we're going to run our app. 45 00:02:31,360 --> 00:02:34,160 And we're gonna say debug equals True cuz we want, 46 00:02:34,160 --> 00:02:37,730 anytime we make changes, we want Flask to auto-restart. 47 00:02:37,730 --> 00:02:41,060 And then just for Treehouse because we're gonna, 48 00:02:41,060 --> 00:02:44,230 we have a new special little thing over here in the corner. 49 00:02:44,230 --> 00:02:50,170 We need to specify a port and a host, so our port is gonna be 8000, and 50 00:02:50,170 --> 00:02:55,820 what the port means is this is the software port to listen on. 51 00:02:55,820 --> 00:03:00,010 Think of it has having, like, a bunch of different doors or 52 00:03:00,010 --> 00:03:04,250 this is like saying which telephone number, kind of, to call on. 53 00:03:04,250 --> 00:03:06,502 It's closer to having a bunch of different doors on a house, 54 00:03:06,502 --> 00:03:08,080 which door is it gonna be at. 55 00:03:08,080 --> 00:03:13,450 And then our host, which is like which house is it on the street, 56 00:03:13,450 --> 00:03:18,933 is going to be 0.0.0.0, four 0s with dots between them. 57 00:03:18,933 --> 00:03:22,630 And that means listen on all addresses that can get to here. 58 00:03:23,650 --> 00:03:30,450 So, we're gonna save that and let's see if this runs. 59 00:03:30,450 --> 00:03:31,690 Let's give it a try. 60 00:03:31,690 --> 00:03:36,460 So what we're gonna do, we're going to come down here to our console and 61 00:03:36,460 --> 00:03:40,392 we're going to do python simple_app.py. 62 00:03:40,392 --> 00:03:44,317 All right, so you see here, it says it's running on our 0s and 63 00:03:44,317 --> 00:03:48,620 colon 8000, and it's restarting with a reloader. 64 00:03:48,620 --> 00:03:51,565 So, we're gonna come up here to this little eye. 65 00:03:51,565 --> 00:03:52,930 And this may be the first time you've seen this. 66 00:03:52,930 --> 00:03:54,070 This is a pretty new feature. 67 00:03:54,070 --> 00:03:57,304 And we're gonna say we wanna preview this on port 80. 68 00:03:57,304 --> 00:04:01,567 Now, we could use any of these numbers, but we wanna use port 8000. 69 00:04:01,567 --> 00:04:05,960 And you can see, we get this 404. 70 00:04:05,960 --> 00:04:08,930 And that's fine because we haven't told there to be anything there. 71 00:04:09,990 --> 00:04:13,115 So, it doesn't know what it's doing, right? 72 00:04:13,115 --> 00:04:16,200 We've got this, this, like, no route. 73 00:04:16,200 --> 00:04:16,790 Okay. 74 00:04:16,790 --> 00:04:19,690 So, let's let's give it a route. 75 00:04:19,690 --> 00:04:23,420 Because we haven't created any, so we need to create some. 76 00:04:23,420 --> 00:04:26,220 All right. So, before we do the app.run, 77 00:04:26,220 --> 00:04:28,330 this is where we get to define our routes. 78 00:04:28,330 --> 00:04:33,430 So let's make a function called index, and we're just gonna return a string. 79 00:04:33,430 --> 00:04:38,055 We're gonna return Hello from Treehouse. 80 00:04:38,055 --> 00:04:39,030 'Kay? 81 00:04:39,030 --> 00:04:40,280 Now, this is just a function. 82 00:04:40,280 --> 00:04:42,350 This isn't a view yet. 83 00:04:43,790 --> 00:04:45,230 I mean it is, but it's really not. 84 00:04:45,230 --> 00:04:46,260 It's really just a function. 85 00:04:46,260 --> 00:04:49,380 What makes this is a view is by giving it a route. 86 00:04:49,380 --> 00:04:55,860 And we give it a route by decorating it with this function called route, and 87 00:04:55,860 --> 00:04:57,820 then we have to specify what the route is. 88 00:04:57,820 --> 00:05:02,450 So we're just going to use forward slash, which, if we look, 89 00:05:02,450 --> 00:05:05,650 let's see if we can make this its own window. 90 00:05:07,000 --> 00:05:08,810 We can, sweet. 91 00:05:08,810 --> 00:05:10,340 If we look at the URL here, 92 00:05:10,340 --> 00:05:13,330 we see that we're technically just at this forward slash. 93 00:05:13,330 --> 00:05:17,100 So, that's where we're going, and now we've got Hello from Treehouse. 94 00:05:18,750 --> 00:05:20,950 So there is Hello from Treehouse. 95 00:05:22,550 --> 00:05:24,145 This is, I said this was a decorator. 96 00:05:24,145 --> 00:05:25,326 So, what is a decorator? 97 00:05:25,326 --> 00:05:30,430 Well, a decorator is a function that wraps around another function and 98 00:05:30,430 --> 00:05:32,990 lets you do things to the function. 99 00:05:32,990 --> 00:05:37,750 So what we've done is we've said, hey, take this index function and register it 100 00:05:37,750 --> 00:05:44,000 with our app as the route, forward slash, just the single forward slash. 101 00:05:44,000 --> 00:05:47,240 So, we've added this route, added this function, and 102 00:05:47,240 --> 00:05:50,908 we get our Hello from Treehouse, so that's pretty awesome. 103 00:05:50,908 --> 00:05:53,021 All right, we're done with everything. 104 00:05:53,021 --> 00:05:55,230 Oh wait, no, that's not nearly enough. 105 00:05:55,230 --> 00:05:58,000 In the next video, let's look at pulling information out of the URL.