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