kandi1clickkits commited on
Commit
a02591a
·
1 Parent(s): a2fbe15

chart code with DB

Browse files
Files changed (1) hide show
  1. main.py +20 -46
main.py CHANGED
@@ -1,58 +1,32 @@
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
9
- import logging
10
- from fastapi import Depends
11
 
12
  app = FastAPI()
13
 
14
- # Use SQLite in-memory database
15
- SQLALCHEMY_DATABASE_URL = "sqlite:///:memory:"
16
-
17
- engine = create_engine(SQLALCHEMY_DATABASE_URL)
18
- SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
19
- Base = declarative_base()
20
-
21
- class Item(BaseModel):
22
- category: str
23
- value: int
24
-
25
- class ItemDB(Base):
26
- __tablename__ = "items"
27
-
28
- id = Column(Integer, primary_key=True, index=True)
29
- category = Column(String, index=True)
30
- value = Column(Integer)
31
-
32
- Base.metadata.create_all(bind=engine)
33
-
34
- def get_db():
35
- db = SessionLocal()
36
- try:
37
- yield db
38
- finally:
39
- db.close()
40
-
41
- def create_gr_chart(data):
42
  chart = gr.LineChart(data=data)
43
  return chart.launch()
44
 
45
  @app.post("/form/")
46
- async def store_data(item: Item, db: Session = Depends(get_db)):
47
- db_item = ItemDB(**item.dict())
48
- db.add(db_item)
49
- db.commit()
50
- db.refresh(db_item)
51
- return db_item
 
 
 
 
 
52
 
53
  @app.get("/visualization/", response_class=HTMLResponse)
54
  async def show_visualization():
55
- # Fetch data from the database and create a Gradio chart
56
- data = get_data_from_db() # Replace with your function to fetch data from the database
57
- chart = create_gr_chart(data) # Replace with your function to create a Gradio chart
58
- return chart
 
 
 
1
+ from fastapi import FastAPI, HTTPException, Form
 
 
 
 
2
  from fastapi.responses import HTMLResponse
3
  import gradio as gr
 
 
 
4
 
5
  app = FastAPI()
6
 
7
+ def create_gr_chart(categories, values):
8
+ data = {"x": categories, "y": values}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  chart = gr.LineChart(data=data)
10
  return chart.launch()
11
 
12
  @app.post("/form/")
13
+ async def show_chart(category: str = Form(...), value: int = Form(...)):
14
+ # You can append the received category and value to a list if needed
15
+ # categories.append(category)
16
+ # values.append(value)
17
+
18
+ # For simplicity, I'm assuming you have two lists of categories and values
19
+ # Modify this part based on your actual data structure
20
+ categories = ["Category1", "Category2", "Category3"]
21
+ values = [10, 20, 30]
22
+
23
+ return create_gr_chart(categories, values)
24
 
25
  @app.get("/visualization/", response_class=HTMLResponse)
26
  async def show_visualization():
27
+ # For simplicity, I'm assuming you have two lists of categories and values
28
+ # Modify this part based on your actual data structure
29
+ categories = ["Category1", "Category2", "Category3"]
30
+ values = [10, 20, 30]
31
+
32
+ return create_gr_chart(categories, values)