• <i id='C1CTp'><tr id='C1CTp'><dt id='C1CTp'><q id='C1CTp'><span id='C1CTp'><b id='C1CTp'><form id='C1CTp'><ins id='C1CTp'></ins><ul id='C1CTp'></ul><sub id='C1CTp'></sub></form><legend id='C1CTp'></legend><bdo id='C1CTp'><pre id='C1CTp'><center id='C1CTp'></center></pre></bdo></b><th id='C1CTp'></th></span></q></dt></tr></i><div id='C1CTp'><tfoot id='C1CTp'></tfoot><dl id='C1CTp'><fieldset id='C1CTp'></fieldset></dl></div>

    • <bdo id='C1CTp'></bdo><ul id='C1CTp'></ul>
    <tfoot id='C1CTp'></tfoot><legend id='C1CTp'><style id='C1CTp'><dir id='C1CTp'><q id='C1CTp'></q></dir></style></legend>
  • <small id='C1CTp'></small><noframes id='C1CTp'>

      1. Plotly-Dash 存在未知问题并创建“加载依赖项时出错";通过使用 Python-pandas.date

        时间:2023-09-28
        1. <small id='OSBfM'></small><noframes id='OSBfM'>

        2. <i id='OSBfM'><tr id='OSBfM'><dt id='OSBfM'><q id='OSBfM'><span id='OSBfM'><b id='OSBfM'><form id='OSBfM'><ins id='OSBfM'></ins><ul id='OSBfM'></ul><sub id='OSBfM'></sub></form><legend id='OSBfM'></legend><bdo id='OSBfM'><pre id='OSBfM'><center id='OSBfM'></center></pre></bdo></b><th id='OSBfM'></th></span></q></dt></tr></i><div id='OSBfM'><tfoot id='OSBfM'></tfoot><dl id='OSBfM'><fieldset id='OSBfM'></fieldset></dl></div>
            <bdo id='OSBfM'></bdo><ul id='OSBfM'></ul>

                  <tbody id='OSBfM'></tbody>
                <legend id='OSBfM'><style id='OSBfM'><dir id='OSBfM'><q id='OSBfM'></q></dir></style></legend>

                  <tfoot id='OSBfM'></tfoot>

                • 本文介绍了Plotly-Dash 存在未知问题并创建“加载依赖项时出错";通过使用 Python-pandas.date_range的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I stumbled upon this post and used the modification mentioned in the comment of the final answer for my own program. But before I tried to test it with the following code:

                  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 上有 Error loading dependencies 文本...

                  As a result it doesn't raise any Python errors, but the HTML, created by Dash, has an Error loading dependencies text on it...

                  推荐答案

                  似乎 dash 默认情况下不支持 datetime 对象.

                  Seems like dash does not support datetime objects by default.

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

                  You can solve this issue by converting your datetime object into an unix timestamp.

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

                  My solution for your problem is:

                  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()
                  

                  这篇关于Plotly-Dash 存在未知问题并创建“加载依赖项时出错";通过使用 Python-pandas.date_range的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:颜色条最小值和最大值 下一篇:向 hovertext 标签添加其他文本

                  相关文章

                  <i id='MZxrF'><tr id='MZxrF'><dt id='MZxrF'><q id='MZxrF'><span id='MZxrF'><b id='MZxrF'><form id='MZxrF'><ins id='MZxrF'></ins><ul id='MZxrF'></ul><sub id='MZxrF'></sub></form><legend id='MZxrF'></legend><bdo id='MZxrF'><pre id='MZxrF'><center id='MZxrF'></center></pre></bdo></b><th id='MZxrF'></th></span></q></dt></tr></i><div id='MZxrF'><tfoot id='MZxrF'></tfoot><dl id='MZxrF'><fieldset id='MZxrF'></fieldset></dl></div>

                    • <bdo id='MZxrF'></bdo><ul id='MZxrF'></ul>

                    <small id='MZxrF'></small><noframes id='MZxrF'>

                      <legend id='MZxrF'><style id='MZxrF'><dir id='MZxrF'><q id='MZxrF'></q></dir></style></legend><tfoot id='MZxrF'></tfoot>