Table of Contents
FastAPI boilerplate provides a simple basic structure for project creation with mysql database.
Instructions for setting up project locally. To get a local copy up and running follow these simple steps.
Requirement of Project
- Install Python
Python-Version : 3.11.0 - Create python virtual environment
python3 -m venv venv
- Activate the python virtual environment
source venv/bin/activate
- Clone the repo
git clone https://github.com/viitoradmin/python-fastapi-boilerplate/tree/feature/fastapi
- Upgrade pip version
python -m pip install --upgrade pip==22.1.2
- Install the requirements for the project into the virtual environment
pip install -r requirements.txt
- Install the dependencies of Fast API
pip install "fastapi[all]"
-
Create python virtual environment
conda create --name venv python=3.11 -
Activate the python virtual environment
conda activate venv -
Install the requirements for the project into the virtual environment in the following sequence:
pip install -r requirements.txt -
Install the dependencies of Fast API
pip install "fastapi[all]" -
Upgrade pip version
python -m pip install --upgrade pip==22.1.2
Note: Because by default Fastapi is provide only initial migrations. It doesn't support the upgrade and downgrade the database. so,to perform automatic migrations follow the following steps:
-
To create Migration folder
python -m alembic init migrations -
Update the sqlalchemy.url into alembic.ini file
sqlalchemy.url = mysql+pymysql://user:pass@host/db_name -
update the Migrations>>env.py file o auto migrate the database.
from models import Base target_database = Base.metadata -
Perform the initial migrations
alembic revision --autogenerate -m 'initials' -
Apply the changes into the database (upgrade the database)
alembic upgrade head -
To downgrade the database if required
alembic downgrade -1
This guide provides detailed instructions on how to set up and use Poetry for managing dependencies and environments in your Python project.
Poetry is a dependency management and packaging tool for Python. It simplifies the process of managing project dependencies, virtual environments, and publishing packages.
Key features:
- Dependency resolution.
- Virtual environment management.
- Project packaging and publishing.
- Python: Ensure Python is installed on your system. Poetry supports Python 3.7 and above.
- Pip: The Python package manager should also be installed.
You can verify installations using the following commands:
python --version
pip --versionRun the following command to install Poetry:
curl -sSL https://install.python-poetry.org | python3 -Once installed, verify Poetry by running:
poetry --versionThis should display the installed version of Poetry.
If Poetry is not recognized, ensure it is added to your system PATH. By default, Poetry is installed in:
- Unix/macOS:
$HOME/.local/bin - Windows:
%APPDATA%\Python\Scripts
Add this directory to your PATH.
Navigate to your project directory and run:
poetry initFollow the prompts to define your project metadata (e.g., package name, version, description).
To add dependencies:
poetry add <package-name>Example:
poetry add requestsTo add development dependencies:
poetry add --dev <package-name>Install all dependencies defined in pyproject.toml:
poetry installPoetry automatically creates a virtual environment for your project. To activate it:
poetry shellTo deactivate, simply exit the shell:
exitTo update dependencies to their latest compatible versions:
poetry updateTo list all installed packages and their versions:
poetry showIf you’re packaging your project, publish it to PyPI with:
poetry publish --buildTo uninstall Poetry, remove its files:
curl -sSL https://install.python-poetry.org | python3 - --uninstallWith this setup, you can efficiently manage Python project dependencies and environments using Poetry. Happy coding!
Add environment variables (given in .env) by running following command in cmd/terminal:
Run the server
python asgi.py
Browse Swagger API Doc at: http://localhost:8000/docs
Browse Redoc at: http://localhost:8000/redoc
Browse Swagger API Doc for version v1 at: http://localhost:8000/v1/docs
Browse Swagger API Doc for version v2 at: http://localhost:8000/v2/docs
- 0.1
- Work in progress
