Python-powered Chat Apps with Twilio and SendGrid Transcripts
Chapter: Creating the Flask Python web app
Lecture: Structuring the app for growth

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, when you look at this, it seems like it couldn't be simpler, right? However, this application is going to grow and grow.
0:07 It's going to get more complicated as things come along. For example, we're gonna have to set up our database. We're gonna have to set up our secrets,
0:15 and our API key is that we can import. As the APP starts, we're going to have API end point. We're gonna have also admin HTML style Web methods.
0:25 Not all of those things belong in just one file. It's very common for people to say, Well, you just start with app.py and flask and then you write,
0:33 Well, that's true, but it doesn't have to be that way. The default most obvious path with the way flask works is to do those things,
0:41 but it really will not suit you. Well, as your application grows larger and larger over time, what I'd like to do is in this short section,
0:49 just get a little structure some organizations. So as our application grows, it's really clear where what functionality lives,
0:56 and it's not all piled in together. So we're going to have a couple of folders are going to create well,
1:01 API. So we're going to have some API endpoints that are being called by studio, by the flow there,
1:08 and we're going to have some Web in points that are just the HTML pages of our site. So those are going to put into the Views folder.
1:16 We're also gonna need a place to put the static HTML templates and create one called templates, and you'll see that we'll have more as we go.
1:23 But this is enough for us to get started. So this is one of these HTML views now it's not really that much HTML. It's the string hello world,
1:32 but it's kind of the general role that it's going to play and let's have one more. But when that's going to process an order and well,
1:42 so I'll just call this ordered like so, like this. So we'll have some API stuff that's going to live over in the API.
1:52 will have our homepage sort of things over here, so I'm gonna create module called home. And let's just take this and stick it over there.
2:02 That's that red squiggly under the APP. What are we supposed to get that Hang tight We'll fix that in a second.
2:08 Over here on this side, we're going to have another Python view API module. I would call that order_api and again,
2:18 this stuff over here, it's going to go there, like that. And again we see this warning.
2:25 So here's one of the reasons why I said that flask encourages you to jam all the stuff into one file because, well now, how do you deal with this?
2:32 We've just somehow to find this API reference to this URL's set attached to this order function, and yet we can't use it.
2:41 We can't even import this piece because if we import that and it's going to create
2:45 the circular reference that Python does not like. So flask has a really great solution for this. It's just not used as much as I wish it was.
2:53 And the idea is that there's a thing called a blueprint, and what blueprints do is they let you define all the routes and then take that
2:59 blueprint and register and say, Here's a bunch more routes that you didn't see a little bit of go, But let's register them as if we had done this.
3:06 So we'll come over here and say, this is going to be flask. We gotta import. Thank you. PyCharm. Give it a blueprint.
3:13 And then it's going to need a name and import name. And you can also do things like change the static folder, Change the template folder so on.
3:21 But all we're gonna do is just call this order API. like so And then down here, where we saw App along with all of its behaviors that it had,
3:32 like, route and so on. We use a blueprint and now we've got blueprint.route and so on.
3:37 This is exactly what we're going to need also over in the home view. this is the home view, sorry.
3:44 They will put home,home have to do the same thing over in the order API, like this. So this will be blueprint.
3:53 A little format. Everything is looking good. Now this is not going to make it run. If I try to run this again,
4:00 I refresh it. Now, the home page 404 not found. What's going on? Well, just because we have these blueprints defined, they're not included in the App.
4:09 So what we have to do is go over here and say from views import home and from api import order_api and then we say
4:21 app.register a blueprint, home .blueprint and you guessed it api.order api.blueprint Okay, clean it up a little. Now,
4:35 if we run it, that's what Hello world works and Oh, yeah, Over here we can go to api/order.
4:43 And we get some random JSON back. Fantastic, as a little bit of a diversion. But I really think you'll find it valuable.
4:52 And we're going to continue to evolve and refine this. You can find it really valuable to take our application and partition it by functionality.
5:00 All the stuff that has to do with a home HTML thing, goes here all the API stuff about ordering cakes goes over here.
5:07 HTML templates go there. Final thing while we're on. This is we can go to Pycharm and say mark this directory as a template
5:14 folder. It seems like we might not need to do that.
5:18 We can already render templates out of their these jinja templates that you're probably familiar with from Flask. But if I do this,
5:25 it does turn on some interesting functionality. It goes over two pycharm and says, Oh, you're working with HTML templates.
5:31 Hold on, Hold on. What flavor of dynamic language do you want to use It says We don't know. So we say, Well, let's go and configure it.
5:39 Wouldn't it be amazing, if what happened when it said, Do you want to configure? It took you to the feature for that.
5:44 Nope. You got to go over here and say, - template, down here under language and frameworks, there's template language and notice
5:53 this is empty, but it can be million or ginger to or jinja 2. Because jinja 2, is the way to do it with flask.
6:00 We're going to pick that. And now if we have an HTML file over there and we interact with it, it's going to give us auto complete colorization,
6:09 syntax highlighting and whatnot for jinja on top of plain HTML. All right now, we've got our App organized in a much nicer way for it
6:18 to grow without growing in complexity.


Talk Python's Mastodon Michael Kennedy's Mastodon