About This Title

Pages: 327
Published: February 2025
ISBN: 9798870616056
In Print

The New and Improved Flask Mega-Tutorial

by Miguel Grinberg

The Flask Mega-Tutorial is an overarching tutorial for Python beginner and intermediate developers that teaches web development with the Flask framework. The concepts that are covered go well beyond Flask, including a wide range of topics Python web developers need to know when writing their own applications. The book will take you on a journey through a realistic web development project implemented in Python and Flask, from start to end and in small incremental steps.


This work was written and produced entirely by the author. We are proud to be distributing it.

eBook Formats:

  • PDF for desktop/tablets

  • epub for Apple Books, e-readers

Get all eBook formats here for $24.99 (USD)

Add to Cart we accept visa, mastercard, amex, discover, paypal


The application that you are going to develop under the guidance of this book is a nicely featured microblogging web application that I decided to call Microblog. These are some of the topics that that you are going to learn along the way:

  • User management, including secure password handling, logins, user profiles and avatars.
  • Database management and database migration support
  • Handling of user input via web forms
  • Pagination of long lists of items
  • Full-text search
  • Emailing notifications to users
  • HTML templates
  • Working with dates and times
  • Internationalization and localization
  • Installation on a production server
  • Working with Docker containers
  • Application Programming Interfaces (APIs)
  • Push notifications
  • Background jobs

What You Need

The example code that accompanies this book can be used on any platform on which Python runs, so macOS, Linux and Microsoft Windows are all valid choices. All you need is a recent version of Python 3.

Resources

Releases:

  • P1.0 2025/02/05

Contents & Extracts

Preface

  • Who This Book Is For
  • Requirements
  • About The Example Application
  • How To Work With The Example Code
  • Conventions Used In This Book
  • Acknowledgements

Hello, World!

  • Installing Python
  • Installing Flask
  • A “Hello, World” Flask Application

Templates

  • What Are Templates?
  • Conditional Statements
  • Loops
  • Template Inheritance

Web Forms

  • Introduction to Flask-WTF
  • User Login Form
  • Form Templates
  • Form Views
  • Receiving Form Data
  • Improving Field Validation
  • Generating Links

Database excerpt

  • Databases in Flask
  • Database Migrations
  • Flask-SQLAlchemy Configuration
  • Database Models
  • Creating The Migration Repository
  • The First Database Migration
  • Database Upgrade and Downgrade Workflow
  • Database Relationships
  • Playing with the Database
  • Shell Context

User Logins

  • Password Hashing
  • Introduction to Flask-Login
  • Preparing The User Model for Flask-Login
  • User Loader Function
  • Logging Users In
  • Logging Users Out
  • Requiring Users To Login
  • Showing The Logged-In User in Templates
  • User Registration

Profile Page and Avatars

  • User Profile Page
  • Avatars
  • Using Jinja Sub-Templates
  • More Interesting Profiles
  • Recording The Last Visit Time For a User
  • Profile Editor

Error Handling

  • Error Handling in Flask
  • Debug Mode
  • Custom Error Pages
  • Sending Errors by Email
  • Logging to a File
  • Fixing the Duplicate Username Bug
  • Enabling Debug Mode Permanently

Followers

  • Database Relationships Revisited
  • Representing Followers
  • Database Model Representation
  • Adding and Removing “follows”
  • Obtaining the Posts from Followed Users
  • Combining Own and Followed Posts
  • Unit Testing the User Model
  • Integrating Followers with the Application

Pagination

  • Submission of Blog Posts
  • Displaying Blog Posts
  • Making It Easier to Find Users to Follow
  • Pagination of Blog Posts
  • Page Navigation
  • Pagination in the User Profile Page

Email Support

  • Introduction to Flask-Mail
  • Flask-Mail Usage
  • A Simple Email Framework
  • Requesting a Password Reset
  • Password Reset Tokens
  • Sending a Password Reset Email
  • Resetting a User Password
  • Asynchronous Emails

