Spaces:
Sleeping
Sleeping
kandi1clickkits
commited on
Commit
·
035d91e
1
Parent(s):
8557221
chart code with DB
Browse files- main.py +42 -19
- requirements.txt +3 -1
main.py
CHANGED
|
@@ -1,24 +1,47 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Body
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
try:
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
return
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Body
|
| 2 |
+
from sqlalchemy import create_engine, Column, Integer, String
|
| 3 |
+
from sqlalchemy.ext.declarative import declarative_base
|
| 4 |
+
from sqlalchemy.orm import sessionmaker
|
| 5 |
+
from fastapi.responses import HTMLResponse
|
| 6 |
+
import gradio as gr
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
|
| 11 |
+
|
| 12 |
+
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
| 13 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 14 |
+
Base = declarative_base()
|
| 15 |
+
|
| 16 |
+
def create_gr_chart(data):
|
| 17 |
+
chart = gr.LineChart(data=data)
|
| 18 |
+
return chart.launch()
|
| 19 |
+
class Item(Base):
|
| 20 |
+
__tablename__ = "items"
|
| 21 |
+
|
| 22 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 23 |
+
category = Column(String, index=True)
|
| 24 |
+
value = Column(Integer)
|
| 25 |
+
|
| 26 |
+
Base.metadata.create_all(bind=engine)
|
| 27 |
+
|
| 28 |
+
def get_db():
|
| 29 |
+
db = SessionLocal()
|
| 30 |
try:
|
| 31 |
+
yield db
|
| 32 |
+
finally:
|
| 33 |
+
db.close()
|
| 34 |
+
|
| 35 |
+
@app.post("/form/")
|
| 36 |
+
async def store_data(item: Item, db: Session = Depends(get_db)):
|
| 37 |
+
db.add(item)
|
| 38 |
+
db.commit()
|
| 39 |
+
db.refresh(item)
|
| 40 |
+
return item
|
| 41 |
+
|
| 42 |
+
@app.get("/visualization/", response_class=HTMLResponse)
|
| 43 |
+
async def show_visualization():
|
| 44 |
+
# Fetch data from the database and create a Gradio chart
|
| 45 |
+
data = get_data_from_db() # Define this function to fetch data from the database
|
| 46 |
+
chart = create_gr_chart(data) # Define this function to create a Gradio chart
|
| 47 |
+
return chart
|
requirements.txt
CHANGED
|
@@ -1,3 +1,5 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
-
pandas
|
|
|
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
+
pandas
|
| 4 |
+
sqlite
|
| 5 |
+
gradio
|