Spaces:
Sleeping
Sleeping
kandi1clickkits
commited on
Commit
·
f3b2b4b
1
Parent(s):
fb3a173
chart code with DB
Browse files
main.py
CHANGED
|
@@ -1,21 +1,33 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.responses import HTMLResponse
|
| 3 |
from typing import List
|
| 4 |
-
import
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@app.post("/form/")
|
| 14 |
async def show_chart(data: List[dict]):
|
| 15 |
categories = [item["category"] for item in data]
|
| 16 |
values = [item["value"] for item in data]
|
| 17 |
|
| 18 |
-
return
|
| 19 |
|
| 20 |
@app.get("/visualization/", response_class=HTMLResponse)
|
| 21 |
async def show_visualization():
|
|
@@ -24,4 +36,4 @@ async def show_visualization():
|
|
| 24 |
categories = ["Category1", "Category2", "Category3"]
|
| 25 |
values = [10, 20, 30]
|
| 26 |
|
| 27 |
-
return
|
|
|
|
| 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 |
+
return HTMLResponse(content=create_chart(categories, values), status_code=200)
|
| 31 |
|
| 32 |
@app.get("/visualization/", response_class=HTMLResponse)
|
| 33 |
async def show_visualization():
|
|
|
|
| 36 |
categories = ["Category1", "Category2", "Category3"]
|
| 37 |
values = [10, 20, 30]
|
| 38 |
|
| 39 |
+
return HTMLResponse(content=create_chart(categories, values), status_code=200)
|