Flask Framework Cookbook - Third Edition

Book description

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.

Table of contents

  1. Flask Framework Cookbook
  2. Third Edition
  3. Contributors
  4. About the author
  5. About the reviewers
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
    9. Download a free PDF copy of this book
  7. Part 1: Flask Fundamentals
  8. Chapter 1: Flask Configurations
    1. Technical requirements
    2. Setting up a virtual environment
      1. How to do it...
      2. How it works...
      3. There’s more...
      4. See also
    3. Handling basic configurations
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Configuring using class-based settings
      1. How to do it...
      2. How it works...
    5. Organizing static files
      1. How to do it...
      2. How it works...
      3. There’s more...
    6. Being deployment-specific with the instance folder
      1. How to do it...
      2. How it works...
    7. Composition of views and models
      1. How to do it...
      2. How it works...
      3. See also
    8. Creating a modular web app with blueprints
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Making a Flask app installable using setuptools
      1. How to do it...
      2. How it works...
      3. See also
  9. Chapter 2: Templating with Jinja
    1. Technical requirements
    2. Bootstrapping the standard layout
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. Getting ready
      5. How to do it...
      6. How it works...
    3. Creating a custom context processor
      1. How to do it...
    4. Creating a custom Jinja filter
      1. How to do it...
      2. How it works...
      3. See also
    5. Creating a custom macro for forms
      1. Getting ready
      2. How to do it...
    6. Advanced date and time formatting
      1. Getting ready
      2. How to do it...
      3. How it works…
      4. There’s more…
  10. Chapter 3: Data Modeling in Flask
    1. Creating an SQLAlchemy DB instance
      1. Getting ready
      2. How to do it...
      3. There’s more…
      4. See also
    2. Creating a basic product model
      1. How to do it…
      2. How it works...
    3. Creating a relational category model
      1. How to do it...
      2. How it works…
      3. See also
    4. Migrating databases using Alembic and Flask-Migrate
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Indexing model data with Redis
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Opting for the NoSQL way with MongoDB
      1. Getting ready
      2. How to do it...
      3. How it works…
      4. See also
  11. Chapter 4: Working with Views
    1. Writing function-based views and URL routes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    2. Writing class-based views
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    3. Implementing URL routing and product-based pagination
      1. Getting ready
      2. How to do it...
      3. See also
    4. Rendering to templates
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Dealing with XHR requests
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Using decorators to handle requests beautifully
      1. Getting ready
      2. How to do it...
      3. See also
    7. Creating custom 4xx and 5xx error handlers
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    8. Flashing messages for better user feedback
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Implementing SQL-based searching
      1. Getting ready
      2. How to do it...
      3. How it works...
  12. Part 2: Flask Deep Dive
  13. Chapter 5: Web Forms with WTForms
    1. Representing SQLAlchemy model data as a form
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    2. Validating fields on the server side
      1. How to do it...
      2. How it works...
      3. See also
    3. Creating a common form set
      1. How to do it...
      2. How it works...
    4. Creating custom fields and validations
      1. How to do it...
      2. How it works...
      3. There’s more...
    5. Creating a custom widget
      1. How to do it...
      2. How it works...
      3. See also
    6. Uploading files via forms
      1. How to do it...
      2. How it works...
    7. Protecting applications from CSRF
      1. How to do it...
      2. How it works...
  14. Chapter 6: Authenticating in Flask
    1. Creating a simple session-based authentication
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    2. Authenticating using the Flask-Login extension
      1. Getting ready
      2. How to do it...
      3. How it works…
      4. There’s more…
      5. See also
    3. Using Facebook for authentication
      1. Getting ready
      2. How to do it...
      3. How it works…
    4. Using Google for authentication
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Using Twitter for authentication
      1. Getting ready
      2. How to do it...
      3. How it works…
    6. Authenticating with LDAP
      1. Getting ready
      2. How to do it...
      3. How it works…
      4. See also
  15. Chapter 7: RESTful API Building
    1. Creating a class-based REST interface
      1. Getting ready
      2. How to do it...
      3. How it works...
    2. Creating an extension-based REST interface
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Creating a complete RESTful API
      1. Getting ready
      2. How to do it…
      3. How it works…
  16. Chapter 8: Admin Interface for Flask Apps
    1. Creating a simple CRUD interface
      1. Getting ready
      2. How to do it...
      3. How it works...
    2. Using the Flask-Admin extension
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
    3. Registering models with Flask-Admin
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Creating custom forms and actions
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Using a WYSIWYG editor for textarea integration
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    6. Creating user roles
      1. Getting ready
      2. How to do it…
      3. How it works…
  17. Chapter 9: Internationalization and Localization
    1. Adding a new language
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more…
      5. See also
    2. Implementing lazy evaluation and the gettext/ngettext functions
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Implementing the global language-switching action
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
  18. Part 3: Advanced Flask
  19. Chapter 10: Debugging, Error Handling, and Testing
    1. Setting up basic file logging
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more…
      5. See also
    2. Sending emails on the occurrence of errors
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Using Sentry to monitor exceptions
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Debugging with pdb
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    5. Creating application factories
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    6. Creating the first simple test
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    7. Writing more tests for views and logic
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    8. Integrating the nose2 library
      1. Getting ready
      2. How to do it…
      3. See also
    9. Using mocking to avoid external API access
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    10. Determining test coverage
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    11. Using profiling to find bottlenecks
      1. Getting ready
      2. How to do it…
      3. How it works…
  20. Chapter 11: Deployment and Post-Deployment
    1. Deploying with Apache
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    2. Deploying with uWSGI and Nginx
      1. Getting ready
      2. How to do it…
      3. See also
    3. Deploying with Gunicorn and Supervisor
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    4. Deploying with Tornado
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Using S3 storage for file uploads
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Managing and monitoring application performance with New Relic
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    7. Infrastructure and application monitoring with Datadog
      1. Getting ready
      2. How to do it…
      3. See also
  21. Chapter 12: Microservices and Containers
    1. Containerization with Docker
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    2. Orchestrating containers with Kubernetes
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
      5. See also
    3. Going serverless with Google Cloud Run
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    4. Continuous deployment with GitHub Actions
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  22. Chapter 13: GPT with Flask
    1. Technical requirements
    2. Automating text completion using GPT
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Implementing chat using GPT (ChatGPT)
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    4. Generating images using GPT
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  23. Chapter 14: Additional Tips and Tricks
    1. Implementing full-text search with Elasticsearch
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    2. Working with signals
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Using caching with your application
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
      5. See also
    4. Implementing email support
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
      5. See also
    5. Understanding asynchronous operations
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    6. Working with Celery
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  24. Index
    1. Why subscribe?
  25. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Flask Framework Cookbook - Third Edition
  • Author(s): Shalabh Aggarwal
  • Release date: July 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781804611104