Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException | |
| from fastapi.responses import HTMLResponse | |
| from typing import List | |
| import matplotlib.pyplot as plt | |
| import io | |
| import base64 | |
| app = FastAPI() | |
| def create_chart(categories, values): | |
| plt.plot(categories, values) | |
| plt.xlabel('Categories') | |
| plt.ylabel('Values') | |
| # Save the plot to a BytesIO object | |
| img = io.BytesIO() | |
| plt.savefig(img, format='png') | |
| img.seek(0) | |
| plt.close() | |
| # Convert the BytesIO object to base64 for embedding in HTML | |
| img_base64 = base64.b64encode(img.read()).decode("utf-8") | |
| return f'<img src="data:image/png;base64,{img_base64}"/>' | |
| async def show_chart(data: List[dict]): | |
| categories = [item["category"] for item in data] | |
| values = [item["value"] for item in data] | |
| return HTMLResponse(content=create_chart(categories, values), status_code=200) | |
| async def show_visualization(): | |
| # For simplicity, I'm assuming you have two lists of categories and values | |
| # Modify this part based on your actual data structure | |
| categories = ["Category1", "Category2", "Category3"] | |
| values = [10, 20, 30] | |
| return HTMLResponse(content=create_chart(categories, values), status_code=200) | |