Plotly-Dash有未知问题并且会创建“#34;错误加载依赖关系”#34;通过使用Python-pandas.date_range

时间:2018-03-19 19:49:14

标签: python plotly plotly-dash

我偶然发现了this帖子,并使用了我自己的程序最终答案评论中提到的修改。但在我尝试使用以下代码进行测试之前:

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上有一个错误加载依赖文本...

1 个答案:

答案 0 :(得分:3)

默认情况下,看起来像破折号的不支持日期时间对象。

您可以通过将datetime对象转换为unix时间戳来解决此问题。

我为您解决的问题的解决方案是:

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd

import time

app = dash.Dash()

daterange = pd.date_range(start='1994',end='2018',freq='W')

def unixTimeMillis(dt):
    ''' Convert datetime to unix timestamp '''
    return int(time.mktime(dt.timetuple()))

def unixToDatetime(unix):
    ''' Convert unix timestamp to datetime. '''
    return pd.to_datetime(unix,unit='s')

def getMarks(start, end, Nth=100):
    ''' Returns the marks for labeling. 
        Every Nth value will be used.
    '''

    result = {}
    for i, date in enumerate(daterange):
        if(i%Nth == 1):
            # Append value to dict
            result[unixTimeMillis(date)] = str(date.strftime('%Y-%m-%d'))

    return result

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 = unixTimeMillis(daterange.min()),
                max = unixTimeMillis(daterange.max()),
                value = [unixTimeMillis(daterange.min()),
                         unixTimeMillis(daterange.max())],
                marks=getMarks(daterange.min(),
                            daterange.max()),
            ),
        ],
        style={'margin-top': '20'}
    ),
    html.Hr(),

    dcc.Graph(id='my-graph')
])

@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(unixToDatetime(year_range[0]),
                                  unixToDatetime(year_range[1]))

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