Python for the .NET developer Transcripts
Chapter: Web frameworks
Lecture: Hello world Flask
0:00 Let's build our first web application in Python.
0:02 So we're going to create a new chapter section over here.
0:09 We'll call it ch07_web.
0:10 And again, we want it to be treated like its own
0:13 top-level thing.
0:14 So I'm going to say 'mark directory as sources root'.
0:17 Now, in here, we're going to create our first web application.
0:22 And we're going to create that Guitary project.
0:27 That's out litte startup name.
0:28 So we're going to create a folder called guitary
0:31 and we're going to basically use this
0:33 as our top-level webb app.
0:36 Now, Flask has a really simple and easy
0:39 getting started thing.
0:40 I could have come up here, even, and said New Project
0:44 go over to Flask, pick off some of the settings
0:47 and it will go and create that.
0:49 Alright, it will create the virtual environment
0:50 it will set up the project structure, it'll choose
0:52 the right templating language
0:55 but, just to make everything a little more obvious
0:58 and clear, what all the steps mean
1:00 the first time here I'm going to go and create it from scratch.
1:03 Maybe you're using VS Code, or something like that
1:06 and it doesn't have a file named Flask project story.
1:09 So the way it works is you just want to create a Hello World
1:11 now and then we're going to create Guitary on top of that.
1:14 So what we're going to do typically
1:16 the startup thing here is called app.py.
1:21 And we're also going to have some project structure.
1:24 Let's go ahead and make that now.
1:25 We'll have templates. These are like
1:27 the equivalent of your Razor CSHTML files
1:31 the dynamic HTML files, and then, eventually
1:34 we're going to have static content as well
1:37 and I like to break this up further.
1:40 Uh, I'll go ahead and copy stuff over
1:44 maybe even more divisions amongst those.
1:47 But for now, all we need is this.
1:49 So, what we want to do, is we want to import flask.
1:53 Now, PyCharm right away says, Whoa, whoa, whoa!
1:56 There's a problem here, I need to install Flask.
1:58 So we come over here, it will install it.
2:01 Remember, the most appropriate way would be to
2:03 come over here and type flask
2:05 and then rerun, down here.
2:09 pip install -r requirements.
2:10 And I would pick up that new change and get it.
2:12 But, you know, since I've already shown you that
2:14 let's just see one more possibility.
2:16 In PyCharm, I could actually just hit
2:17 alt-enter and it will show me
2:19 hey, you can install Flask.
2:22 Let's go in to install it, in a second.
2:25 Now we need to go ahead and use it.
2:27 So the first thing we need to do is create an app.
2:30 The way we do this, this is a really important part
2:33 of the project. It contains the decorators and
2:35 all the stuff that drive the project.
2:36 So we say flask.Flask, then we need to give it a name
2:41 and typically the import name is going to be
2:43 the same as the file name.
2:45 So Python files have this __name__
2:50 ambient variable always defined
2:51 and it's like 'app' or 'main' or something to this effect
2:54 depending on how it gets read.
2:56 It's either the file name, the module name
2:58 or it's main if you run it directly.
3:01 Now we have this installed on Flask
3:04 now sometimes it'll say you should add it
3:06 to the requirements but it's not doing that here
3:08 so, before I forget let's go and add it
3:10 but I was hoping it would let us show you
3:13 but again it's, I think we have too many projects
3:14 going on, all in one giant meta-project here.
3:18 So, what we need to do, is we need to define
3:21 in the MVC, ASP.NET MVC world, what would be called
3:24 an action method. In Flask these are often referred to
3:27 as view methods, but same idea.
3:30 Here we're going to define an index
3:32 and it's currently going to take no parameters.
3:35 And then we're just going to return, "Hello World".
3:38 Something like this.
3:40 Now this seems great, except for
3:43 how does Flask know that this is a web method
3:46 and not just some utility function or
3:48 some other random thing.
3:49 You add a decorator, so we say app.route.
3:53 Like this is where we specify the route or the URL
3:56 so this'll just forward-slash and this is going to define
4:00 just the homepage of our website.
4:03 The last thing we want to do, is we're going to run
4:05 the program, we're going to basically start up the
4:08 web server and start listening for requests.
4:10 And remember I have my cool little extension here
4:12 so I'm going to create a main, and in my main
4:14 we're going to call app.run.
4:16 We could say even, debug equals true.
4:19 It'll do things like, check to see if we've
4:21 modified the file and then auto-restart it
4:24 and we'll see how that works in a sec.
4:26 Alright, so let's just go try to run our Guitary here.
4:33 And that's a pretty good sign.
4:34 First of all, it says, this is a development server
4:37 so just beware, don't put it into production
4:39 use a production WSGI server.
4:41 Remember we talked about WSGI
4:42 and Web Server Gateway Interfaces?
4:44 These are uWSGI, Gunicorn, things like that.
4:47 We'll get to that at the very end of the course.
4:49 Alright, so let's run it, Ahhh! Hello World!
4:51 We do a 'view source', Hello World
4:54 not too interesting, it's not really HTML
4:56 but, you know, the browsers roll with it anyway
4:58 we can just show that on the screen.
5:00 So here is our is our "Hello Flask"
5:03 what we've done, is we import Flask
5:05 we've installed it, and put in the requirements.
5:07 We create a singleton, super important
5:09 singleton app instance here that's shared
5:12 throughout the rest of this entire application.
5:15 And then we use it to, amongst other things
5:17 you know, specify the URL routes on any method.
5:20 This doesn't have to be called index
5:22 it could be called, whatever.
5:25 By the way, notice that when I made that change
5:28 it said, oh, oh this file has changed,
5:30 it's reloading in the debugger.
5:33 Or reloading the project, and now it still works
5:36 even though I called it some random thing.
5:39 That's important because the Python code here
5:41 if it changes, it doesn't automatically restart the app
5:44 usually, only in this debug mode.
5:46 So, for example, in production, if you make a change
5:48 to like the logic of this method.
5:51 It's not going to reload it.
5:52 In some frameworks, even the templates get cached
5:54 and not reloaded, so that's the big difference between
5:57 debug and not debug.
5:59 Then once everything's all set up, we just call app.run
6:03 and we have a website.