File size: 1,077 Bytes
4570f48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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)