- Install
python3.9 - Create a top level directory
mkdir fastapi_workspaceand navigate to that directorycd fastapi_workspace - Make sure the current working directory is
fastapi_workspace - Create a virtual environment. Run
python3.9 -m venv env - Activate the virtual env. Run
source env/bin/activate - Make sure you are in
fastapi_workspacedirectory. Clone the project from github. Rungit clone https://github.com/RohitKKansal/fastapi_authentication.git - Navigate to the project directory. Run
cd fastapi_authentication/ - Run
pip3 install --upgrade pip3 && pip3 install -r requirements.txt - Run
python main.py. You can see the project on your browser at0.0.0.0:8000
-
Create a top lavel directory
mkdir fastapi_projectand navigate to that directorycd fastapi_project -
Create a environment with Python latest version. Run
python3.10 -m venv env -
Activate the virtual env. Run
source env/bin/activate -
Make sure you are in
fastapi_projectdirectory. Create a new file. Runtouch main.py -
Open the directory
fastapi_projectin your favorite integrated development environment (IDE). If you open the directory in visual studio code then Runcode ./ -
Open the
main.pyfile in visual studio code then write the code:-- Import the FastAPI in the
main.pyfile -
from fastapi import FastAPI import uvicorn app = FastAPI() @app.get('/') def index_view(): return "It's working!" if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000) - Run
python main.py. - You can see the project on your browser at
0.0.0.0:8000
- Import the FastAPI in the
-
Create a file for database connection :-
- Install package. Run
pip3 install sqlalchemy - Create a folder for connect database
mkdir settingsin this folder create a filetouch database.py -
from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7" ALGORITHM = "HS256" ACCESS_TOKEN_EXPIRE_MINUTES = 30 SQLALCHEMY_DATABASE_URL = "sqlite:///./twofactor_app.db" engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base()
- Install package. Run
-
How to create models :-
- Create a model file
models.pyin current directoryfastapi_project - There are a few parts to make this work. The first part is to connect to the database:
engine = create_engine(my_database_connection) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base()- Then create models class For Example:
from sqlalchemy import Boolean, Column, Integer, String, Enum, ForeignKey from settings.database import Base class User(Base): __tablename__ = "users" id = Column(Integer, primary_key=True, index=True) email = Column(String, unique=True, index=True) first_name = Column(String) last_name = Column(String) hashed_password = Column(String) otp_secret = Column(String) disabled = Column(Boolean, default=False)-
SQLAlchemyis a Python SQL toolkit and ORM supporting database manipulation and management.SQLAlchemyprovides users with an ORM using Data Mapper design pattern. -
sqlalchemyAll atributes import fromsqlalchemyfor example Model Data types:-Boolean, Column, Integer, String, Enum, ForeignKeyetc. We can import any data type fromsqlalchemyIt is provide lots of data types. -
The
__tablename__attribute tells SQLAlchemy the name of the table to use in the database for each of these models. -
Base:-declarative_baseis a factory function, that returns a base class (actually a metaclass), and the entities are going to inherit from it. Once the definition of the class is done, the Table and mapper will be generated automatically.
- Create a model file
git clone git@github.com:RohitKKansal/fastapi_authentication.git
cd fastapi_authentication
python3.8 -m venv env
source env/bin/active
pip install --upgrade pip && pip install -r requirements.txt
uvicorn main:app --reload
https://alembic.sqlalchemy.org/en/latest/tutorial.html#running-our-first-migration