【发布时间】:2020-05-26 21:13:47
【问题描述】:
我是 Dash 和 Plotly 生态系统的新手,几天前开始构建基于 Web 的仪表板。
这是一段sn-p代码:
import dash
import dash_html_components as html
import dash_core_components as dcc
# initialize the application
app = dash.Dash()
# define the layout of the app
app.layout = html.Div([
# add a date range selector
dcc.DatePickerRange(
id = 'my-date-picker-range',
min_date_allowed = dt(2010,1,4),
max_date_allowed = dt(2020, 12, 31),
initial_visible_month = dt(2020, 5, 23)
),
html.Div(id = 'output-container-date-picker-range'),
# add some markdown text
dcc.Markdown(f'''
This report covers the time period spanning {start_date} to {end_date}.
'''),
])
@app.callback(
dash.dependencies.Output('output-container-date-picker-range', 'children'),
[dash.dependencies.Input('my-date-picker-range', 'start_date'),
dash.dependencies.Input('my-date-picker-range', 'end_date')])
app.run_server(debug = True)
我正在尝试在降价文本中显示start_date 和end_date 变量(使用f string)。不幸的是,我收到以下错误消息:
NameError: name 'start_date' is not defined
是否可以在 Markdown 文本中包含变量输出?谢谢!
【问题讨论】:
-
也许你正在寻找这个: dcc.Markdown("#### 格式化文本," + variable_to_show + "!格式化继续:" + another_variable + ".") 而不是 f-string和三引号,您可以像字符串一样使用 + 号连接降价文本。
标签: python plotly markdown plotly-dash