Check out a free preview of the full Intermediate Python course

The "Flask Hello World" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to start a Flask app.

Preview
Close

Transcript from the "Flask Hello World" Lesson

[00:00:00]
>> Nina Zakharenko: To make our first basic Flask app, we're gonna need to install Flask.
>> Nina Zakharenko: So to do that it's, python -m pip install flask.
>> Nina Zakharenko: And I already have it, but you should see it downloading.
>> Nina Zakharenko: Okay, I'm gonna make a new file. And I'm gonna save it as app.py.

[00:00:28]
>> Nina Zakharenko: And then,
>> Nina Zakharenko: The first thing I'm gonna need to do is import Flask. So I'm gonna say from lowercase flask, which is the package here, import Flask, which is the class.
>> Nina Zakharenko: And then I'm gonna make a new app object. I'm gonna say the app is equal to the Flask class.

[00:00:50]
And I'm gonna instantiate it with vendor name.
>> Nina Zakharenko: Now, the way Flask knows how to handle requests is by specifying an app route. We're gonna use something called a decorator to specify this route.
>> Nina Zakharenko: And the way that decorators work in Python is you put them above the function definition.

[00:01:21]
So I'm gonna have a simple hello_world method here. And I want it to return Hello World! But in order for Flask to know which route to serve this from, I'm gonna have use the @ symbol, that's the symbol for decorator, and say app.route.
>> Nina Zakharenko: And I'm gonna pass in a string here of just a /.

[00:01:47]
That's the route for our application. Now if I save this file,
>> Nina Zakharenko: I'm gonna save this file as app.py or as hello.py so I can use app.py later. So I'm gonna move this app.py to hello.py. And if you are not sure how to do that from the terminal, you can just do Save As in VS Code.

[00:02:20]
Okay, so to run my FLASK_APP,
>> Nina Zakharenko: I need to tell the Flask runner,
>> Nina Zakharenko: Where is my code contained? I'm gonna say it's contained in hello.py. And then I'm gonna say flask run. Let me try that again. So this brings up a local server on our computer that's running our applications, serving it.

[00:02:56]
You don't wanna use this development server in a production environment, right? It doesn't scale, it's just for testing. You wanna use the production WSGI server instead. So just an FYI. Something like Gunicorn would do the trick. A really great feature of VS Code is I can put my mouse over this link.

[00:03:19]
And it will tell me I can Cmd + click to follow it. So if I do that, I should get my Hello World Flask application here. Does everyone see the same thing?
>> Nina Zakharenko: Okay,
>> Nina Zakharenko: So that is Flask Hello World, It's pretty easy. Windows folks, you're gonna have to try this out for me.

[00:03:50]
>> Nina Zakharenko: $env in here, and just copy this in here, this commit.
>> Nina Zakharenko: So for Windows folks, try $env:FLASK_APP,
>> Nina Zakharenko: =, and then these single quotes, 'hello.py'. And then this : and flask run. And let me know if that works.
>> Speaker 2: Is that a semicolon or a colon after the hello.py?

[00:04:28]
>> Nina Zakharenko: It's a semicolon.
>> Speaker 2: Semicolon, thank you.
>> Nina Zakharenko: And I'll just paste it up here. And I'll say Windows? Hello.
>> Speaker 3: That works for me.
>> Nina Zakharenko: Awesome.
>> Speaker 2: Good to go, thank you.
>> Nina Zakharenko: Awesome, okay, so save this command somewhere handy. You'll be needing that for the rest of the exercise.

[00:04:51]
And I will put this at the top, right here. And I'm gonna have to open my hello.py. Okay, here we go.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now