Spaces:
Sleeping
Sleeping
kandi1clickkits
commited on
Commit
·
007a3ba
1
Parent(s):
36e9717
chart code with DB
Browse files
main.py
CHANGED
|
@@ -1,21 +1,26 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
@@ -32,6 +37,10 @@ def get_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)
|
|
@@ -42,6 +51,6 @@ async def store_data(item: Item, db: Session = Depends(get_db)):
|
|
| 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() #
|
| 46 |
-
chart = create_gr_chart(data) #
|
| 47 |
-
return chart
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Body, Depends
|
| 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 |
+
from fastapi import Depends
|
| 7 |
import gradio as gr
|
| 8 |
+
import os
|
| 9 |
+
import logging
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
+
# Use an absolute path for the SQLite database file
|
| 14 |
+
database_path = os.path.join(os.getcwd(), "test.db")
|
| 15 |
+
SQLALCHEMY_DATABASE_URL = f"sqlite:///{database_path}"
|
| 16 |
+
|
| 17 |
+
# Set up logging for debugging
|
| 18 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 19 |
|
| 20 |
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
| 21 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 22 |
Base = declarative_base()
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
class Item(Base):
|
| 25 |
__tablename__ = "items"
|
| 26 |
|
|
|
|
| 37 |
finally:
|
| 38 |
db.close()
|
| 39 |
|
| 40 |
+
def create_gr_chart(data):
|
| 41 |
+
chart = gr.LineChart(data=data)
|
| 42 |
+
return chart.launch()
|
| 43 |
+
|
| 44 |
@app.post("/form/")
|
| 45 |
async def store_data(item: Item, db: Session = Depends(get_db)):
|
| 46 |
db.add(item)
|
|
|
|
| 51 |
@app.get("/visualization/", response_class=HTMLResponse)
|
| 52 |
async def show_visualization():
|
| 53 |
# Fetch data from the database and create a Gradio chart
|
| 54 |
+
data = get_data_from_db() # Replace with your function to fetch data from the database
|
| 55 |
+
chart = create_gr_chart(data) # Replace with your function to create a Gradio chart
|
| 56 |
+
return chart
|