Flask Framework Cookbook - Shalabh Aggarwal - E-Book

Flask Framework Cookbook E-Book

Shalabh Aggarwal

0,0
28,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 318

Veröffentlichungsjahr: 2023

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Flask Framework Cookbook Third Edition

Enhance your Flask skills with advanced techniques and build dynamic, responsive web applications

Shalabh Aggarwal

BIRMINGHAM—MUMBAI

Flask Framework Cookbook

Third Edition

Copyright © 2023 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Pavan Ramchandani

Publishing Product Manager: Bhavya Rao

Senior Editor: Divya Anne Selvaraj

Technical Editor: Joseph Aloocaran

Copy Editor: Safis Editing

Project Coordinator: Sonam Pandey

Proofreader: Safis Editing

Indexer: Manju Arasan

Production Designer: Alishon Mendonca

Marketing Coordinator: Nivedita Pandey

First published: November 2014

Second edition: July 2019

Third edition: June 2023

Production reference: 1300623

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80461-110-4

www.packtpub.com

To my late father, Videsh, who will always be there in my thoughts for the love and encouragement he gave me to explore new things in life.

– Shalabh Aggarwal

Contributors

About the author

Shalabh Aggarwal is a seasoned engineering leader who develops and manages enterprise systems and web and mobile applications for small- and large-scale organizations. He started his career working with Python, and although he now works on multiple technologies, he remains a Python developer at heart. He is passionate about open source technologies and writes highly readable and high-quality code. Throughout his career, he has built software products and engineering teams for companies moving from 0 to 1, 1 to 10, and 10 to 100 levels.

He is also active in voluntary training for engineering students and young engineers on non-conventional and open source topics. When not working with full-time assignments, he consults for start-ups on starting or scaling up while leveraging different technologies. When not writing code, he dedicates his time to his young kid and occasional travels.

I would like to thank my family, my wife, and my mother for putting up with me during my long writing and research sessions. Special thanks to my three-year-old son, Krishiv, for it was his time that was sacrificed the most. Thanks also to the Pocoo and Pallets teams, Armin Ronacher, and the complete Flask community for developing and maintaining the framework that made this series of books possible.

About the reviewers

Vincenzo Antignano, hailing from Naples, Italy, is a software engineer who specializes in finding solutions and leading developers to make successes of products. He currently works at IQVIA.

He has 10 years of experience in Python and has worked extensively with Flask in many projects in the last few years, from leading the dev team that boosted the acclaimed Google Outside Campaign in London to designing and implementing data and APIs for the clinical trial and life science industry.

Rahul Shelke is a seasoned technology professional with over 13 years of experience in software architecture, development, cybersecurity, and DevOps tools. He is passionate about helping start-ups succeed. He is a curious person and a fluid thinker. He likes sharing insights on software, cybersecurity, and personal growth by simplifying complex topics. As a skilled trainer and mentor, he has trained teams and created automated development standards. He advises start-ups on product development, architecture design, team hiring, and coaching, and he has contributed to open source communities. Rahul has also done technical reviews for other Packt Publishing books on web development using Python Flask.

Thank you, Packt Publishing, for giving me the opportunity to do the technical review for this book. A special thanks to the editorial team and coordinator for their constant support and patience. I would also like to thank my family, who have been my unwavering source of support and motivation. Finally, I would like to acknowledge the author of this book for his dedication and hard work. His expertise and passion for the subject matter are evident in every chapter, and it has been an honor to work with him..

Table of Contents

Preface

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

Other Books You May Enjoy

Part 1: Flask Fundamentals

As the name suggests, this part of the book focuses on the basic fundamental building blocks of any Flask web application. A clear understanding of these topics is essential to building scalable, configurable, and extensible web applications with Flask as we move to more complex topics.

Developers usually struggle with how to set up their Flask application configurations to suit different environments, such as development and production. At times, they might find it difficult to choose the best way to structure their Flask applications. The first chapter helps in answering such questions and many more.

The next three chapters focus on creating the foundational pillars of any web application – that is, models, views, and templates. A very important concept covered in these chapters concerns connecting multiple types of databases with your application.

The content in these chapters will be basic- to mid-level in complexity, and it will act as the groundwork to address more complex use cases in the next two parts of the book.

At the end of this first part of the book, you will be able to create a full-fledged Flask application that will be functional and capable of basic functionalities.

This part of the book comprises the following chapters:

Chapter 1, Flask ConfigurationsChapter 2, Templating with JinjaChapter 3, Data Modeling in FlaskChapter 4, Working with Views