【问题标题】:Dependency issues with python's plotly and dashpython的情节和破折号的依赖性问题
【发布时间】:2019-07-05 14:46:51
【问题描述】:

我正在尝试使用 plotly 和破折号在 python 中构建一个仪表板,它将使用滑块来更改图形的范围(x 轴)。 x 轴包含日期。

我正在关注 plotly 网站 (https://dash.plot.ly/dash-core-components/rangeslider) 上的滑块示例。滑块示例在我的环境中运行良好。当我用数据框中的最小和最大日期值替换滑块的最小值和最大值时,如下所示:

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
    dcc.RangeSlider(
        id='slider',
        min = df['date'].min(),
        max = df['date'].max()
    ),
    html.Div(id='slider-container')
])

我的浏览器抛出以下错误:“加载依赖项时出错”。我的数据框中的“日期”是 pandas._libs.tslibs.timestamps.Timestamp

我已经按照https://github.com/plotly/dash/issues/82 卸载并重新安装了 dash、dash-renderer、dash-html-components、dash-core-components 和 plotly

对出了什么问题有什么建议吗?我的示例的完整代码是:

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
    dcc.RangeSlider(
        id='slider',
        min = df['date'].min(),
        max = df['date'].max()
    ),
    html.Div(id='slider-container') ])


@app.callback(
    dash.dependencies.Output('slider-container', 'children'),
    [dash.dependencies.Input('slider', 'value')])
def update_output(value):
    return 'You have selected "{}"'.format(value)


if __name__ == '__main__':
    app.run_server(debug=True)

如果滑块值设置为:

    dcc.RangeSlider(
        id='slider',
        min=0,
        max=20,
        step=0.5,
        value=[5, 15]

【问题讨论】:

  • “加载依赖项时出错”是 Dash 生成的错误,用于让您知道您的应用代码中存在问题,通常在回调函数中。安装可能没问题,问题出在您的应用程序本地。我会调查一下,看看能不能帮上忙。
  • 请添加一个示例 DF,最好直接在 python 代码中(例如df = pd.DataFrame({...}).

标签: python plotly dashboard plotly-dash


【解决方案1】:

似乎RangeSlider 不能直接与Timestamp 对象一起使用。您需要将它们转换为数字 (POSIX) 时间戳才能使用滑块,然后再将它们转换为回调函数内的对象。

以下内容对我有用:

import dash
import dash_core_components as dcc
import dash_html_components as html
import numpy as np
import pandas as pd

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

# Demo dataframe with a 'date' column
df = pd.DataFrame({'date': pd.to_datetime(np.linspace(1500000000, 1550000000, 9), unit='s')})

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
    dcc.RangeSlider(
        id='slider',
        min=df['date'].min().timestamp(),
        max=df['date'].max().timestamp(),
        value=[df['date'].min().timestamp(), df['date'].max().timestamp()],
    ),
    html.Div(id='slider-container')
])


@app.callback(
    dash.dependencies.Output('slider-container', 'children'),
    [dash.dependencies.Input('slider', 'value')])
def update_output(value):
    if value:
        return 'You have selected "{}"'.format(list(pd.to_datetime(value, unit='s')))


if __name__ == '__main__':
    app.run_server(debug=True)

【讨论】:

  • 谢谢。是的,这也适用于我。感谢您解决此问题(对于延迟标记为已接受表示抱歉)。
猜你喜欢
  • 1970-01-01
  • 2022-01-23
  • 2021-02-20
  • 2021-12-20
  • 1970-01-01
  • 2020-11-18
  • 2011-07-09
  • 2013-10-09
  • 2011-08-25
相关资源
最近更新 更多