Spaces:
Sleeping
Sleeping
import pandas as pd | |
from fastapi import FastAPI | |
import plotly.graph_objects as go | |
import gradio as gr | |
import uvicorn | |
def create_plot(df): | |
fig = go.Figure() | |
for column in df.columns[1:]: | |
fig.add_trace(go.Scatter(x=df[df.columns[0]], y=df[column], mode='lines', name=column)) | |
# 配置图例 | |
fig.update_layout( | |
legend=dict( | |
title="Variables", | |
orientation="h", | |
yanchor="bottom", | |
y=1.02, | |
xanchor="right", | |
x=1 | |
), | |
xaxis_title='Time', | |
yaxis_title='Values' | |
) | |
return fig | |
# 创建Gradio界面 | |
demo = gr.Blocks() | |
with demo: | |
# 示例数据 | |
data = { | |
'time': pd.date_range(start='2023-01-01', periods=6, freq='D'), | |
'y1': [0, 1, 4, 9, 16, 25], | |
'y2': [0, 1, 2, 3, 4, 5] | |
} | |
df = pd.DataFrame(data) | |
plot = create_plot(df) | |
gr.Plot(plot) | |
# 运行Gradio界面 | |
if __name__ == "__main__": | |
app = FastAPI() | |
app = gr.mount_gradio_app(app, demo, path="/") | |
uvicorn.run(app, host="127.0.0.1", port=7860) | |