Design and deploy robust state-of-the-art web applications using Flask 2.x and Python 3 frameworks and libraries for streamlined development and optimal performance Purchase of the print or Kindle book includes a free PDF eBook Key Features: A practical and rich companion guide for web developers, offering real-world situations and use cases to learn Flask Get the most out of the powerful Flask framework while preserving the flexibility of your design choices Write cleaner, testable, and maintainable code with the help of sample apps Book Description: Discover what makes Flask, the lightweight Python web framework, popular, as you delve into its modular design that enables the development of scalable web apps. With this practical guide, you'll explore modern solutions, recommended design patterns, and best practices for Flask web development. Updated to the latest version of Flask and Python, this third edition of the Flask Framework Cookbook moves away from the outdated libraries, updates content to incorporate new coding patterns, and introduces recipes for the latest tools. You'll explore different ways to integrate with GPT to build AI-ready Flask applications. The book starts with an exploration of Flask application configurations and then guides you through working with templates and understanding the ORM and view layers. You'll also be able to write an admin interface and get to grips with testing using the factory pattern, debugging, and logging errors. Then you'll discover different ways of using Flask to create, deploy, and manage microservices using AWS, GCP, and Kubernetes. Finally, you'll gain insights into various deployment and post-deployment techniques for platforms such as Apache, Tornado, and Datadog. By the end of this book, you'll have acquired the knowledge necessary to write Flask applications that cater to a wide range of use cases in the best possible way and scale them using standard industry practices. What You Will Learn: Explore advanced templating and data modeling techniques Discover effective debugging, logging, and error-handling techniques in Flask Work with different types of databases, including RDBMS and NoSQL Integrate Flask with different technologies such as Redis, Sentry, and Datadog Deploy and package Flask applications with Docker and Kubernetes Integrate GPT with your Flask application to build future-ready platforms Implement continuous integration and continuous deployment (CI/CD) to ensure efficient and consistent updates to your Flask web applications Who this book is for: If you are a web developer seeking to expand your knowledge of developing scalable and production-ready applications in Flask, this is the book for you. It is also highly valuable if you are already aware of Flask's major extensions and want to leverage them for better application development. This book will come handy as a quick reference for specific topic on Flask, its popular extensions, or specific use cases. It assumes basic Python programming experience, as well as familiarity with web development and related terminology.
Author(s): Shalabh Aggarwal
Edition: 3
Publisher: Packt Publishing Pvt Ltd
Year: 2023
Language: English
Pages: 449
Flask Framework Cookbook
Third Edition
Contributors
About the author
About the reviewers
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Share Your Thoughts
Download a free PDF copy of this book
Part 1: Flask Fundamentals
1
Flask Configurations
Technical requirements
Setting up a virtual environment
How to do it...
How it works...
There’s more...
See also
Handling basic configurations
Getting ready
How to do it...
How it works...
Configuring using class-based settings
How to do it...
How it works...
Organizing static files
How to do it...
How it works...
There’s more...
Being deployment-specific with the instance folder
How to do it...
How it works...
Composition of views and models
How to do it...
How it works...
See also
Creating a modular web app with blueprints
Getting ready
How to do it...
How it works...
Making a Flask app installable using setuptools
How to do it...
How it works...
See also
2
Templating with Jinja
Technical requirements
Bootstrapping the standard layout
Getting ready
How to do it...
How it works...
Getting ready
How to do it...
How it works...
Creating a custom context processor
How to do it...
Creating a custom Jinja filter
How to do it...
How it works...
See also
Creating a custom macro for forms
Getting ready
How to do it...
Advanced date and time formatting
Getting ready
How to do it...
How it works…
There’s more…
3
Data Modeling in Flask
Creating an SQLAlchemy DB instance
Getting ready
How to do it...
There’s more…
See also
Creating a basic product model
How to do it…
How it works...
Creating a relational category model
How to do it...
How it works…
See also
Migrating databases using Alembic and Flask-Migrate
Getting ready
How to do it...
How it works...
See also
Indexing model data with Redis
Getting ready
How to do it...
How it works...
Opting for the NoSQL way with MongoDB
Getting ready
How to do it...
How it works…
See also
4
Working with Views
Writing function-based views and URL routes
Getting ready
How to do it...
How it works...
There’s more...
Writing class-based views
Getting ready
How to do it...
How it works...
There’s more...
See also
Implementing URL routing and product-based pagination
Getting ready
How to do it...
See also
Rendering to templates
Getting ready
How to do it...
How it works...
See also
Dealing with XHR requests
Getting ready
How to do it...
How it works...
Using decorators to handle requests beautifully
Getting ready
How to do it...
See also
Creating custom 4xx and 5xx error handlers
Getting ready
How to do it...
How it works...
There’s more...
Flashing messages for better user feedback
Getting ready
How to do it...
How it works...
Implementing SQL-based searching
Getting ready
How to do it...
How it works...
Part 2: Flask Deep Dive
5
Web Forms with WTForms
Representing SQLAlchemy model data as a form
Getting ready
How to do it...
How it works...
See also
Validating fields on the server side
How to do it...
How it works...
See also
Creating a common form set
How to do it...
How it works...
Creating custom fields and validations
How to do it...
How it works...
There’s more...
Creating a custom widget
How to do it...
How it works...
See also
Uploading files via forms
How to do it...
How it works...
Protecting applications from CSRF
How to do it...
How it works...
6
Authenticating in Flask
Creating a simple session-based authentication
Getting ready
How to do it...
How it works...
See also
Authenticating using the Flask-Login extension
Getting ready
How to do it...
How it works…
There’s more…
See also
Using Facebook for authentication
Getting ready
How to do it...
How it works…
Using Google for authentication
Getting ready
How to do it…
How it works…
Using Twitter for authentication
Getting ready
How to do it...
How it works…
Authenticating with LDAP
Getting ready
How to do it...
How it works…
See also
7
RESTful API Building
Creating a class-based REST interface
Getting ready
How to do it...
How it works...
Creating an extension-based REST interface
Getting ready
How to do it…
How it works…
See also
Creating a complete RESTful API
Getting ready
How to do it…
How it works…
8
Admin Interface for Flask Apps
Creating a simple CRUD interface
Getting ready
How to do it...
How it works...
Using the Flask-Admin extension
Getting ready
How to do it…
How it works…
There’s more…
Registering models with Flask-Admin
Getting ready
How to do it…
How it works…
Creating custom forms and actions
Getting ready
How to do it…
How it works…
Using a WYSIWYG editor for textarea integration
Getting ready
How to do it…
How it works…
See also
Creating user roles
Getting ready
How to do it…
How it works…
9
Internationalization and Localization
Adding a new language
Getting ready
How to do it...
How it works...
There’s more…
See also
Implementing lazy evaluation and the gettext/ngettext functions
Getting ready
How to do it…
How it works…
Implementing the global language-switching action
Getting ready
How to do it…
How it works…
There’s more…
Part 3: Advanced Flask
10
Debugging, Error Handling, and Testing
Setting up basic file logging
Getting ready
How to do it...
How it works...
There’s more…
See also
Sending emails on the occurrence of errors
Getting ready
How to do it…
How it works…
Using Sentry to monitor exceptions
Getting ready
How to do it…
How it works…
Debugging with pdb
Getting ready
How to do it…
How it works…
See also
Creating application factories
Getting ready
How to do it…
How it works…
See also
Creating the first simple test
Getting ready
How to do it…
How it works…
See also
Writing more tests for views and logic
Getting ready
How to do it…
How it works…
See also
Integrating the nose2 library
Getting ready
How to do it…
See also
Using mocking to avoid external API access
Getting ready
How to do it…
How it works…
See also
Determining test coverage
Getting ready
How to do it…
How it works…
See also
Using profiling to find bottlenecks
Getting ready
How to do it…
How it works…
11
Deployment and Post-Deployment
Deploying with Apache
Getting ready
How to do it…
How it works…
See also
Deploying with uWSGI and Nginx
Getting ready
How to do it…
See also
Deploying with Gunicorn and Supervisor
Getting ready
How to do it…
How it works…
See also
Deploying with Tornado
Getting ready
How to do it…
How it works…
Using S3 storage for file uploads
Getting ready
How to do it…
How it works…
Managing and monitoring application performance with New Relic
Getting ready
How to do it…
How it works…
See also
Infrastructure and application monitoring with Datadog
Getting ready
How to do it…
See also
12
Microservices and Containers
Containerization with Docker
Getting ready
How to do it…
How it works…
See also
Orchestrating containers with Kubernetes
Getting ready
How to do it…
How it works…
There’s more…
See also
Going serverless with Google Cloud Run
Getting ready
How to do it…
How it works…
See also
Continuous deployment with GitHub Actions
Getting ready
How to do it…
How it works…
See also
13
GPT with Flask
Technical requirements
Automating text completion using GPT
Getting ready
How to do it…
How it works…
See also
Implementing chat using GPT (ChatGPT)
Getting ready
How to do it…
How it works…
See also
Generating images using GPT
Getting ready
How to do it…
How it works…
See also
14
Additional Tips and Tricks
Implementing full-text search with Elasticsearch
Getting ready
How to do it…
How it works…
See also
Working with signals
Getting ready
How to do it…
How it works…
See also
Using caching with your application
Getting ready
How to do it…
How it works…
There’s more…
See also
Implementing email support
Getting ready
How to do it…
How it works…
There’s more…
See also
Understanding asynchronous operations
Getting ready
How to do it…
How it works…
See also
Working with Celery
Getting ready
How to do it…
How it works…
See also
Index
Why subscribe?
Other Books You May Enjoy
Packt is searching for authors like you
Share Your Thoughts
Download a free PDF copy of this book