Book Image

Building Data Science Applications with FastAPI

By : François Voron
5 (1)
Book Image

Building Data Science Applications with FastAPI

5 (1)
By: François Voron

Overview of this book

FastAPI is a web framework for building APIs with Python 3.6 and its later versions based on standard Python-type hints. With this book, you’ll be able to create fast and reliable data science API backends using practical examples. This book starts with the basics of the FastAPI framework and associated modern Python programming language concepts. You'll be taken through all the aspects of the framework, including its powerful dependency injection system and how you can use it to communicate with databases, implement authentication and integrate machine learning models. Later, you’ll cover best practices relating to testing and deployment to run a high-quality and robust application. You’ll also be introduced to the extensive ecosystem of Python data science packages. As you progress, you’ll learn how to build data science applications in Python using FastAPI. The book also demonstrates how to develop fast and efficient machine learning prediction backends and test them to achieve the best performance. Finally, you’ll see how to implement a real-time face detection system using WebSockets and a web browser as a client. By the end of this FastAPI book, you’ll have not only learned how to implement Python in data science projects but also how to maintain and design them to meet high programming standards with the help of FastAPI.
Table of Contents (19 chapters)
1
Section 1: Introduction to Python and FastAPI
7
Section 2: Build and Deploy a Complete Web Backend with FastAPI
13
Section 3: Build a Data Science API with Python and FastAPI

Creating a WebSocket with FastAPI

Thanks to Starlette, FastAPI has built-in support to serve WebSockets. As we'll see, defining a WebSocket endpoint is quick and easy, and we'll be able to get started in minutes. However, things will get more complex as we try to add more features to our endpoint logic. Let's start simple, with a WebSocket that waits for messages and simply echoes them back.

In the following example, you'll see the implementation of such a simple case:

app.py

from fastapi import FastAPI, WebSocket
from starlette.websockets import WebSocketDisconnect
 
app = FastAPI()
 
 
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    try:
        while True:
            data = await websocket.receive_text()
       ...