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

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!