Python for Decision Makers and Business Leaders Transcripts
Chapter: Web development with Python
Lecture: Hello world Flask
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Here we are in our Python IDE, called PyCharm. There's a couple of good editors out there in the world. PyCharm is one of them, my favorite.
0:09
There's also a Visual Studio Code and a couple of other options. I've already set up a project by just creating a couple of folders and a file here.
0:17
To say that, we're going to use Flask. And we're going to start by creating the Hello World Flask application and then we're going to
0:25
extend that out, quite a bit. Often, people think the Hello World app idea is to show you the simplest thing that you could possibly do to get
0:33
Hello World on the screen. But actually one of the important things of these, Hello World little demo applications is to show you the entire
0:39
system is, kind of, hanging together and working. Yes, I can run Python. Yes, I can open up the web server and things like that.
0:45
So, let's do that, real quick here. Want a new Python file, my convention is typically called app, and in order to use a
0:54
library in Python regardless of whether it is one of these built-in standard library ones these batteries included or an external one
1:02
that I have gotten installed you just type import and the name of it. Here, you can see it automatically helps us out.
1:09
It says Flask, that's what we want to use. So in here when I create a thing called app and we say Flask dot capital Flask
1:16
and we give it the name of this file which you can get to like that. Now we have to create a function which is called when somebody requests a webpage.
1:24
So I'll just call that index or something like that. And here we're just going to say something like really simple return Hello world like that.
1:33
How about Hello Flask. Now this itself is not enough to reformat that This itself is not enough to make it exposed to Flask.
1:43
We've got to do one more thing. We're going to go over here and say app this is a route as in a destination URL, it's just forward slash.
1:51
Then finally down here we're going to say app.run. Now there's a little convention in Python that allows us to import this into
1:59
a production server or run it in testing. So I'm going to do that here like this. We'll go ahead and tell it it's in it's debug mode.
2:09
So in order to run it I just right click and say run. There's our web server. Look at that, it's already off to a good start.
2:16
What happens if I click on it? Well, Hello Flask. Whoo, make that nice and big, there we go way more dramatic when it's huge right?
2:23
Hello Flask, that's it. So we've created our Flask web application. There was no compile step, there was no installing
2:30
things other than to get this installed into here. Which I did before. But there's no like messing with the system
2:38
and things like that, it's just write these couple of lines run it, and we're off to the races with our Flask web app.