First Steps5:58 with Kenneth Love
It's always good to start small, so let's create a simple Flask app with a single view and route.
View: A view is a function that returns an HTTP response. This response has to be a string but can be any string you want.
Route: A route is the URL path to a view. They always start with a forward slash
/ and can end with one if you want.
dunder: A quick way of saying "double underscore".
We call our Flask script an app. 0:00 The app is where all of the requests come to. 0:03 The app then sends those requests to the correct function or view. 0:05 It finds this function through the route. 0:09 And, you know what, that's way too many words. 0:10 Bring on the graphics. 0:13 [SOUND] Okay, so the app gets all of the requests. 0:15 Requests are just people on the Internet asking for information from our app. 0:18 Once they get to our app, [SOUND] the app looks at all the routes and 0:22 figures out which function to call. 0:26 A lot of people call these functions [SOUND] controllers, but 0:27 I prefer the Django way and call [SOUND] them views. 0:30 The [SOUND] view then sends back a response, which can be HTML, or JSON, or 0:32 XML, or whatever your specific needs are. 0:36 We call that the request-response cycle. 0:39 If it doesn't make much sense now, don't worry, we'll be going over it repeatedly. 0:41 Let's go make our app, and a simple function or view. 0:46 Since Flask is already installed on Workspaces, as you can see here, 0:49 I don't need to install it and I can immediately start using it. 0:53 So, I'm gonna make a file up here, and I'm gonna call it simple_app.py. 0:58 And this is going to hold my app. 1:07 So, let's make an app, right? 1:10 We're gonna make a nice, simple little app. 1:12 What we have to do first, though, is we have to import Flask. 1:14 So, from flask, with a lowercase f, we want to import Flask with an uppercase F. 1:16 If you remember, in Python, 1:23 we always name classes with an uppercase letter, or almost always. 1:26 You're supposed to always. 1:31 Anyway, so Flask here with a capital F is a class. 1:33 That's how we know that. 1:36 So now we need to make an app instance. 1:38 So, what we're gonna do is we're gonna make it to where our app is named, 1:41 whatever our current namespace is. 1:44 And namespaces are a somewhat complicated area in Python, or they can be. 1:46 What we're gonna do, though, is we're gonna do this app equals flask and 1:51 we're gonna pass in this magic variable called dunder name, __name__. 1:56 Dunders on both sides of the name. 2:01 And what this means is use whatever our current namespace is. 2:04 So, if we run this file with like Python simple_app.py, 2:08 then the namespace is dunder main, which means it's being run directly. 2:13 If we were to import simple_app.py into another file, 2:18 then our namespace will be simple_app. 2:21 So, it's just a way of mean, saying, always refer to yourself. 2:24 Then we're going to run our app. 2:28 And we're gonna say debug equals True cuz we want, 2:31 anytime we make changes, we want Flask to auto-restart. 2:34 And then just for Treehouse because we're gonna, 2:37 we have a new special little thing over here in the corner. 2:41 We need to specify a port and a host, so our port is gonna be 8000, and 2:44 what the port means is this is the software port to listen on. 2:50 Think of it has having, like, a bunch of different doors or 2:55 this is like saying which telephone number, kind of, to call on. 3:00 It's closer to having a bunch of different doors on a house, 3:04 which door is it gonna be at. 3:06 And then our host, which is like which house is it on the street, 3:08 is going to be 0.0.0.0, four 0s with dots between them. 3:13 And that means listen on all addresses that can get to here. 3:18 So, we're gonna save that and let's see if this runs. 3:23 Let's give it a try. 3:30 So what we're gonna do, we're going to come down here to our console and 3:31 we're going to do python simple_app.py. 3:36 All right, so you see here, it says it's running on our 0s and 3:40 colon 8000, and it's restarting with a reloader. 3:44 So, we're gonna come up here to this little eye. 3:48 And this may be the first time you've seen this. 3:51 This is a pretty new feature. 3:52 And we're gonna say we wanna preview this on port 80. 3:54 Now, we could use any of these numbers, but we wanna use port 8000. 3:57 And you can see, we get this 404. 4:01 And that's fine because we haven't told there to be anything there. 4:05 So, it doesn't know what it's doing, right? 4:09 We've got this, this, like, no route. 4:13 Okay. 4:16 So, let's let's give it a route. 4:16 Because we haven't created any, so we need to create some. 4:19 All right. So, before we do the app.run, 4:23 this is where we get to define our routes. 4:26 So let's make a function called index, and we're just gonna return a string. 4:28 We're gonna return Hello from Treehouse. 4:33 'Kay? 4:38 Now, this is just a function. 4:39 This isn't a view yet. 4:40 I mean it is, but it's really not. 4:43 It's really just a function. 4:45 What makes this is a view is by giving it a route. 4:46 And we give it a route by decorating it with this function called route, and 4:49 then we have to specify what the route is. 4:55 So we're just going to use forward slash, which, if we look, 4:57 let's see if we can make this its own window. 5:02 We can, sweet. 5:07 If we look at the URL here, 5:08 we see that we're technically just at this forward slash. 5:10 So, that's where we're going, and now we've got Hello from Treehouse. 5:13 So there is Hello from Treehouse. 5:18 This is, I said this was a decorator. 5:22 So, what is a decorator? 5:24 Well, a decorator is a function that wraps around another function and 5:25 lets you do things to the function. 5:30 So what we've done is we've said, hey, take this index function and register it 5:32 with our app as the route, forward slash, just the single forward slash. 5:37 So, we've added this route, added this function, and 5:44 we get our Hello from Treehouse, so that's pretty awesome. 5:47 All right, we're done with everything. 5:50 Oh wait, no, that's not nearly enough. 5:53 In the next video, let's look at pulling information out of the URL. 5:55
You need to sign up for Treehouse in order to download course files.Sign up