python - 根据“Dash”中的用户选择创建图表
问题描述
我对 python 和“Dash”非常陌生——我正在寻找一种方法,可以从选择的数据中绘制图表。
以下是我的代码,但由于某种原因,我在选择一个选项后只返回一个空白图?
我认为问题出现在“Def 交互式图表”部分。
import dash
import dash_core_components as dcc
import dash_html_components as html
import time
from collections import deque
import plotly.graph_objs as go
import random
import plotly.express as px
import pandas as pd
df = pd.read_csv("d5.csv").tail(10)
from dash.dependencies import Output, Input
app = dash.Dash(__name__)
app.layout=html.Div([
html.H1("Graph Analysis"),
dcc.Dropdown(id='graph-choice',
options=[{'label':'Room 1 Temperature', 'value':'R1Temp'},
{'label':'Room 2 Temperature', 'value':'R2Temp'},
{'label':'Room 3 Temperature', 'value':'R3Temp'}],
value='R1Temp'
),
dcc.Graph(id='my-graph', figure={}
)
])
@app.callback(
Output(component_id='my-graph', component_property='figure'),
Input(component_id='graph-choice', component_property='value')
)
def interactive_graphs(selected_value):
if selected_value == 'R1Temp':
fig_line = px.line(data_frame=df, x='Time', y='R1Temp')
else if selected_value == 'R2Temp':
fig_line = px.line(data_frame=df, x='Time', y='R1Temp')
else:
fig_line = px.line(data_frame=df, x='Time', y='R3Temp')
if __name__=='__main__':
app.run_server()
解决方案
您没有在回调中返回任何内容。你应该返回这个数字,即你的回调应该是这样的,
@app.callback(Output('my-graph', 'figure'), Input('graph-choice', 'value'))
def interactive_graphs(selected_value):
if selected_value == 'R1Temp':
return px.line(data_frame=df, x='Time', y='R1Temp')
if selected_value == 'R2Temp':
return px.line(data_frame=df, x='Time', y='R1Temp')
return px.line(data_frame=df, x='Time', y='R3Temp')
推荐阅读
- java - Chromedriver 在 Teamcity 上无法执行,存在 linux-agent 问题
- php - Laravel 问题:为什么我的 foreach 循环在遍历数据库记录数组时返回意外输出
- python - 如何根据单元格中的值对 Pandas 中的出现次数求和
- html - 使用 CSS 溢出、剪辑路径,仅在 Safari Mobile 上遇到问题
- python - 删除具有一定数量 NA 的行
- .net - 确保所有invoke-sqlcmd 命令都发生在同一个事务中?
- r - 如何使用 R 上的“igraph”创建介数的视觉表示(在网络中)?
- forms - VS 2019 安装程序未对目标安装文件夹应用适当的权限
- karate - 如何通过空手道 DSL 中的先前场景响应创建动态 URL
- html - 嵌套单元格保持在右侧,而您可以在单元格左侧正常键入