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