Introduction to the Flask Microframework

This course teaches the basics of web development with the Flask microframework. It will teach you everything you need to become a productive Python web developer with this new and popular framework.
Course info
Rating
(187)
Level
Intermediate
Updated
Dec 26, 2014
Duration
3h 57m
Table of contents
Course Overview
Getting Started
Basic Templates and Views
Forms and View Logic
Persistence
Users and Authentication
Managing Bookmarks
When Your App Grows
Description
Course info
Rating
(187)
Level
Intermediate
Updated
Dec 26, 2014
Duration
3h 57m
Description

Flask is a relatively new framework that has taken the Python web development community by storm: in a short time it became one of the most popular frameworks around. It offers a lot of flexibility and clean code with a lot of extensibility. You won't feel dragged down by a huge framework that tells you what to do; instead, you'll feel free, productive, and creative!

About the author
About the author

After years of working in software development, Reindert-Jan Ekker has decided to pursue another passion of his: education. He currently works as a college professor of Computer Science in the Netherlands, teaching many subjects like web development, algorithms and data structures and Scrum.

More from the author
Python Best Practices for Code Quality
Intermediate
1h 15m
May 17, 2019
Pandas Playbook: Visualization
Intermediate
2h 11m
Oct 9, 2018
More courses by Reindert-Jan Ekker
Section Introduction Transcripts
Section Introduction Transcripts

Getting Started
Hi, my name is Reindert-Jan Ekker and welcome to this module in which we'll get started with the first simple Flask project. In this module we'll get everything up and running for our first simple Flask project. Of course we'll need Python and then we'll need to obtain and install Flask and several other tools and libraries as well. For that we're going to use a tool call pip. With that done, we'll set up our development environment with another tool called virtualenv. Then we'll be ready to create our first simple application. And I'll give you a nice short overview of what Flask has to offer.

Basic Templates and Views
Hi I'm Reindert-Jan Ekker and welcome to this module in which we'll start a new Flask project and to implement some basic templates and views. What can you expect from this module? Well we're going to be starting a new Flask project called thermos and that's going to be a simple social bookmarking site like Delicious. It's what we'll be working on for the rest of this course. To this project we'll be adding some simple pages starting with an index page. The HTML for that will be generated using Jinga 2, the template engine that comes included with Flask. We'll also use CSS to add some styling and we'll see how to serve CSS files using the static content feature of Flask. We're also going to add a second page containing a form where users will be able to add their bookmarks. For now we'll just create the page so we're not going to add any logic to process user input. In other words, in this module we're going to focus on generating HTML and we'll see how to use template inheritance to make sure both pages will be styled uniformly without having to duplicate any code. And how to create maintainable links in our pages with a Flask function called URL for. Then finally we'll add some custom error pages to the project.

Forms and View Logic
Welcome, my name is Reindert-Jan Ekker and in this module we'll add some logic to our views and forms. So in this module we're going to expand on forms. We'll see how to make our life a lot easier with the Flask-WTF extension. It will help us greatly with generating the HTML for forms and validating and processing the user inputs. We'll also see how to handle forms from a view function. We'll see how to get the input when it's sent through the HTTP post method and how to redirect to another page after a form has been submitted successfully. There's also a Flask feature called message flashing, which we'll use to notify users about the results of their actions.

Persistence
Hi, I'm Reindert-Jan Ekker, and in this module we'll see how to persist the data for our Flask application. What can we expect from this module? Well the main topic is persistence, and that means we will be storing bookmarks and user profiles in a database. In the model template view pattern, the classes we used to represent this data are called models. Now Flask doesn't provide any standard component for writing models, but I'm going to use a library called SQLAlchemy, which is probably the most popular tool for mapping classes to database tables. We call such a tool an object relational mapper or ORM. We'll only have to write Python classes and methods, and SQLAlchemy will generate the corresponding SQL queries. In this module we'll only see some very simple relations and queries, and I'll show you some more advanced use cases later. We'll also use another Flask extension called Flask-script that will help us with database administration tasks. Now one thing we're going to see along the way is how to make our app, which is slowly becoming larger and harder to maintain, into separate Python files.

Users and Authentication
Welcome. I'm Reindert-Jan Ekker, and in this module we'll add user sign up and authentication to our projects. In the previous module we added a fake function that always returns the same logged in user. In this module we'll fix that by implementing real login authentication. We'll start with installing and configuring another Flask extension called Flask-Login. We also need to do a small adaptation to the User class to make it work with that extension. The next thing is to decorate all views for which we want users to be logged in with a decorator provided by Flask-Login, and that decorator is called login_required. Of course, that means we'll have to add a mechanism for the user to actually log in, so we'll add a login view with a form and a template, and of course we shouldn't forget a logout counterpart to that. Finally we have to enable users to sign up for the site, and that of course also entails adding a view, a form, and a template.

Managing Bookmarks
Hi, my name is Reindert-Jan Ekker and in this module we'll add some features to our site for editing and deleting bookmarks. Along the way we'll learn some slightly more advanced techniques. We have now pretty much seen the basics of setting up a site with Flask, let's move on to some more advanced techniques. We'll implement four things. First we're going to fix the layout of the bookmark list, then we're going to add pages for deleting and editing bookmarks. First we'll add the edit page, and the delete page will be at the very end of the module. We'll be adding text to bookmarks, and while implementing these things we'll come across some new and advanced things. These can be roughly separated into two categories. First we'll have things that have to do with templates. So these will have to do with the Jinja2 template engine, we'll see how to use Jinja filters to do some new Operations on our data. We'll create a partial template that lays out a single bookmark and then include that in our bookmark list. We'll learn about something called a context processor that lets us inject things into the template context, and we'll get to use some JavaScript in a template. The other category concerns the database. To add text to bookmarks we need to implement a many-to-many relation. And that means the database model changes, and we can use a flask extension to make our life easier and do automated database migration. So now you know what to expect, let's get started by fixing the layout of our bookmark_list.

When Your App Grows
Hi, my name is Reindert-Jan Ekker and in this last module I'll discuss various topics that become more and more important as your app grows larger. So with the last couple of modules we've seen our tiny app slowly becoming larger and larger. And as it grows things like debugging become more complicated and we'll want to add unit testing for better quality control. We'll start with adding a nice tool to make debugging easier, but to implement unit testing in a convenient way we need to do some more work. First, we'll have to split our application into modules called blueprints. That adds some nice structure to the project, and it also paves the way for two other structural changes, namely creating our app object with a factory function, which as we'll see is necessary to dynamically configure our project according to different scenarios. And this means we'll be able to, for example, use a separate database file for unit testing or to turn debug mode on and off depending on the context. So let's get started with the first small step, which is adding a debug toolbar.