Python Project Template for A Quick Setup
Sharing a python template that has helped me speed the development setup using FastAPI and pydantic
During the last few months I found myself needing to set up a quick python environment for interview assignments. At work I usually had templates I could import from other projects, where I removed the unnecessary logic and was set to go. I found that setting up the environment from scratch takes longer than I expected, and that finding documentation that wasn’t aimed for beginners or small projects takes even longer.
I created a template and uploaded it to GitHub, you can get it here.
TL;DR
- Set up a virtual environment.
- Define the API.
- Expose it using FastAPI.
Virtual Environments
Setting up a virtual environment is important to keep the project dependencies separate from other projects. That way we can create a project environment that is stable, reliable and simple to replicate between team members.
For setting up the virtual environment I’ll direct you to Daniela Brailovsky’s blogpost on virtual envs in Python.
Template Overview
For this example I created a project named PythonTemplate
, which contains several files such as an entry point main.py
file and requirements files. There are two main directories to note — template
which contains the code (everything aside from the entry point) and tests
which contains the code testing the code:
For simplicity, I created a project that receives an HTTP POST request of a user and outputs a greeting.
The input — a JSON structure containing the user’s name:
curl -X 'POST' \
'http://127.0.0.1:8000/users/' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"name": "Miri"
}'
The output — a greeting:
"Hey Miri!"
As you can see in the FastAPI UI:
Now that you’ve seen how it should look, let’s go over the template! We’ll start with the business logic.
Python Project Template
The Business Logic
- Create a user model class under
template.models.user
. Since I’m using FastAPI, I’ll usepydantic.BaseModel
to represent the class:
- Create a service class under
template.logic.user_service
:
Now that we have some logic, let’s see how we can use FastAPI to expose this logic.
Expose The Logic
Create a controller module under template.ctrls.users
directory.
In this module we call our logic and expose it via FastAPI’s routes using an APIRouter:
As you can see, it takes 3 lines of code to expose the controller logic — import fastapi.APIRouter
, create “users” APIRouter
with a path prefix and annotate the function you want to expose with the router and HTTP method!
This is the template
directory structure we created:
Now let’s move on to the main.py
entry point.
Server Side Entry Point
In main we create our FastAPI application and add our users
route, and run it using uvicorn
:
Try out your app via the the local server!
And that’s it — you got a greeting from you application!
Don’t forget to add tests when using the template!
The GIFs in this post were created using Windows screen capture feature, ezgif to crop the video and xconvert to convert the videos to gifs.
A special thanks to Ella sheer, Lea Cohen and Naomi Kriger, who helped improve this post!