#100DaysOfWeb in Python Transcripts
Chapter: Days 1-4: Flask Intro
Lecture: Define your Flask app routes in routes.py

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright, welcome back. In our last video we looked at the __init__.py file and we now move on to the routes.py file. You can see the relationship here.
0:15 At the end of __init__.py where we're setting up our Flask app instance, the last thing we do is we then say, from our program package, import routes
0:26 which is going to be our routes.py file. Let's edit that file now so we actually have something to import. Something to work with.
0:35 Now, traditionally, if this was a single file Flask app we would import Flask here but we've already done that in the __init__.py file
0:46 so we can pretty much get straight to the code. The only thing that we need to import here is the Flask app.
0:55 Okay, that app object that we created in our __init__.py file. To do that, we just simply go, from program from program import app.
1:09 And the whole __init__.py thing handles that, alright? That's all we need for the absolute basic Flask app here using our packaged app scenario.
1:20 Alright, so pretty much we can get straight into the Flask code. What we're going to do here is we're going to create an app route.
1:28 Now, this is going to be our URL. So the first URL we want to create is the root or the index path. In this instance, we can actually specify both
1:40 'cause they are practically the same thing, okay? So, we have this decorator here and it is creating our URL. What is it creating that against?
1:52 What is the code that is going to run against this URL? That's what the decorator works against.
1:59 In this case, we're going to run it against this function and we're going to call the function index().
2:06 General rule of thumb, I wouldn't say all the time but one of the good practices with Flask is to try and name your functions
2:14 similar to your actual URL. So, this is the index() function for the index or root path to our website. Alright. For the simplest of simple
2:29 all we're going to do is return something which is just plain text. We're just going to return plain text. We go "Hello Pythonistas" and that's it.
2:44 That's all we're returning. And would you believe it, that is our app. That's all we need to do, to make this very simple
2:53 initial Flash app work. So we can save that and we can pretty much run our app now. We just have to do one last thing so move on to the next video
3:04 we're going to configure that demo.py file and you'll see why I've left that 'till the end in the next video.


Talk Python's Mastodon Michael Kennedy's Mastodon