-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
28 lines (20 loc) · 875 Bytes
/
main.py
File metadata and controls
28 lines (20 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from typing import Optional
from fastapi import FastAPI, Form
from sqlmodel import Field, Session, SQLModel, create_engine
import uvicorn
app = FastAPI(version="1.0.0", title="Introduction to SQLModel", description="Author: Ahmet BAŞ")
class train(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
fullname: str
age: int
gender: str
@app.post("/writetodb", description="Write to Database")
async def write_to_db(fullname : str = Form(...), age : int = Form(...), gender : str = Form(...)):
adding_data = train(fullname=fullname, age=age, gender=gender)
engine = create_engine("sqlite:///sqlmodel.db")
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
session.add(adding_data)
session.commit()
if __name__ == '__main__':
uvicorn.run(app, port=5959, host='0.0.0.0')