Practical Python Backend Programming: Build Flask and FastAPI applications, asynchronous programming, containerization and deploy apps on cloud
By Tim Peters
()
About this ebook
"Practical Python Backend Programming" is a quick pragmatic book that teaches both new and experienced developers the fundamentals of backend development with Python.
Read more from Tim Peters
Mastering Python Network Automation: Automating Container Orchestration, Configuration, and Networking with Terraform, Calico, HAProxy, and Istio Rating: 0 out of 5 stars0 ratingsParallel Python with Dask: Perform distributed computing, concurrent programming and manage large dataset Rating: 0 out of 5 stars0 ratings
Related to Practical Python Backend Programming
Related ebooks
Mastering MEAN Stack: Build full stack applications using MongoDB, Express.js, Angular, and Node.js (English Edition) Rating: 0 out of 5 stars0 ratingsAzure Bicep QuickStart Pro Rating: 0 out of 5 stars0 ratingsPractical OneOps Rating: 0 out of 5 stars0 ratingsGo Programming Cookbook Rating: 0 out of 5 stars0 ratingsSpring Boot 3.0 Crash Course Rating: 0 out of 5 stars0 ratingsInter-Service Communication with Go: Mastering protocols, queues, and event-driven architectures in Go (English Edition) Rating: 0 out of 5 stars0 ratingsPractical C++ Backend Programming Rating: 0 out of 5 stars0 ratingsImplementing GitOps with Kubernetes: Automate, manage, scale, and secure infrastructure and cloud-native applications on AWS and Azure Rating: 0 out of 5 stars0 ratingsMastering Play Framework for Scala Rating: 0 out of 5 stars0 ratingsBuilding the Future: Advanced Web Development Techniques with Flask and Python Rating: 0 out of 5 stars0 ratingsUltimate Microservices with RabbitMQ Rating: 0 out of 5 stars0 ratingsUltimate Docker for Cloud Native Applications Rating: 0 out of 5 stars0 ratingsNoSQL Essentials: Navigating the World of Non-Relational Databases Rating: 0 out of 5 stars0 ratingsMastering GraphQL: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsMastering Secure Java Applications: Navigating security in cloud and microservices for Java (English Edition) Rating: 0 out of 5 stars0 ratingsJetpack Compose 1.7 Essentials: Developing Android Apps with Jetpack Compose 1.7, Android Studio, and Kotlin Rating: 0 out of 5 stars0 ratingsJava Persistence with NoSQL: Revolutionize your Java apps with NoSQL integration (English Edition) Rating: 0 out of 5 stars0 ratingsProfessional JavaScript for Web Developers Rating: 0 out of 5 stars0 ratingsExtending Docker Rating: 5 out of 5 stars5/5Ultimate Git and GitHub for Modern Software Development Rating: 0 out of 5 stars0 ratingsUltimate Flutter Handbook: Learn Cross-Platform App Development with Visually Stunning UIs and Real-World Projects Rating: 0 out of 5 stars0 ratingsUltimate Selenium WebDriver for Test Automation Rating: 0 out of 5 stars0 ratingsRediscovering Redis: Mastering Data Management Rating: 0 out of 5 stars0 ratings
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsLearn SQL in 24 Hours Rating: 5 out of 5 stars5/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsLearn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5PYTHON PROGRAMMING Rating: 4 out of 5 stars4/5iPhone For Dummies Rating: 0 out of 5 stars0 ratingsAutomate the Boring Stuff with Python, 2nd Edition: Practical Programming for Total Beginners Rating: 4 out of 5 stars4/5HTML, CSS, and JavaScript Mobile Development For Dummies Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5
Reviews for Practical Python Backend Programming
0 ratings0 reviews
Book preview
Practical Python Backend Programming - Tim Peters
Practical Python Backend Programming
Build Flask and FastAPI applications, asynchronous programming, containerization and deploy apps on cloud
Tim Peters
Preface
Practical Python Backend Programming
is a quick pragmatic book that teaches both new and experienced developers the fundamentals of backend development with Python. All sorts of developers, from Python programmers to non-Python programmers, full stack developers, and web developers, will find what they need to know to become experts in backend programming in this entire book.
The book covers key topics in backend development, including how to set up stable development environments and how to use virtual environments for better dependency management. With this book, readers will have a firm grasp of Python programming with an emphasis on backend tasks by learning the language's syntax, data structures, and functions.
The book teaches you to create and launch dynamic web apps by providing an in-depth look at web frameworks such as Flask and FastAPI. It teaches SQLAlchemy for efficient data handling and advanced database integration, and it shows to improve applications with databases like PostgreSQL, MySQL, and MongoDB. Strategies for managing concurrent operations and improving performance are also covered in the book, along with asynchronous programming in Python.
Ensuring security is of utmost importance in backend development. This book delves into various authentication methods, secure communication protocols such as HTTPS, and techniques to secure REST APIs. For efficient management of asynchronous tasks and real-time data processing, it also introduces message brokers such as RabbitMQ and Kafka.
The book teaches its readers how to containerize apps and manage them on a large scale by integrating technologies like Docker and Kubernetes. It goes on to talk about how to use serverless architectures, how to use modern tools for continuous integration and deployment, and how to deploy apps to cloud platforms like AWS.
This book claims to teach its readers how to write efficient and readable Python code for robust backend systems and how to build, deploy, and maintain such systems.
In this book you will learn how to:
Build dynamic web apps with strong backend logic using Flask and FastAPI.
Write efficient, well-structured backend code by learning Python's syntax, functions, and best practices.
Make your apps more efficient and scalable by using asynchronous programming techniques.
Investigate Kubernetes and Docker to orchestrate and containerize apps for improved deployment and scalability.
Use AWS's cloud services to deploy apps with guaranteed uptime and lightning-fast performance.
Improve efficiency and compatibility by setting up and managing Python development environments.
Enhance your data handling capabilities by learning to integrate and manipulate databases using SQLAlchemy.
Protect online apps with OAuth and JWT's sophisticated authorization and authentication features.
Efficiently process data in real-time and broker messages with RabbitMQ and Kafka.
Streamline processes, cut down on mistakes, and implement continuous integration and deployment by following best practices.
Prologue
Welcome to Practical Python Backend Programming.
I'm Tim Peters, and in this book, I've simplified my years of software development experience to help you navigate the complex world of backend programming with Python. Whether you're a seasoned Python developer, a non-Python programmer venturing into Python's vast landscape, a full stack developer looking to improve your backend skills, or a new web developer starting out, this book is for you.
The challenges I faced as I transitioned from writing simple scripts to developing robust backend systems inspired me to write this book. I've learned over the years that the backend architecture is the foundation of any successful application. Data processing, security enforcement, and the business logic all take place here. As a result of Python's popularity as a top language for backend development, I saw a need for a resource that could walk developers through every step of the process.
This book begins with the fundamentals of creating a Python development environment optimized for backend development. You'll learn how to set up Python with the appropriate tools and libraries to improve your coding experience. From there, we'll delve deep into the heart of Python programming, where I'll help you master its syntax, data structures, and fundamental concepts, giving you a solid foundation to build on.
We then move on to more advanced topics like web development with popular frameworks like Flask and FastAPI. These chapters are intended to teach you not only how to build applications, but also about the structure and design patterns that result in scalable and maintainable code.
Since data is fundamental to backend programming, this book explores deeply into database management techniques. You'll work with relational databases like PostgreSQL and MySQL, as well as NoSQL options like MongoDB, to understand when and how to use them effectively. SQLAlchemy demonstrates how integrating an ORM can simplify database operations in your applications.
As applications grow, they need to handle more complex tasks like asynchronous operations and real-time data processing. Here, I'll show you how to do asynchronous programming in Python and real-time data handling with Kafka. You'll learn how to manage background tasks with Celery and build scalable applications that can handle large amounts of data without issue.
Security is critical, and this book provides a comprehensive look at how to secure your applications. From JWT and OAuth authentication to API endpoint security and SSL implementation, you'll learn how to effectively protect your application and data.
Finally, no modern backend book is complete without addressing deployment and scalability. You'll learn how to containerize your applications with Docker, manage them with Kubernetes, and deploy them to the cloud via AWS. I'll demonstrate how these technologies work to
gether to create a continuous pipeline from development to deployment.
Practical Python Backend Programming
is more than just a programming guide; it is a path to becoming a skilled backend developer capable of designing and deploying powerful and efficient applications. Join me on this journey and let's create some incredible things with Python.
Copyright © 2024 by GitforGits
All rights reserved. This book is protected under copyright laws and no part of it may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without the prior written permission of the publisher. Any unauthorized reproduction, distribution, or transmission of this work may result in civil and criminal penalties and will be dealt with in the respective jurisdiction at anywhere in India, in accordance with the applicable copyright laws.
Published by: GitforGits
Publisher: Sonal Dhandre
www.gitforgits.com
First Printing: January 2024
Cover Design by: Kitten Publishing
For permission to use material from this book, please contact GitforGits at [email protected].
Content
Preface
GitforGits
Acknowledgement
Chapter 1: Fundamentals of Backend Development
Introduction
Overview of Backend Development
Essential Components of Backend Development
Server
Database
Application Programming Interfaces (API)
Web Framework
Middleware
Caching
Functioning of Backend Components
Role of Python in Backend Development
Versatility and Readability
Comprehensive Standard Library
Frameworks and Tools for Backend Development
Asynchronous Support
Integration Capabilities
Database Connectivity
Community and Resources
Use in AI and Machine Learning
Setting up Development Environment: Python, VS Code, and Linux
Installing Linux
Installing Python
Installing Visual Studio Code (VS Code)
Configuring VS Code for Python
Setting Up a Virtual Environment
Final Steps and Testing
Introduction to Virtual Environments
What is a Virtual Environment?
Why use Virtual Environments?
How to Setup and use Virtual Environments?
Installation
Creating Virtual Environment
Activating Virtual Environment
Installing Packages
Deactivating
Managing Dependencies
Best Practices
Basics of Command Line Interface (CLI)
Understanding the CLI
Basic CLI Commands
Tips for using CLI
Introduction to Version Control with Git
Introduction to Version Control and Git
Basic Git Operations for Backend Development
Installing Git
Configuring Git
Initializing Repository
Cloning Repository
Adding and Committing Files
Branching and Merging
Pushing Changes
Pulling Updates
Handling Merge Conflicts
Using the Git Log
Python Refresher: Syntax, Data Structures, and Functions
Python Syntax
Indentation
Variables
Comments
Data Structures
Lists
Tuples
Sets
Dictionaries
Functions
Defining a Function
Calling a Function
Parameters
Keyword Arguments
Arbitrary Arguments
Lambda Functions
Error Handling
Modules and Packages
Importing Modules
Importing with Aliases
From...Import Statement
Best Practices in Python Programming
Follow the Zen of Python
Adhere to PEP 8
Write Docstrings
Use Python’s Built-in Functions and Libraries
Utilize List Comprehensions and Generator Expressions
Error Handling with Exceptions
Use Context Managers for Resource Management
Keep Functions Small and Focused
Avoid Premature Optimization
Use Version Control
Test your Code
Summary
Chapter 2: Building Your First Web Application with Flask
Introduction
Flask Basics
What is Flask?
Core Features of Flask
Simplicity
Flexibility
Development Server and Debugger
Unicode Based
Documentation
Up and Running with Flask
Routing
Templates
Setting up Flask Environment
Organizing your Project
Setting up Flask
Install Flask
Create Flask Application
Create Simple View
Managing Dependencies with Pip
Configuring Flask App
Create Configuration File
Load Configuration
Running the Flask Application
Flask Command Line
Using a Script
Routing and Views
Defining Routes
Dynamic Routes
HTTP Methods
Constructing URLs
Error Handling
Combining Routes and Views Effectively
Templates and Static Files
Understanding Templates
Managing Static Files
Organizing Static Files
Serving Static Files
Form Handling and File Uploads
Handling Forms in Flask
Create HTML Form
Create Route to Display Form
Handling Form Data
File Uploads
Modifying HTML Form for File Uploads
Handling File Uploads in Flask
Database Integration Basics
Setting up SQLAlchemy with Flask
Install Flask-SQLAlchemy
Configure Application
Defining Models
Creating Database
Interacting with Database
Handling Relationships
Retrieving Related Data
Leveraging Query Options
Introduction to Flask Extensions
Understanding Flask Extensions
Flask-WTF
Flask-SQLAlchemy
Flask-Migrate
Flask-Login
Flask-Mail
Flask-RESTful
Using Flask Extensions
Install the Extension
Import and Initialize
Configure
Deploying Flask Application
Preparing Flask Application for Deployment
Switch to Production Database
Use Environment Variables for Configuration
Remove Debug Mode
Choosing Hosting Service
Setting up Web Server and WSGI Application Server
Using Heroku
Prepare Application
Set up Git Repository
Create Heroku App
Verify Deployment
Summary
Chapter 3: Advanced Flask Development
Introduction
Structuring Larger Applications with Flask Blueprints
What are Flask Blueprints?
Creating and Registering Blueprints
Define Blueprint
Register Blueprint
Structuring Views in Blueprints
Using Blueprints
Flask Application Factory Pattern
Understanding Flask Application Factory Pattern
Implementing Application Factory Pattern
Create Factory Function
Organize Configuration Settings
Register Blueprints and Extensions
Implementing RESTful Services with Flask-RESTful
Introduction to Flask-RESTful
Functioning of Flask-RESTful
Creation of REST API using Flask-RESTful
Testing the API
User Authentication and Authorization
Importance of Authentication, Authorization and Session Management
Authentication
Authorization
Session Management
Implementing Authentication in Flask
Configuring Flask-Login
User Loader Function
Defining User Model
Creating Authentication Routes
Implementing Authorization
Role-Based Access Control (RBAC)
Checking Permissions
Error Handling and Logging
Error Handling in Flask
Handling Application Errors
Handling Exceptions
Logging in Flask
Using Logging for Request Data
Custom Loggers
Performance Optimization Techniques
Database Optimization
Query Optimization
Batch Inserts and Updates
Request Handling Optimization
Efficient Data Serialization
Asynchronous Handlers
Caching Responses
Application and Web Server Configuration
Monitoring and Profiling
Integrating Flask Applications with Docker
Installing Docker
Create a Dockerfile
Create .dockerignore file
Build Docker Image
Run the Docker Container
Testing the Docker Container
Summary
Chapter 4: Introduction to FastAPI
Introduction
Transitioning to FastAPI
Key Differences Between FastAPI and Flask
Performance
Type Hints and Automatic Data Validation
Dependency Injection
Built-in Interactive API Documentation
Modern Python Features
Building RESTful APIs with FastAPI
Defining RESTful Endpoints
Path Parameters and Query Strings
Path Parameters
Query Parameters
Using Request Body
Response Handling
Custom Status Codes
Response Headers
Handling Errors
Dependency Injection
Understanding Dependency Injection in FastAPI
How Dependency Injection Works in FastAPI?
Defining Dependencies
Using Dependencies in Route Handlers
Handling Dependencies in Larger Applications
Advanced Database Integration with SQLAlchemy
Setting up SQLAlchemy with FastAPI
Install Necessary Packages
Configure Database URL
Create Database and Tables
Integrating SQLAlchemy with FastAPI
Database Session Dependency
Performing Database Operations
Asynchronous Handling
Setup Asynchronous Database Connection
Using Asynchronous Queries
Implementing Background Tasks
Basics of Background Tasks in FastAPI
How to Define Background Tasks?
Implementing More Complex Background Operations
Setting up Celery
Define Celery Tasks
Trigger Celery Tasks from FastAPI
FastAPI and Docker Integration
Installing Docker
Creating a Dockerfile for FastAPI
Building the Docker Image
Running FastAPI App in Docker Container
Verify Application
Summary
Chapter 5: Working with Databases
Introduction
MySQL and PostgreSQL
MySQL
PostgreSQL
Choosing Between MySQL and PostgreSQL
Application Requirements
Scalability
Support and Compatibility
MongoDB
Understanding MongoDB and Its Document Model
Documents
Collections
Why Choose MongoDB?
MongoDB's Key Features
Integrating MongoDB
Installation
Connecting to MongoDB
Operations
Database Design Principles
Core Principles of Database Design
Normalization
Entity-Relationship Model
Practical Database Schema Design
SQL Code for Creating Tables
CRUD Operations
Creating Records (CREATE)
Reading Records (READ)
Updating Records (UPDATE)
Deleting Records (DELETE)
Considerations for CRUD Operations
Advanced Querying Techniques
Subqueries
Joins
SQL Aggregate Functions
Advanced Filtering with HAVING
Window Functions
Database Migrations and Version Control
Understanding Database Migrations
Setting up Alembic
Initialize Alembic
Configure Alembic
Creating and Applying Migrations
Create Migration
Edit Migration Script
Apply Migrations
Managing Schema Changes
Versioning
Collaboration
Deployment
Integrating Databases with Python ORMs
Overview of Python ORMs
SQLAlchemy Overview
SQLAlchemy Core
SQLAlchemy ORM
Integrating SQLAlchemy ORM with Python
Installation
Defining Models
Creating Session
Performing Database Operations
Caching Strategies for Database Optimization
Types of Caching
Result Caching
Object Caching
Query Plan Caching
Implementing Caching in Web Applications
Considerations for Effective Caching
Summary
Chapter 6: Asynchronous Programming in Python
Introduction
Asynchronous Programming Overview
What is Asynchronous Programming?
Asynchronous Programming for Backend Development
Asynchronous Programming Use Cases
Web Servers
Data Processing
Real-time Applications
Microservices Architecture
How Asynchronous Programming Works?
Event Loop
Coroutines
Futures and Tasks
Sample Program: Asynchronous Execution
Asyncio Basics
‘async’ Syntax
‘await’ Syntax
Structure of Asyncio Program
Sample Program: Integrate Asyncio in University Application
Developing Asynchronous Web Applications
Sample Program: Asynchronous University Application
Setup and Installation
Defining Asynchronous Endpoints
Asynchronous Database Access
Understanding Asynchronous Database Access
Setup and Installation
Integrating Asynchronous Database Operations in FastAPI
Configuration
Asynchronous Database Functions
Using Asynchronous Functions in Routes
Implementing Websockets
Understanding WebSockets
Implementing WebSockets in FastAPI
Setup and Basic WebSocket Endpoint
Install FastAPI