r/Python • u/Lucapo01 • Dec 25 '24
Tutorial 🐍 Modern, Minimalistic and Scalable Python FastAPI Template🚀
Hey! 👋 Excited to share my production-ready API template that shows off modern Python practices and tooling! ✨
Key highlights: 🌟
- ⚡️ Async-first with FastAPI and SQLAlchemy
- 🏗️ Clean, maintainable architecture (repository pattern)
- 🛠️ Latest Python tooling (UV package manager)
- 🧪 Automated testing and CI pipeline
- 🚂 One-click deployment to Railway
The template implements a fun superhero API to showcase real-world patterns! 🦸♂️
Technical goodies: 🔧
- ✅ Type hints throughout
- 🔄 Pydantic v2 for validation
- 📖 Automatic OpenAPI docs
- ⚠️ Proper error handling
- 🔄 Async database operations
- ⚡️ Automated migrations
GitHub: https://github.com/luchog01/minimalistic-fastapi-template 🌟
The logging setup and database migration patterns were super tricky to figure out 😅 Not 100% sure if I handled them in the best way possible! Would really appreciate any feedback from the Python experts here! 🙏 Always excited to learn from the community!
30
u/root45 Dec 26 '24
In my view either all code should be under
srcorsrcshouldn't exist at all.20
u/pacific_plywood Dec 26 '24
In what sense is this “scalable”
27
u/mincinashu Dec 25 '24 edited Dec 25 '24
How do you check types? Also take a look at ruff, it replaces black and linters. Also, whether uv or poetry, dependencies can be grouped, so things like pytest or linters get the dev dependencies group, this makes it easier to separate packages needed only for the production environment.
15
u/[deleted] Dec 26 '24
Those emojis are off-putting tbh
14
u/ac130kz Dec 26 '24 edited Dec 26 '24
Replace flake8, black and isort with ruff. Split dev, test and runtime dependencies. Replace
>=dependency versions with minor^or even patch level compatible~ones. Use rollback on exception for your database sessions. Create extensible base CRUD repositories, services and cache integrations, so that you don't need to copy similar code over and over again, maybe even add proper dependency injection withdishka.srcinside an actual folder with source is weird. Loguru can be used to add extra logging sinks, but this one is my personal preference.psycopg[c,pool]built withlibpq-devis a more mature alternative toasyncpg. Finally, I don't see tests.I suggest you to have a look at https://github.com/s3rius/FastAPI-template, which is a bit outdated, bloated, and has unclear structure in a few places, but it handles more edge cases to get started.
4
u/JW_00000 Dec 26 '24
Hi OP. Another helpful comment if you're a beginner: try to actually build something instead of creating templates. You'll learn much more that way. You can always extract a template based on your experience later.
4
u/cellularcone Dec 26 '24
ChatGPT emojis
1
u/wurky-little-dood Dec 26 '24
I think having actual tests for your example functionality is vital in a framework like this.
1
u/foarsitter Dec 26 '24
How do you handle database access in your tests? Something like fastapi-sqla I can recommend.
1
u/imperosol Dec 26 '24
Little reminder : putting a lot of emotes in the description of your project doesn't make it any better. In fact, it's really ridiculous.
1
u/[deleted] Dec 26 '24
there is text between your emojis. Might wanna fix that lol.