Table of Contents
This example demonstrates the seamless integration of FastAPI, a modern, high-performance web framework, with Pydantic 2.0, a robust and powerful data validation library. The integration is further enhanced by the use of SQLAlchemy ORM, a popular and feature-rich Object-Relational Mapping tool, and PostgreSQL16 relational database.
The entire stack is connected using the asyncpg Database Client Library, which provides a robust and efficient way to interact with PostgreSQL databases in Python, leveraging the power of asyncio and event loops.
Notably, this example showcases the latest and greatest versions of SQLAlchemy and psycopg, which are renowned for their robustness, power, and speed. The inclusion of FastAPI adds a modern, fast, and high-performance web framework to the mix allowing for the rapid development of APIs with Python 3.8+.
FastAPI has received significant recognition in the industry, including a review on thoughtworks Technology Radar in April 2021, where it was classified as a Trial technology, with comments praising its performance, ease of use, and features such as API documentation using OpenAPI. Additionally, FastAPI was recognized in the Python Developers Survey 2022 Results, conducted by the Python Software Foundation and JetBrains, where it was reported that 1 in 4 Python developers use FastAPI, with a 4 percentage point increase from the previous year.
To build , run and test and more ... use magic of make help to play with this project.
1. make docker-build
2. make docker-up
3. make docker-apply-db-migrations
4. make docker-feed-db
It took me a while to find nice data set. Hope works of Shakespeare as example will be able to cover first part with read only declarative base configuration and all type of funny selects :) Data set is coming form https://github.com/catherinedevlin/opensourceshakespeare Next models were generated with https://github.com/agronholm/sqlacodegen
To deliver better user(developer) experience when watching logs with tons of information from few emitters (which are really needy on development stage) project is using rich library. Event with rich superpowers reading logs is not easy. Found rich really nice - but it took time to learn how to integrate it as logger object properly and keep it as singleton.
To address below needs:
- it is hard to find what I am looking for even with glasses on.
- don’t want to hire ELK to be able to use logs.
- want to move fast enough with debugging.
Below steps were done to integrate rich into project.
- Configure emitters with config.ini
- Eliminate duplicates i.e. sqlalchemy echo by separate handlers
- Keep logger as singleton pattern to avoid multiple instances
- add uvicorn parameter --log-config config.ini
Setup user authentication with JWT and Redis as token storage.
pyenv install 3.12 && pyenv local 3.12
poetry install
Hope you enjoy it.
Power of Polars Library in data manipulation and analysis.
It uses the polars library to read the Excel data into a DataFrame by passing the bytes to the pl.read_excel()
function -
https://docs.pola.rs/py-polars/html/reference/api/polars.read_excel.html
In pl.read_excel()
“calamine” engine can be used for reading all major types of Excel Workbook (.xlsx, .xlsb, .xls) and is dramatically faster than the other options, using the fastexcel module to bind calamine.
Use this space to list resources you find helpful and would like to give credit to. I've included a few of my favorites to kick things off!
- Open Source Shakespeare Dataset
- SQL Code Generator
- Passlib - password hashing library for Python
- Polars - fast DataFrame library for Rust and Python
- Rich - Traceback and logging, made easy
- [long time ago...] it was a long time ago in galaxy far far away...
- [JUN 4 2022] alembic migrations added to project
- [JUN 6 2022] initial dataset for shakespeare models
- [OCT 3 2022] poetry added to project
- [NOV 12 2022] ruff implemented to project as linting tool
- [FEB 14 2023] bump project to Python 3.11
- [APR 10 2023] implement logging with rich
- [APR 28 2023] Rainbow logs with rich 🌈
- [JUL 7 2023] migrate to pydantic 2.0 ⏩
- [JUL 25 2023] add user authentication with JWT and Redis as token storage 🔒 🔑
- [SEP 2 2023] add passlib and bcrypt for password hashing 🔒 🔑
- [OCT 21 2023] refactor shakespeare models to use sqlalchemy 2.0 ⏩
- [FEB 1 2024] bump project to Python 3.12 ⏩
- [MAR 15 2024] add polars and calamine to project 😻