Jun 23, 2024
Create Virtual Environment:*
python3 -m venv envsource env/bin/activateInstall Dependencies:*
pip install fastapi sqlalchemy psycopg2-binaryCreate main.py File:
app = FastAPI()Create Pydantic Models:
ChoiceBase and QuestionBase classes inheriting from BaseModel.Create database.py File:
create_engine, sessionmaker from SQLAlchemyfrom sqlalchemy.ext.declarative import declarative_basepostgresql://<username>:<password>@localhost:5432/quiz_application_ytengine = create_engine(database_url)SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)Base = declarative_base()Create Database via PGAdmin:
quiz_application_ytCreate models.py File:
Questions and Choices tables/models inheriting from BaseSynchronize Models to Database:
POST /questions:
async def create_question(question: QuestionBase, db: Session = Depends(get_db))GET /questions/{question_id}:*
async def read_question(question_id: int, db: Session = Depends(get_db))GET /choices:
async def read_choices(question_id: int, db: Session = Depends(get_db))Run Uvicorn Server:*
uvicorn main:app --reloaduvicorn is installed, else run: pip install uvicornInteract with API via Browser: