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