pythonbook/实力学习pygwalker/fastapidemo.py

24 lines
857 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import FastAPI
from fastapi.responses import HTMLResponse
import pandas as pd
import uvicorn
# 假设这个库和函数存在用于处理DataFrame
import pygwalker as pyg
# 加载数据并处理
df = pd.read_csv('./titanic.csv')
walker = pyg.walk(df) # 假设这个方法返回一个可以转换为HTML的对象例如DataFrame
app = FastAPI()
@app.get("/", response_class=HTMLResponse)
async def read_root():
# 将walker数据转换为HTML
# 如果walker是DataFrame可以使用to_html()。否则,您需要根据数据类型自定义转换逻辑。
walker_html = walker.to_html()
return walker_html
if __name__ == "__main__":
# 使用Uvicorn运行应用。在这里指定host和port以及`reload=True`实现代码改动时自动重启。
uvicorn.run("2:app", host="127.0.0.1", port=8000, reload=True)