Facelift

  • CSS Frameworks
  • Introducing Bootstrap
  • Using Bootstrap
  • Rendering Bootstrap Forms
  • Rendering of Blog Posts
  • Rendering Pagination Links
  • Before And After

Dates and Times

  • Timezone Hell
  • Timezone Conversions
  • Introducing Moment.js and Flask-Moment
  • Using Moment.js

I18n and L10n

  • Introduction to Flask-Babel
  • Marking Texts to Translate In Python Source Code
  • Marking Texts to Translate In Templates
  • Extracting Text to Translate
  • Generating a Language Catalog
  • Updating the Translations
  • Translating Dates and Times
  • Command-Line Enhancements

Ajax

  • Server-side vs. Client-side
  • Live Translation Workflow
  • Language Identification
  • Displaying a “Translate” Link
  • Using a Third-Party Translation Service
  • Ajax From The Server
  • Ajax From The Client

A Better Application Structure

  • Current Limitations
  • Blueprints
  • The Application Factory Pattern
  • Unit Testing Improvements
  • Environment Variables
  • Requirements File

Full-Text Search

  • Introduction to Full-Text Search Engines
  • Installing Elasticsearch
  • Elasticsearch Tutorial
  • Elasticsearch Configuration
  • A Full-Text Search Abstraction
  • Integrating Searches with SQLAlchemy
  • Search Form
  • Search View Function

Deployment on Linux

  • Traditional Hosting
  • Creating an Ubuntu Server
  • Using an SSH Client
  • Password-less Logins
  • Securing Your Server
  • Installing Base Dependencies
  • Installing the Application
  • Setting Up MySQL
  • Setting Up Gunicorn and Supervisor
  • Setting Up Nginx
  • Deploying Application Updates
  • Raspberry Pi Hosting

Deployment on Heroku

  • Hosting on Heroku 226
  • Creating a Heroku account
  • Installing the Heroku CLI
  • Setting Up Git
  • Creating a Heroku Application
  • The Ephemeral File System
  • Working with a Heroku Postgres Database
  • Logging to stdout
  • Compiled Translations
  • Elasticsearch Hosting
  • Updates to Requirements
  • The Procfile
  • Deploying the Application
  • Deploying Application Updates

Deployment on Docker Containers

  • Installing Docker
  • Building a Container Image
  • Starting a Container
  • Using Third-Party “Containerized” Services
  • The Docker Container Registry
  • Deployment of Containerized Applications

Some JavaScript Magic

  • Server-side Support
  • Introduction to the Bootstrap Popover Component
  • Executing a Function On Page Load
  • Finding DOM Elements with Selectors
  • Popovers and the DOM
  • Creating the Popover Components
  • Ajax Requests
  • Popover Update

User Notifications

  • Private Messages
  • Static Message Notification Badge
  • Dynamic Message Notification Badge
  • Delivering Notifications to Clients

Background Jobs

  • Introduction to Task Queues
  • Using RQ
  • Database Representation of Tasks
  • Integrating RQ with the Flask Application
  • Sending Emails from the RQ Task
  • Task Helpers
  • Implementing the Export Task
  • Export Functionality in the Application
  • Progress Notifications
  • Deployment Considerations

Application Programming Interfaces (APIs)

  • REST as a Foundation of API Design
  • Implementing an API Blueprint
  • Representing Users as JSON Objects
  • Representing Collections of Users
  • Error Handling
  • User Resource Endpoints
  • API Authentication
  • API Friendly Error Messages
  • A Last Word

Author

Miguel Grinberg has over 25 years of experience as a software engineer. He blogs at miguelgrinberg.com about a variety of topics including Web development, Python, JavaScript and robotics. Miguel currently lives in Ireland.

eBook Formats:

  • PDF for desktop/tablets

  • epub for Apple Books, e-readers

Get all eBook formats here for $24.99 (USD)

Add to Cart we accept visa, mastercard, amex, discover, paypal

Related Titles:

About This Title

Pages: 327
Published: February 2025
ISBN: 9798870616056
Edition: 1
In Print