kandi1clickkits commited on
Commit
da0318e
·
1 Parent(s): 47e780d

chart code with DB

Browse files
Files changed (1) hide show
  1. main.py +9 -31
main.py CHANGED
@@ -1,37 +1,18 @@
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.responses import HTMLResponse
3
- from typing import List
4
- import matplotlib.pyplot as plt
5
- import io
6
- import base64
7
 
8
  app = FastAPI()
9
 
10
  def create_chart(categories, values):
11
- plt.plot(categories, values)
12
- plt.xlabel('Categories')
13
- plt.ylabel('Values')
14
-
15
- # Save the plot to a BytesIO object
16
- img = io.BytesIO()
17
- plt.savefig(img, format='png')
18
- img.seek(0)
19
- plt.close()
20
-
21
- # Convert the BytesIO object to base64 for embedding in HTML
22
- img_base64 = base64.b64encode(img.read()).decode("utf-8")
23
- return f'<img src="data:image/png;base64,{img_base64}"/>'
24
 
25
  @app.post("/form/")
26
- async def show_chart(data: List[dict]):
27
- categories = [item["category"] for item in data]
28
- values = [item["value"] for item in data]
29
-
30
- chart_html = create_chart(categories, values)
31
-
32
- # Embed the chart HTML in an iframe
33
- iframe_html = f'<iframe width="600" height="400" srcdoc="{chart_html}"></iframe>'
34
- return HTMLResponse(content=iframe_html, status_code=200)
35
 
36
  @app.get("/visualization/", response_class=HTMLResponse)
37
  async def show_visualization():
@@ -40,8 +21,5 @@ async def show_visualization():
40
  categories = ["Category1", "Category2", "Category3"]
41
  values = [10, 20, 30]
42
 
43
- chart_html = create_chart(categories, values)
44
-
45
- # Embed the chart HTML in an iframe
46
- iframe_html = f'<iframe width="600" height="400" srcdoc="{chart_html}"></iframe>'
47
- return HTMLResponse(content=iframe_html, status_code=200)
 
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.responses import HTMLResponse
3
+ import gradio as gr
 
 
 
4
 
5
  app = FastAPI()
6
 
7
  def create_chart(categories, values):
8
+ data = {"students": categories, "marks": values}
9
+ plot = gr.LinePlot(data, every=5, x="students", y="marks", y_title="marks (percent)", overlay_point=True, width=500, height=500)
10
+ return plot
 
 
 
 
 
 
 
 
 
 
11
 
12
  @app.post("/form/")
13
+ async def show_chart(categories: list, values: list):
14
+ chart = create_chart(categories, values)
15
+ return HTMLResponse(content=chart.launch(), status_code=200)
 
 
 
 
 
 
16
 
17
  @app.get("/visualization/", response_class=HTMLResponse)
18
  async def show_visualization():
 
21
  categories = ["Category1", "Category2", "Category3"]
22
  values = [10, 20, 30]
23
 
24
+ chart = create_chart(categories, values)
25
+ return HTMLResponse(content=chart.launch(), status_code=200)