使用下拉列表选择问题加载 Plotly Dash 数据表
Issue loading Plotly Dash datatable with drop downlist selection
我在选择下拉列表时加载破折号数据 table 时遇到问题。图表能够加载但不能加载破折号数据table。没有抛出错误。任何人都可以帮助告知这里的错误是什么?
提前致谢。
下面是代码片段
'''
服务器 = flask.Flask(名称)
app = dash.Dash(name, server=server)
# Populate the layout with HTML and graph components
app.layout = html.Div([
html.H5("SALES Report"),
#Drop Down
html.Div(
[
dcc.Dropdown(
id="SALESRange",
options=[{
'label': i,
'value': i
} for i in SALES_Range_Options],
value='All SALES Range'
)
],
style={'width': '25%',
'display': 'inline-block'}),
#CHART
html.Div([dcc.Graph(id='summarybargraph') ]),
#TABLE
html.H6("Details list"),
html.Div([
dash_table.DataTable(id='detailsresults') ]),
])
# Add the callbacks to support the interactive componets
@app.callback(
Output('summarybargraph', 'figure'),
[Input('SALESRange', 'value')])
def generate_graph(SALESRange):
if SALESRange == "All SALES Range" :
df_plot = SALES_Range_CAT_df.copy()
else:
df_plot = SALES_Range_CAT_df[SALES_Range_CAT_df['SALES_RANGE'] == SALESRange]
trace1 = go.Bar(x=df_plot['SALES_RANGE'],
y=df_plot['Patron_count'],
name='No of Patrons',
text =df_plot['Patron_count'],
#textposition='auto',
#marker_color=colors # marker color can be a single color value or an iterable
)
return {
'data': [trace1],
'layout':
go.Layout(
title='SALES Range for {}'.format(SALESRange)
)
}
def generate_table(dataframe):
'''Given dataframe, return template generated using Dash components
'''
return html.Div( [dash_table.DataTable(
#id='match-results',
data=dataframe.to_dict('records'),
columns=[{"name": i, "id": i} for i in dataframe.columns],
style_header={'backgroundColor': "#FFD700",
'fontWeight': 'bold',
'textAlign': 'center',},
style_table={'overflowX': 'scroll'},
style_cell={'minWidth': '180px', 'width': '180px',
'maxWidth': '180px','whiteSpace': 'normal'},
row_selectable="multi"),
html.Hr()
])
@app.callback(
Output('detailsresults', 'data'),
[
Input('SALESRange', 'value'),
]
)
def load_results(SALESRange):
results = GR_df[GR_df['SALES_RANGE'] == SALESRange]
return generate_table(results)
if __name__ == '__main__':
app.run_server(debug=False)
'''
我认为问题在于,您的 generate_table
函数 returns 和 Div
。您的回调尝试将 Div
作为 data
.
添加到您的 DataTable
在布局中尝试将图表更改为
#TABLE
html.H6("Details list"),
html.Div(id='detailsresults')),
并将您的回调更改为:
@app.callback(
Output('detailsresults', 'childern'),
[Input('SALESRange', 'value'),])
我在选择下拉列表时加载破折号数据 table 时遇到问题。图表能够加载但不能加载破折号数据table。没有抛出错误。任何人都可以帮助告知这里的错误是什么? 提前致谢。
下面是代码片段
''' 服务器 = flask.Flask(名称) app = dash.Dash(name, server=server)
# Populate the layout with HTML and graph components
app.layout = html.Div([
html.H5("SALES Report"),
#Drop Down
html.Div(
[
dcc.Dropdown(
id="SALESRange",
options=[{
'label': i,
'value': i
} for i in SALES_Range_Options],
value='All SALES Range'
)
],
style={'width': '25%',
'display': 'inline-block'}),
#CHART
html.Div([dcc.Graph(id='summarybargraph') ]),
#TABLE
html.H6("Details list"),
html.Div([
dash_table.DataTable(id='detailsresults') ]),
])
# Add the callbacks to support the interactive componets
@app.callback(
Output('summarybargraph', 'figure'),
[Input('SALESRange', 'value')])
def generate_graph(SALESRange):
if SALESRange == "All SALES Range" :
df_plot = SALES_Range_CAT_df.copy()
else:
df_plot = SALES_Range_CAT_df[SALES_Range_CAT_df['SALES_RANGE'] == SALESRange]
trace1 = go.Bar(x=df_plot['SALES_RANGE'],
y=df_plot['Patron_count'],
name='No of Patrons',
text =df_plot['Patron_count'],
#textposition='auto',
#marker_color=colors # marker color can be a single color value or an iterable
)
return {
'data': [trace1],
'layout':
go.Layout(
title='SALES Range for {}'.format(SALESRange)
)
}
def generate_table(dataframe):
'''Given dataframe, return template generated using Dash components
'''
return html.Div( [dash_table.DataTable(
#id='match-results',
data=dataframe.to_dict('records'),
columns=[{"name": i, "id": i} for i in dataframe.columns],
style_header={'backgroundColor': "#FFD700",
'fontWeight': 'bold',
'textAlign': 'center',},
style_table={'overflowX': 'scroll'},
style_cell={'minWidth': '180px', 'width': '180px',
'maxWidth': '180px','whiteSpace': 'normal'},
row_selectable="multi"),
html.Hr()
])
@app.callback(
Output('detailsresults', 'data'),
[
Input('SALESRange', 'value'),
]
)
def load_results(SALESRange):
results = GR_df[GR_df['SALES_RANGE'] == SALESRange]
return generate_table(results)
if __name__ == '__main__':
app.run_server(debug=False)
'''
我认为问题在于,您的 generate_table
函数 returns 和 Div
。您的回调尝试将 Div
作为 data
.
在布局中尝试将图表更改为
#TABLE
html.H6("Details list"),
html.Div(id='detailsresults')),
并将您的回调更改为:
@app.callback(
Output('detailsresults', 'childern'),
[Input('SALESRange', 'value'),])