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 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!