Spaces:
Sleeping
Sleeping
kandi1clickkits
commited on
Commit
·
94180d1
1
Parent(s):
0848930
chart code with DB
Browse files
main.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Depends
|
| 2 |
from sqlalchemy import create_engine, Column, Integer, String
|
| 3 |
from sqlalchemy.ext.declarative import declarative_base
|
| 4 |
-
from sqlalchemy.orm import sessionmaker, Session
|
| 5 |
from fastapi.responses import HTMLResponse
|
| 6 |
import gradio as gr
|
| 7 |
import os
|
|
@@ -16,7 +17,11 @@ engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
|
| 16 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 17 |
Base = declarative_base()
|
| 18 |
|
| 19 |
-
class Item(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
__tablename__ = "items"
|
| 21 |
|
| 22 |
id = Column(Integer, primary_key=True, index=True)
|
|
@@ -38,10 +43,11 @@ def create_gr_chart(data):
|
|
| 38 |
|
| 39 |
@app.post("/form/")
|
| 40 |
async def store_data(item: Item, db: Session = Depends(get_db)):
|
| 41 |
-
|
|
|
|
| 42 |
db.commit()
|
| 43 |
-
db.refresh(
|
| 44 |
-
return
|
| 45 |
|
| 46 |
@app.get("/visualization/", response_class=HTMLResponse)
|
| 47 |
async def show_visualization():
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
from fastapi import FastAPI, Depends
|
| 3 |
from sqlalchemy import create_engine, Column, Integer, String
|
| 4 |
from sqlalchemy.ext.declarative import declarative_base
|
| 5 |
+
from sqlalchemy.orm import sessionmaker, Session
|
| 6 |
from fastapi.responses import HTMLResponse
|
| 7 |
import gradio as gr
|
| 8 |
import os
|
|
|
|
| 17 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 18 |
Base = declarative_base()
|
| 19 |
|
| 20 |
+
class Item(BaseModel):
|
| 21 |
+
category: str
|
| 22 |
+
value: int
|
| 23 |
+
|
| 24 |
+
class ItemDB(Base):
|
| 25 |
__tablename__ = "items"
|
| 26 |
|
| 27 |
id = Column(Integer, primary_key=True, index=True)
|
|
|
|
| 43 |
|
| 44 |
@app.post("/form/")
|
| 45 |
async def store_data(item: Item, db: Session = Depends(get_db)):
|
| 46 |
+
db_item = ItemDB(**item.dict())
|
| 47 |
+
db.add(db_item)
|
| 48 |
db.commit()
|
| 49 |
+
db.refresh(db_item)
|
| 50 |
+
return db_item
|
| 51 |
|
| 52 |
@app.get("/visualization/", response_class=HTMLResponse)
|
| 53 |
async def show_visualization():
|