Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Building Data Science Applications with FastAPI
Building Data Science Applications with FastAPI

Building Data Science Applications with FastAPI: Develop, manage, and deploy efficient machine learning applications with Python

eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Building Data Science Applications with FastAPI

Chapter 1: Python Development Environment Setup

Before we can get started on our FastAPI journey, we need to configure a clean and efficient Python environment. This chapter will show you the best practices and conventions that Python developers use daily to run their projects.

By the end of this chapter, you'll be able to run Python projects and install third-party dependencies in a contained environment that won't raise conflicts if you happen to work on another project that uses different versions of the Python language or different dependencies.

In this chapter, we're going to cover the following main topics:

  • Installing a Python distribution using pyenv
  • Creating a Python virtual environment
  • Installing Python packages with pip
  • Installing the HTTPie command-line utility

Technical requirements

Throughout this book, we'll assume you have access to a Unix-based environment, such as a Linux distribution or macOS.

If they haven't done so already, macOS users should install the Homebrew package (https://brew.sh), which helps a lot in installing command-line tools.

If you are a Windows user, you should enable Windows Subsystem for Linux (https://docs.microsoft.com/windows/wsl/install-win10), WSL, and install a Linux distribution (such as Ubuntu) that will run alongside the Windows environment, which should give you access to all the required tools. There are currently two versions of WSL, WSL and WSL2. Depending on your Windows version, you might not be able to install the newest version. However, we do recommend using WSL2 if your Windows installation supports it.

Installing a Python distribution using pyenv

Python is already bundled with most Unix environments. To ensure this is the case, you can run this command in a command line to show the version of the currently installed Python:

$ python3 --version

The output version displayed will vary depending on your system. You may think that this is enough to get started, but it poses an important issue: you can't choose the Python version for your project. Each Python version introduces new features and breaking changes. Thus, it's important to be able to switch to a recent version for new projects to take advantage of the new features but still be able to run older projects that may not be compatible. This is why we need pyenv.

pyenv (https://github.com/pyenv/pyenv) is a tool that helps you manage and switch between multiple Python versions on your system. It allows you to set a default Python version for your whole system but also per project.

Beforehand, you need to install several build dependencies on your system to allow pyenv to compile Python on your system. The official documentation provides clear guidance on this (https://github.com/pyenv/pyenv/wiki#suggested-build-environment), but here are the commands you should run:

  1. Install the build dependencies:
    • macOS users, use this:
      $ brew install openssl readline sqlite3 xz zlib
    • Ubuntu users, use this:
      $ sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

      Package managers

      Brew and APT are what are commonly known as package managers. Their role is to automate the installation and management of software on your system. Thus, you don't have to worry about where to download them and how to install and uninstall them. The commands just tell the package manager to update its internal package index and then install the list of required packages.

  2. Install pyenv:
    $ curl https://pyenv.run | bash

    Tip

    If you are a macOS user, you can also install it with Homebrew: brew install pyenv.

  3. This will download and execute an installation script that will handle everything for you. At the end, it'll prompt you with some instructions to add some lines to your shell scripts so that pyenv is discovered properly by your shell:

a. Open your ~/.profile script in nano, a simple command-line text editor:

$ nano ~/.profile

b. Add the following lines before the block containing ~/.bashrc:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"

c. Save by using the keyboard shortcut Ctrl + O and confirm by pressing Enter. Then, quit by using the keyboard shortcut Ctrl + X.

d. Open your ~/.bashrc script in nano. If you are using zsh instead of Bash (the default on the latest macOS), the file is named ~/.zshrc:

$ nano ~/.bashrc

e. Add the following line at the end:

eval "$(pyenv init -)"

f. Save by using the keyboard shortcut Ctrl + O and confirm by pressing Enter. Then, quit by using the keyboard shortcut Ctrl + X.

  1. Reload your shell configuration to apply those changes:
    $ source ~/.profile && exec $SHELL
  2. If everything went well, you should now be able to invoke the pyenv tool:
    $ pyenv
    pyenv 1.2.21
    Usage: pyenv <command> [<args>]
  3. We can now install the Python distribution of our choice. Even though FastAPI is compatible with Python 3.6 and later, we'll use Python 3.7 throughout this book, which has more mature handling of the asynchronous paradigm. All the examples in the book were tested with this version but should work flawlessly with newer versions. Let's install Python 3.7:
    $ pyenv install 3.7.10

This may take a few minutes since your system will have to compile Python from the source.

  1. Finally, you can set the default Python version with the following command:
    $ pyenv global 3.7.10

This will tell your system to always use Python 3.7.10 by default, unless specified otherwise in a specific project.

  1. To make sure everything is in order, run the following command to check the Python version that is invoked by default:
    $ python --version
    Python 3.7.10

Congratulations! You can now handle any version of Python on your system and switch it whenever you like!

Creating a Python virtual environment

As for many programming languages of today, the power of Python comes from the vast ecosystem of third-party libraries, including FastAPI, of course, that help you build complex and high-quality software very quickly. The Python Package Index (https://pypi.org), PyPi, is the public repository that hosts all those packages. This is the default repository that will be used by the built-in Python package manager, pip.

By default, when you install a third-party package with pip, it will install it for the whole system. This is different from some other languages, such as Node.js' npm, which by default creates a local directory for the current project to install those dependencies. Obviously, this may cause issues when you work on several Python projects with dependencies that have conflicting versions. It also makes it difficult to retrieve only the dependencies necessary to deploy a project properly on a server.

This is why Python developers generally use virtual environments. Basically, a virtual environment is just a directory in your project containing a copy of your Python installation and the dependencies of your project. It's quite similar to the node_modules directory in Node.js. This pattern is so common that the tool to create them is bundled with Python:

  1. Create a directory that will contain your project:
    $ mkdir fastapi-data-science
    $ cd fastapi-data-science

    Tip

    If you are on Windows with WSL, we recommend that you create your working folder on the Windows drive rather than the virtual filesystem of the Linux distribution. It'll allow you to edit your source code files in Windows with your favorite text editor or IDE while running them in Linux.

    To do this, you can actually access your C: drive in the Linux command line through /mnt/c. You can thus access your personal documents using the usual Windows path, for example, cd /mnt/c/Users/YourUsername/Documents.

  2. You can now create a virtual environment:
    $ python -m venv

Basically, this command tells Python to run the venv package of the standard library to create a virtual environment in the venv directory. The name of this directory is a convention, but you can choose another name if you wish.

  1. Once this is done, you have to activate this virtual environment. It'll tell your shell session to use the Python interpreter and the dependencies in the local directory instead of the global ones. Simply run the following command:
    $ source venv/bin/activate

After doing this, you may notice that the prompt adds the name of the virtual environment:

(venv) $

Remember that the activation of this virtual environment is only available for the current session. If you close it or open other command prompts, you'll have to activate it again. This is quite easy to forget, but it will become natural after some practice with Python.

You are now ready to install Python packages safely in your project!

Installing Python packages with pip

As we said earlier, pip is the built-in Python package manager that will help us install third-party libraries. To get started, let's install FastAPI and Uvicorn:

$ pip install fastapi uvicorn[standard]

We'll talk about it in later chapters, but Uvicorn is required to run a FastAPI project.

Tip

You have probably noticed the word standard inside square brackets just after uvicorn. Sometimes, some libraries have sub-dependencies that are not required to make the library work. Usually, they are needed for optional features or specific project requirements. The square brackets are here to indicate that we want to install the standard sub-dependencies of uvicorn.

To make sure the installation worked, we can open a Python interactive shell and try to import the FastAPI package:

$ python
>>> from fastapi import FastAPI

If it passes without any errors, congratulations, FastAPI is installed and ready to use!

Installing the HTTPie command-line utility

Before getting into the heart of the topic, there is one last tool that we'll install. FastAPI is, as you probably know, mainly about building REST APIs. To do so, you have several options:

  • FastAPI automatic documentation (we'll talk about this later in the book)
  • Postman, a GUI tool to perform HTTP requests
  • cURL, the well-known and widely used command-line tool to perform network requests

Even if visual tools are nice and easy to use, they sometimes lack some flexibility and may not be as productive as command-line tools. On the other hand, cURL is a very powerful tool with thousands of options but can be complex and verbose for testing simple REST APIs.

This is why we'll introduce HTTPie, a command-line tool aimed at making HTTP requests with an intuitive syntax, JSON support, and syntax highlighting. It's available to install from most package managers:

  • macOS users, use this:
    $ brew install httpie
  • Ubuntu users, use this:
    $ sudo apt-get update; sudo apt-get install httpie

Let's see how to perform simple requests on a dummy API:

  1. First, let's retrieve data:
    $ http GET https://603cca51f4333a0017b68509.mockapi.io/todos
    HTTP/1.1 200 OK
    Content-Length: 195
    Content-Type: application/json
    [
        {
            "id": "1",
            "text": "Island"
        }
    ]

As you can see, you can invoke HTTPie with the http command and simply type the HTTP method and the URL. It outputs both the HTTP headers and the JSON body in a clean and formatted way.

  1. HTTPie also supports sending JSON data in a request body very quickly without having to format the JSON yourself:
    $ http -v POST https://603cca51f4333a0017b68509.mockapi.io/todos text="My new task"
    POST /todos HTTP/1.1
    Accept: application/json, */*;q=0.5
    User-Agent: HTTPie/2.3.0
    {
        "text": "My new task"
    }
    HTTP/1.1 201 Created
    Content-Length: 31
    Content-Type: application/json
    {
        "id": "6",
        "text": "My new task"
    }

By simply typing the property name and its value separated by =, HTTPie will understand that it's part of the request body in JSON. Notice here that we specified the -v option, which tells HTTPie to output the request before the response, which is very useful to check that we properly specified the request.

  1. Finally, let's see how we can specify request headers:
    $ http -v GET https://603cca51f4333a0017b68509.mockapi.io/todos "My-Header: My-Header-Value"
    GET /todos HTTP/1.1
    Accept: */*
    My-Header: My-Header-Value
    User-Agent: HTTPie/2.3.0
    HTTP/1.1 200 OK
    Content-Length: 227
    Content-Type: application/json
    [
        {
            "id": "1",
            "text": "Island"
        }
    ]

That's it! Just type your header name and value separated by a colon to tell HTTPie it's a header.

Summary

You now have all the tools and setup required to confidently run the examples of this book and all your future Python projects. Understanding how to work with pyenv and virtual environments is a key skill to ensure everything goes smoothly when you switch to another project or when you have to work on somebody else's code. You also learned how to install third-party Python libraries using pip. Finally, you saw how to use HTTPie, a simple and efficient way to run HTTP queries that will make you more productive while testing your REST APIs.

In the next chapter, we'll highlight some of Python's peculiarities as a programming language and get a grasp of what it means to be Pythonic.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Cover the concepts of the FastAPI framework, including aspects relating to asynchronous programming, type hinting, and dependency injection
  • Develop efficient RESTful APIs for data science with modern Python
  • Build, test, and deploy high performing data science and machine learning systems with FastAPI

Description

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.

Who is this book for?

This Python data science book is for data scientists and software developers interested in gaining knowledge of FastAPI and its ecosystem to build data science applications. Basic knowledge of data science and machine learning concepts and how to apply them in Python is recommended.

What you will learn

  • Explore the basics of modern Python and async I/O programming
  • Get to grips with basic and advanced concepts of the FastAPI framework
  • Implement a FastAPI dependency to efficiently run a machine learning model
  • Integrate a simple face detection algorithm in a FastAPI backend
  • Integrate common Python data science libraries in a web backend
  • Deploy a performant and reliable web backend for a data science application

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 08, 2021
Length: 426 pages
Edition : 1st
Language : English
ISBN-13 : 9781801074186
Vendor :
Google
Category :
Languages :
Concepts :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Oct 08, 2021
Length: 426 pages
Edition : 1st
Language : English
ISBN-13 : 9781801074186
Vendor :
Google
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 110.97
Data Science Projects with Python
€29.99
Building Data Science Applications with FastAPI
€41.99
Time Series Analysis with Python Cookbook
€38.99
Total 110.97 Stars icon
Banner background image

Table of Contents

18 Chapters
Section 1: Introduction to Python and FastAPI Chevron down icon Chevron up icon
Chapter 1: Python Development Environment Setup Chevron down icon Chevron up icon
Chapter 2: Python Programming Specificities Chevron down icon Chevron up icon
Chapter 3: Developing a RESTful API with FastAPI Chevron down icon Chevron up icon
Chapter 4: Managing Pydantic Data Models in FastAPI Chevron down icon Chevron up icon
Chapter 5: Dependency Injections in FastAPI Chevron down icon Chevron up icon
Section 2: Build and Deploy a Complete Web Backend with FastAPI Chevron down icon Chevron up icon
Chapter 6: Databases and Asynchronous ORMs Chevron down icon Chevron up icon
Chapter 7: Managing Authentication and Security in FastAPI Chevron down icon Chevron up icon
Chapter 8: Defining WebSockets for Two-Way Interactive Communication in FastAPI Chevron down icon Chevron up icon
Chapter 9: Testing an API Asynchronously with pytest and HTTPX Chevron down icon Chevron up icon
Chapter 10: Deploying a FastAPI Project Chevron down icon Chevron up icon
Section 3: Build a Data Science API with Python and FastAPI Chevron down icon Chevron up icon
Chapter 11: Introduction to NumPy and pandas Chevron down icon Chevron up icon
Chapter 12: Training Machine Learning Models with scikit-learn Chevron down icon Chevron up icon
Chapter 13: Creating an Efficient Prediction API Endpoint with FastAPI Chevron down icon Chevron up icon
Chapter 14: Implement a Real-Time Face Detection System Using WebSockets with FastAPI and OpenCV Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7
(16 Ratings)
5 star 87.5%
4 star 0%
3 star 6.3%
2 star 6.3%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Orzu Jan 09, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
best book for fast api
Subscriber review Packt
Érico Andrei Nov 16, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Both scientists and software developers will benefit from this book, as François Voron presents the reader with a comprehensive approach to building robust API solutions for data science and machine learning projects using Python and FastAPI.By covering API security, data persistence, WebSockets, automated testing and deployment, the author provides the toolset needed by the readers to build their own solutions, in a reliable and scalable way.
Amazon Verified review Amazon
Ben Oct 11, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
When I first cracked open this text, I was skeptical of its usefulness.I'd been using fast api for over a year, on multiple projects, at multiple jobs, including a real-time recommendation system for scientific publications.Already being familiar with FastApi and it's documentation (which is fantastic, by the way), I didn't think this book could add much.When I got into section one, I was pleased with how the book was structured.It followed the structure of the Fastapi documentation in a linear fashion but everywhere the the documentation glossed by minor details, the author went deep into the surrounding technologies, underlying infrastructure and client protocols.Additionally the source code examples here are more continuous and less atomic than the documentation.The long chain of related example modules was hugely helpful in understanding how the modules of an API server come together.Section two continued with this trend, explaining the minutiae of related technologies.Subjects like types of databases, websockets, authorization headers, were presented thoroughly in a less coherent way.The level of detail on deeper topics like unit test parameterization, was still well beyond any docs or blogs, and helped me think through a problem I was considering that week at work.The closing chapter corrected course and explained the nuances of how to structure your api server for low IO needs.I expected topics that complex to be outside the scope of this book, and I was pleasantly surprised at their inclusion in the late sections.In conclusion, this book makes a thorough and helpful resource for web services with complex performance requirements.I'd recommend it highly to anyone working on ML engineering or an adjacent field.
Amazon Verified review Amazon
Vishvas Patel Oct 12, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book perfectly addresses modern data science challenges like managing and deployment of data models, security and testing using the FastAPI web framework. It covers from the ground up from basic python to advance model build & deployment using FastAPI methods. The author, François, has covered deep into the features with clarification on why, what, how and when to use. I am impressed with the depth of the author's knowledge as well as the ability to convey concepts clearly. I highly recommend this book to read by any experience #DataScientist or novice who is in that journey. Reading this book is really worth the amount of time put into it. #HappyLeanrning
Amazon Verified review Amazon
BhavaSri Oct 08, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Francois explains the concepts with nice examples and hands on to exercises. I have used FAST API for my projects. This book had lots to offer for people who already is aware. Love the organization of the topics.Highly recommend 😊
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.