Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Introducing the Flask framework
Lecture: Building blocks of Flask
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Before we actually start writing some code let's do a quick high-level flyover of Flask and talk about the building blocks of Flask.
0:08
You're probably itching to write some code, and so am I we're going to do that really, really soon. But I just want to do a quick high-level flyover
0:14
so you know of all the moving parts and you kind of see what's coming and what's going to fit together and how. So let's talk quickly about those.
0:23
First of all, we need to talk about routes. These are URL patterns that are going to match incoming requests. They could be things like
0:29
just forward slash to map over to the main home page they could be /books/7 that'll say show me the book with ID 7, things like that.
0:41
But we're going to set up these patterns and these patterns are going to be defined as routes in Flask and have little placeholders
0:47
where part of the URL could be data, pass to our functions that's going to figure out what to do with it like that 7 in my book example.
0:55
Next the routes are going to figure out which method to run. What part of the code runs for our given URL?
1:02
So we're going to define these things called view methods and this is where we're going to do the main logic of our application
1:08
we're going to process the request maybe talk to the database, call a web service maybe, you know, look at the cookies, who knows.
1:13
But we're going to take the inbound request and do some processing on it and decide what kind of response to send them and things like that.
1:21
Now, Flask and many other frameworks would be what you might call a Model-View-Controller, MVC, framework so in that pattern terminology
1:30
these view methods would be called controllers. Once we have some data, maybe we want to turn it to HTML. Well, we're not going to do that in Python
1:38
that would be a horrible idea. So we want to write mostly HTML files but there's some dynamic nature. Maybe I have a list of books
1:45
and I want to show them to the user. How am I going to generate that list part? Well, there's template languages like Jinja2, Chameleon
1:52
and others that we can pass model data to which will look at that, loop over and generate true HTML that goes to the browser.
2:00
So we're going to work with templates that generate dynamic HTML, and in the MVC world these are views. And finally we have models.
2:07
This is the data passed from the view method over to the template. In Flask, this often looks just like
2:13
keyword arguments to the rendered template function as we'll see in a little bit. We might also pass a dictionary
2:20
when we get to some more advanced patterns further in the course. But the model is sort of all the data that's passed from the view to the template.
2:28
We also have great support for static content. This is really good and important for development it's kind of irrelevant for production.
2:35
In production we would use some other non-Python-backed production-level server like Nginx or something like that
2:44
and that will completely take care of the static content. But, when we get started and we start developing we're going to have static content
2:49
that's part of our world here and Flask has some nice defaults for that. We also have configuration.
2:55
Maybe we want to develop with one set of settings, right maybe using SQLite in some local little test database and then maybe we, in production
3:04
want to use a Postgres server, or something like that. So maybe something like our database connection stream is going to change
3:10
maybe something like how much logging we do is going to change. We also can have a test mode which will make it run in still a different way.
3:19
Maybe we don't have a database at all in test mode we going to mock that out, right? So we have these different configuration settings
3:25
and this can be open-ended and easily extendable. So these are the major building blocks of Flask.