FastAPI: Modern Python Web Development
Bill Lubanovic;
First Edition
About this book
FastAPI is a young yet solid framework that takes advantage of newer Python features in a clean design. As its name implies, FastAPI is indeed fast, rivaling similar frameworks in languages such as Golang. With this practical book, developers familiar with Python will learn how FastAPI lets you accomplish more in less time with less code.
"FastAPI made simple! This book excels at simplifying FastAPI concepts, showcasing the author's mastery. Readers will gain practical knowledge and hit the ground running."
Author Bill Lubanovic covers the nuts and bolts of FastAPI development with how-to guides on various topics such as forms, database access, graphics, maps, and more that will take you beyond the basics. The book also gets you up to speed on RESTful APIs, data validation, authorization, and performance. With its similarities to frameworks like Flask and Django, you'll find it easy to get started with FastAPI., Ganesh Harke Senior Software Engineer, Citibank
Through the course of this book, you will:
- Learn how to build web applications with FastAPI
- Understand the differences between FastAPI, Starlette, and Pydantic
- Learn two features that set FastAPI apart: asynchronous functions and data type checking and validation
- Examine new features of Python 3.8+, especially type annotations
- Understand the differences between sync and async Python
- Learn how to connect with external APIs and services
"This book provides a comprehensive overview of the FastAPI framework and its surrounding ecosystem, giving readers a quick yet comprehensive view of modern web development.", William Jamir Silva Senior Software Engineer, Adjust GmbH
Questions & Answers from this book
Questions and answers are connected to the referenced book and its available source material.
Chapter 1: The Modern Web
How does the chapter explain the difference between concurrency and parallelism?
The chapter explains that concurrency does not mean full parallelism. In a single CPU, concurrency does not involve multiple processes executing in the same nanosecond; instead, it mostly avoids busy waiting by letting the CPU do useful work while waiting for slow responses. Parallelism, by contrast, implies actual simultaneous processing, which is not what concurrency means on one CPU.
What are the five characteristics of a RESTful architecture according to the chapter?
The chapter lists four RESTful characteristics, not five: using HTTP and a client-server protocol, stateless connections, cacheable responses, and resource-based endpoints.
Chapter 2: Modern Python
What two third-party Python packages was FastAPI's design heavily based on, according to Ramírez?
FastAPI's design was heavily based on two third-party Python packages: Starlette for web details and Pydantic for data details.
What is the minimum Python version required for FastAPI, and why?
The minimum Python version required for FastAPI is 3.7. This version includes type hints and asyncio, which are core requirements for FastAPI. The author recommends using at least Python 3.9 for a longer support lifetime.
Chapter Part II. A FastAPI Tour: Part II. A FastAPI Tour
Chapter 4: Async, Concurrency, and Starlette Tour
How does FastAPI handle asynchronous path functions compared to synchronous ones?
FastAPI calls async def path functions on its own async event loop, while synchronous def path functions are run in a threadpool. Async path functions can pause on I/O waits, letting the server handle other requests during that time. A developer does not need to add await when FastAPI invokes an async endpoint.
In FastAPI, how does the framework determine whether a function parameter like 'who' is a path parameter, query parameter, body parameter, or header parameter?
FastAPI checks whether the parameter name appears inside curly braces in the URL pattern of the decorator; if it does, it is a path parameter. If the parameter has a Body() or Header() default, FastAPI reads it from the HTTP body or headers. Otherwise, a plain parameter with no special default is assumed to be a query parameter.
Chapter 5: Pydantic, Type Hints, and Models Tour
What are the three solutions mentioned in the chapter for handling data validation and type checking in Python, and which one does the author choose for FastAPI?
The three solutions are dataclasses, attrs, and Pydantic. The author chooses Pydantic for FastAPI because it is integrated into FastAPI and stands out for validation.
What are the main differences between Pydantic and dataclasses in terms of how they define objects and their validation capabilities?
Pydantic defines models by subclassing BaseModel, while dataclasses use the @dataclass decorator on a plain class. Dataclasses mainly group data and are part of the standard library, whereas Pydantic is third-party and provides built-in validation such as type checks and constraints like min_length. Pydantic stands out for validation, which is why it is commonly used with FastAPI.
You may also be interested in
FastAPI: Modern Python Web Development
Bill Lubanovic;
37 questions
Generative Deep Learning: Teaching Machines to Paint, Write, Compose, and Play
David Foster;
33 questions
AI for the Ordinary_ A Non-technical Playbook for Citizens, Students, and Manage
Unknown
30 questions
AI for Qualitative Research: A Hands-On Guide for Management Scholars
Diana Garcia Quevedo
29 questions