Building data-driven web apps with Flask and SQLAlchemy Transcripts
Chapter: Welcome to the course
Lecture: Course topics
0:00 Let's quickly lay out the topics
0:01 that we're going to cover and add
0:03 just a little bit of background to each one of them.
0:05 So after this chapter, we're going to quickly talk
0:08 about how to set up your machine,
0:09 make sure you have the right version of Python,
0:12 the right editor and also all the starter source code
0:16 or data that you might need to get going.
0:19 Then we're going to talk about the Flask web framework itself
0:22 compared to some of the other frameworks
0:24 like Django, Pyramid, Bottle and so on,
0:27 where does it fit in that world
0:28 and what are some of the core elements
0:30 or building blocks of it.
0:32 We're going to go and create our first Flask site.
0:36 Now, we're going to build a pretty involved
0:39 and very cool app as we get through this course.
0:42 But let's not start there, let's just create
0:44 the hello world equivalent of a Flask site
0:46 and see what all the moving parts are,
0:48 and then build out something more interesting.
0:52 We're going to focus on some of the techniques
0:55 and language features of Jinja templates.
0:59 The idea with Flask is we're going to code
1:01 maybe to a database or a service or something like that,
1:03 pull back some data and we want to render that to HTML.
1:06 But you don't do that all on Python.
1:08 Most of the HTML is fixed.
1:10 You maybe just want to take a list
1:11 and repeat little elements of it as HTML fragments
1:14 or conditionally show or hide things
1:16 like if you're logged in or not logged in.
1:18 So we're going to talk about the Jinja template language
1:21 and how that fits in the Flask.
1:24 One of the first things that happen
1:26 in these MVC, Model-View-Controller frameworks like Flask
1:30 is that a URL request will come in
1:32 both the verb and the URL is going to come in
1:35 and it's up to this thing called routing
1:38 to figure out what function is going to handle that request,
1:42 what Python function.
1:43 So we're going to figure out how to use routing
1:45 to map URLs to our view methods.
1:47 Flask doesn't do anything for making our site look good,
1:51 it just delivers whatever HTML we write.
1:54 So we don't want to have to write all of it.
1:55 We're going to talk about some of the cool
1:57 frontend frameworks,
1:59 Bootstrap as well as a couple of others
2:01 that we could use to make our site look better.
2:04 And we will use some Bootstrap to make out site look good.
2:08 Almost all the interesting web applications
2:10 talk to some kind of data backend,
2:12 and we're going to talk to our relational database.
2:15 So we're going to focus on using SQLAlchemy
2:17 and its ORM to map Python classes to the database.
2:22 So we're going to see how to define those classes,
2:24 how to set up the connections,
2:26 and really do all the queries and inserts and updates,
2:29 everything you need to do to work with SQLAlchemy.
2:31 SQLAlchemy is great for creating
2:34 the initial database structure.
2:35 I have a packages and class, and I talk to SQLAlchemy
2:39 and then create packages table in database.
2:42 But it will not migrate it, it will not change it over time,
2:44 and if those things become out of sync,
2:46 your database class and your database schema,
2:50 and the app will crash and freak out
2:52 and say whoa, whoa, I can't work with this,
2:53 this database is incompatible with my understanding
2:56 of what it should be.
2:57 So in order to evolve our database,
3:00 our database schema,
3:01 as things grow and change in our application,
3:04 we need to use something called migrations.
3:06 So we're going to use something called Alembic,
3:08 which is paired with SQLAlchemy
3:10 to very nicely and in many ways automatically
3:13 evolve our database schema to stay in line
3:16 with our application code.
3:17 Web apps that show data, well they're fun,
3:20 but it's way better if you can accept user input,
3:23 let people search, let them interact with things
3:25 and so on, create accounts, register, what not.
3:28 So we're going to talk about HTML forms
3:31 and how we let users submit data back
3:33 to our Flask application.
3:36 We're also going to see that the internet
3:38 is a dangerous place.
3:40 People will send us invalid data,
3:41 either by mistake or maliciously.
3:44 So we're going to look at a design pattern
3:46 called view models that abstract the way
3:49 or isolate a way the validation for all data
3:52 coming into the server, as well as the data exchange
3:55 with the template.
3:56 It's going to be really, really nice.
3:57 And on the client side, we're going to look
3:59 at some HTML file features that will, with almost no effort,
4:03 provide really great validation
4:05 for at least the browsers.
4:07 We want to make sure that our web app is working
4:09 and stays working.
4:11 So we're going to write some unit tests,
4:13 we're going to use PyTest and some special infrastructure
4:15 that is baked right into Flask
4:17 to help us write testable code.
4:20 You'll see that testing web applications can be tricky.
4:23 We already talked about it, interacting with the database.
4:25 But it also does things like interact
4:27 with the Flask web framework itself.
4:30 It expects a request to come in properly structured
4:33 and so on. So we're going to talk about how we can
4:36 either fake out some of those things
4:38 or use a special testing infrastructure from Flask
4:41 to make sure that we can write our tests in isolation
4:44 the way they're meant to work.
4:46 Once we get out app all working and we want to put it online,
4:48 so we're going to deploy this
4:49 to a Linux cloud virtual machine
4:52 using Nginx, uWSGI and Ubuntu.
4:56 You'll see that not whole lot to it
4:57 and it's really platform to run our web app on.
5:01 And finally, we're going to talk about converting
5:03 our web app to MongoDB.
5:05 As we talked about before, this is interesting
5:08 because MongoDB is a great choice for database backend
5:11 and it has some really great similarities
5:13 to the SQLAlchemy, so it will be quite similar,
5:16 but it also will show you the true power
5:18 of some of our various design patterns
5:20 that we're going to implement,
5:21 and sometimes we're going to call services, our view models,
5:24 and things like that. And we're going to be able to completely swap out
5:27 the entire database style,
5:29 not just implementation but even relational
5:32 versus NoSQL or non-relational databases
5:35 with just changing a couple of files.
5:37 So it's going to be really great.
5:38 It's going to be a great sort of way or grounded style
5:40 and appreciate what we built.
5:42 Also I'll show you how flexible our data access layer
5:44 will have become by this point.
5:48 Well, that's it. This is what we're going to cover.
5:50 I think this is a great set of topics.
5:52 Once you get through all of these,
5:53 you're going to know pretty much everything
5:55 that you need to know to write real true production
5:58 data-driven web apps.
5:59 It's going to be a lot of fun.
6:00 I hope you're excited.
6:01 I'm definitely excited to get started on this with you.