Skip to content

Latest commit

 

History

History
328 lines (232 loc) · 6.9 KB

File metadata and controls

328 lines (232 loc) · 6.9 KB

Logo

Product Fast API boilerplate

Fast API boiler plate project

Table of Contents
  1. About The Project
  2. Getting Started
  3. License

About The Project

FastAPI boilerplate provides a simple basic structure for project creation with mysql database.

Built With

  • Python
  • FastAPI

Getting Started

Instructions for setting up project locally. To get a local copy up and running follow these simple steps.

Install + configure the project

1. Linux

Prerequisites

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

Installation

  1. Clone the repo
    git clone https://github.com/viitoradmin/python-fastapi-boilerplate/tree/feature/fastapi
  2. Upgrade pip version
    python -m pip install --upgrade pip==22.1.2
  3. Install the requirements for the project into the virtual environment
    pip install -r requirements.txt
  4. Install the dependencies of Fast API
    pip install "fastapi[all]"

2. Windows

  1. Create python virtual environment

    conda create --name venv python=3.11
    
  2. Activate the python virtual environment

    conda activate venv
    
  3. Install the requirements for the project into the virtual environment in the following sequence:

    pip install -r requirements.txt
    
  4. Install the dependencies of Fast API

    pip install "fastapi[all]"
    
  5. Upgrade pip version

    python -m pip install --upgrade pip==22.1.2
    

Use the alembic to Upgrade/Downgrade the database in the FastAPI

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:

  1. To create Migration folder

    python -m alembic init migrations
    
  2. Update the sqlalchemy.url into alembic.ini file

    sqlalchemy.url = mysql+pymysql://user:pass@host/db_name
    
  3. update the Migrations>>env.py file o auto migrate the database.

    from models import Base 
    target_database = Base.metadata
    
  4. Perform the initial migrations

    alembic revision --autogenerate -m 'initials'
    
  5. Apply the changes into the database (upgrade the database)

    alembic upgrade head
    
  6. To downgrade the database if required

    alembic downgrade -1
    

Poetry Installation Guide

This guide provides detailed instructions on how to set up and use Poetry for managing dependencies and environments in your Python project.

What is Poetry?

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.

Prerequisites

  • 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 --version

Installing Poetry

1. Using the Official Installer

Run the following command to install Poetry:

curl -sSL https://install.python-poetry.org | python3 -

2. Verifying Installation

Once installed, verify Poetry by running:

poetry --version

This should display the installed version of Poetry.

3. Adding Poetry to Your PATH

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.

Setting Up Poetry in a Project

1. Initialize a New Project

Navigate to your project directory and run:

poetry init

Follow the prompts to define your project metadata (e.g., package name, version, description).

2. Adding Dependencies

To add dependencies:

poetry add <package-name>

Example:

poetry add requests

To add development dependencies:

poetry add --dev <package-name>

3. Installing Dependencies

Install all dependencies defined in pyproject.toml:

poetry install

4. Using Virtual Environments

Poetry automatically creates a virtual environment for your project. To activate it:

poetry shell

To deactivate, simply exit the shell:

exit

Managing Your Project

Updating Dependencies

To update dependencies to their latest compatible versions:

poetry update

Listing Installed Packages

To list all installed packages and their versions:

poetry show

Publishing Your Package

If you’re packaging your project, publish it to PyPI with:

poetry publish --build

Uninstalling Poetry

To uninstall Poetry, remove its files:

curl -sSL https://install.python-poetry.org | python3 - --uninstall

Additional Resources

With this setup, you can efficiently manage Python project dependencies and environments using Poetry. Happy coding!

Run the server in development mode

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

Release History

  • 0.1
    • Work in progress