Skip to content

Latest commit

 

History

History
117 lines (108 loc) · 3.61 KB

python-fastapi.md

File metadata and controls

117 lines (108 loc) · 3.61 KB

Python - FastAPI Framework Notes

Introduction

Official Documentation

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.

The key features are:

  • Fast: Very high performance, on par with NodeJS and Go (thanks to **Starlette** and **Pydantic**). One of the fastest Python frameworks available.
  • Fast to code: Increase the speed to develop features by about 200% to 300%. *
  • Fewer bugs: Reduce about 40% of human (developer) induced errors. *
  • Intuitive: Great editor support. Completion everywhere. Less time debugging.
  • Easy: Designed to be easy to use and learn. Less time reading docs.
  • Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
  • Robust: Get production-ready code. With automatic interactive documentation.
  • Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.
How to install FastAPI

To install FastAPI you need to have python and pip installed on your machine.

It is prefered to install a virtual environment for your project and install required dependencies on the virtual environment.

The following guide can be used for assistance in installing venv module, creating a virtual env. and activating it.

Step 1: Install VirtualEnv Module

pip install vitualenv

Step 2: Create a Virtual Env.

python -m venv

Step 3: Activate your Virtual Env.

venv/Scripts/activate

The pip command for installing FastAPI and all of its components:

pip install fastapi[all]

This will install all required dependencies such as uvicorn and pydantic and others.

Getting Started and Running the FastAPI Server

Create your first Endpoint

First of all create an endpoint to retreive data from.

Import FastAPI as shown below and create an app variable as shown below. Use FastAPI(debug=True) to enable debug mode.

The @app.get("/") uses the fastapi decorator class and the "/" indicates that navigating to http://:/ will whow the returned result, in our case is the dictionary {"message": "Hello World"}.

    from fastapi import FastAPI

    app = FastAPI()


    @app.get("/")
    async def root():
        return {"message": "Hello World"}