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