This repository is dedicated to learning and practicing FastAPI, a powerful web framework for Python. Thanks to this YouTube video, I was introduced to FastAPI and am already feeling empowered while building APIs!
This repo definitely doesn't cover all features of fastapi but contains go to fundamentals for getting started with fastapi. Following steps outline the key concepts and features can be considered as the learning path at beginner level, referring learn_fastapi.ipynb
and myapi.py
:
-
fastapi Documentation
The official fastapi documentation is the best. Solely following it step by step would be enough for anyone. Really, just try it.
1.1. **Concurrency + Parallelism ==> Web + Machine Learning **
Read and summarized from fastapi async:
concurrency
: The ability to manage multiple tasks at the same time by quickly switching between them. In FastAPI, this is achieved using asynchronous programming (async/await
), enabling efficient handling of I/O-bound operations like database queries or API calls without blocking the main thread.parallelism
: Running multiple tasks simultaneously on different CPU cores. In Python, libraries like multiprocessing or frameworks like FastAPI with workers (e.g., usinggunicorn
with multiple workers) enable parallel execution, which is particularly useful forCPU-bound
tasks.multiprocessing
: A technique to create separate processes (not threads) to run tasks in parallel, utilizing multiple CPU cores. This is effective forCPU-bound
tasks in Python, bypassing the Global Interpreter Lock (GIL
). For FastAPI, multiprocessing can help with heavy computation tasks by delegating them to worker processes.CPU bound
: A type of task that is limited by the speed of the CPU, such as mathematical computations or data processing. In FastAPI, CPU-bound tasks are better handled using multiprocessing or external workers (e.g.,celery
orbackground tasks
) to prevent slowing down the main application thread.
-
Installation:
Need to install
fastapi
library and also needuvicorn
to run local server. For data validation,pydantic
is superb which comes preinstalled with fastapi.pip install fastapi uvicorn
-
Folder Structure
(not dived much into it yet)
Depending on the project, it might be a good idea to have separate files for routing, database connection, models and schemas. Its been thoroughly implemented in the Hotel Transylvania repo!
-
Basics:
- learn how to initiate
fastapi
application - api endpoints or routers
- CRUD with GET, POST, PUT, DELETE
- Data validation with pydantic
- parameters
- path parameters
- query parameters
- request body
- response model
- learn how to initiate
-
Run Uvicorn Server:
- command to run the app through uvicorn:
uvicorn myapi:app --reload
myapi
is the name of the py fileapp
is the name of the instance of FASTAPI classreload
flag enables auto reload when changed and saved
- command to run the app through uvicorn:
-
Auto Generated API Docs:
- learn the most awesome thing of fastapi, the auto generated api documentation
- swagger UI
- docly
- learn the most awesome thing of fastapi, the auto generated api documentation
-
CRUD:
- dive into the depth of CRUD with GET, POST, PUT, DELETE
- How to validate data with pydantic models
-
Useful Libraries/approaches/validations:
fastapi
- HTTPException (raising HTTP exception/error)
pydantic
basemodel
EmailStr
validator
(decorator for custom validation)- json serialization [
.json()
to json,.model_dump()
to dict,.parse_raw()
to pydantic]
uuid
- UUID and uuid4 (generate unique id)
-
Test fastapi
- add
__init__.py
in the repo root --> addresses import issues - create
tests/
folder to contain all test files - add
__init__.py
in thetests/
folder as well --> pytest can easily detect test files - pytest using
fastapi.TestClient
- follow
test_myapi.py
- follow
If hungry for more: to get habituted with industry standard and good practices of fastapi
+pydantic
+loguru
+pytest
on how to build+structure+organize from scratch dive into this repo: Hotel Transylvania! Its a hotel management system that can handle guest and room CRUD operations.
its an on going project at the moment, so expect some services to be finished soon. [30 Jan, 2025]
This repo includes:
- API for guests and rooms which is kinda fast -->
fastAPI
- Clean & modular repo structure
- separate application and tests directory
- clean module/package initiation at each submodule
- modular structure for required data, schemas, services and routers
- Clean application
- Added multiple modular routers to the main
- can be switched to any other framework easily
- Use of custom Exception
- Logging using
loguru
: what a library! pytest
for unit testing- Of course,
github actions
to automate testing (CI)
If you're passionate about testing or Python development, let’s discuss ideas and experiences. Feedback, forks, and collaboration requests are always welcome!