Check out a free preview of the full Intermediate Python course

The "Routing" 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 add routes to the app, and also how to pass in a flag that reloads the app every time there is a saved change.

Preview
Close

Transcript from the "Routing" Lesson

[00:00:00]
>> Nina Zakharenko: So very quickly, yesterday we looked at the request and response cycle for an API. Think of routing in Flask as the same thing. Flask is our web application, consider it our server. So our client is the browser. In this case, I'm asking for the route URL from my server, and my server is returning a response, just a plain old string that says, hello world.

[00:00:38]
>> Nina Zakharenko: Declaring routes in Flask. We saw that forward slash resolves to the route. But we can also put in as many paths as we want. Let's say bonjour_worlds returns, "Bonjour, World!". By default, we want all of our visitors to see, "Hello World!", but optionally, if they wanna take the special path for let's say France, we can do that.

[00:01:22]
So we would wanna declare a new app route. So still the forward slash, that's the route. But now I'm gonna say /french.
>> Nina Zakharenko: And make sure that your Flask app is running here. Before we do that, I'm gonna show you something pretty handy. In Flask you can have the debug mode for you local environment, where you can have your site just live reload your code.

[00:02:00]
So that you don't have to type your new function, change your route, play around with things, kill the server, start it back up again, pretty tedious process. So here, just like we set the Flask app to be hello.py, we want to back up a little bit. And we want to set a new environment variable.

[00:02:21]
And we're gonna set FLASK_ENV = development
>> Nina Zakharenko: So I'm just gonna copy that. So for Mac, it would be this and for Windows,
>> Nina Zakharenko: Hopefully, sorry about that. For Windows, hopefully I can just, come on, VS Code, you are not collaborating with me today. Hopefully I can just copy and paste this whole thing, and that does the right trick.

[00:02:58]
So I'm saying that FLASK_ENV is equal to development,
>> Nina Zakharenko: For Windows. Okay, now when we run Flask with our development environment, we'll see that debug mode is on. And it'll say, restarting what stat, the debugger's active. And now let's see. If we go to our webpage, we should be able to load it just fine.

[00:03:37]
Now if we try to add the path french, we'll see that the more specific route was picked up. Or I'm sorry, the route for french was picked up.
>> Nina Zakharenko: You can do a lot with this app.route. You can even accept parameters.
>> Nina Zakharenko: You can give type hints for those parameters.

[00:04:05]
You can specify which HTTP methods you want to accept in your route. For example, just get or just post. Let's try to make a new route that accepts a parameter. So I'm gonna call this app route /name/ and then a new symbol here, these brackets. And I'm gonna put a,

[00:04:42]
>> Nina Zakharenko: Name in here.
>> Nina Zakharenko: Now I can say, I'm gonna write a new function here, and my function has to accept this one parameter because I defined it in the app.route. And I'm gonna return an f string, and it's gonna say, Hello, and name.
>> Nina Zakharenko: Okay, now, saving this, I don't have to stop and start my server.

[00:05:16]
I'm gonna go to the path name/Nina, and now whatever I put at the end of this URL will be used as a parameter.

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