我偶然发现了这篇文章,并将最终答案的评论中提到的修改用于我自己的程序。但在我尝试使用以下代码对其进行测试之前:
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
app = dash.Dash()
daterange = pd.date_range(start='1994',end='2018',freq='W')
app.layout = html.Div(children=[
html.H1('Range Slider Testing'),
html.Div(
[
html.Label('From 1994 to 2018', id='time-range-label'),
dcc.RangeSlider(
id='year_slider',
min=daterange.min (),
max=daterange.max (),
value = [daterange.min(), daterange.max()],
step='W',
),
],
style={'margin-top': '20'}
),
html.Hr(),
dcc.Graph(id='my-graph')
])
@app.callback(
dash.dependencies.Output('my-graph', 'figure'),
[dash.dependencies.Input('year_slider', 'value')])
def _update_graph(year_range):
date_start = '{}'.format(year_range[0])
date_end = '{}'.format(year_range[1])
@app.callback(
dash.dependencies.Output('time-range-label', 'children'),
[dash.dependencies.Input('year_slider', 'value')])
def _update_time_range_label(year_range):
return 'From {} to {}'.format(year_range[0], year_range[1])
if __name__ == '__main__':
app.run_server()
因此,它不会引发任何 Python 错误,但由 Dash 创建的 HTML在其上加载依赖文本时出现错误...