当前位置: 首页 > 知识库问答 >
问题:

回调错误更新plot-div.children

孔阳炎
2023-03-14

我遇到了一个奇怪的行为——我在Plotly论坛和Stackoverflow上看到了类似的问题,但没有解决方案。基本上,我试图将中间值(在其他回调中重用)存储在隐藏的div“data storage json”中,但将其作为输入的回调似乎没有发生。后端没有错误。在前端,我得到“更新plot-div.children时出现回调错误”(这是指定为输出的组件)

import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table 
from dash.exceptions import PreventUpdate

########### Layout:
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[
    html.Div(id='data-storage-json', style={'display': 'none'}),
    html.Div(children=[
                dash_table.DataTable(
                        id='event-table',
                        style_data={'whiteSpace': 'normal'}, #'border': '1px solid blue'},
                        style_cell={'textAlign': 'center'},
                        #style_header={ 'border': '1px solid pink' },
                        css=[{
                            'selector': '.dash-cell div.dash-cell-value',
                            'rule': 'display: inline; white-space: inherit; overflow: inherit; text-overflow: inherit;'
                        }],
                        columns=[{"name": i, "id": i} for i in event_df.columns if i is not 'id'],
                        style_table={'overflowX': 'scroll'},
                        row_selectable='single',
                        selected_rows=[],
                        page_current=0,
                        page_size=PAGE_SIZE,
                        page_action='custom', 
                        filter_action='custom',
                        filter_query='',
                        sort_action='custom',
                        sort_mode='multi',
                        sort_by=[]                        
                  ),
                  html.Div(id='event-stats', style={'width': '80%', 'color': 'black', 'font-size': '9'})],
                  style={'width': '90%', 'margin-left': '20px', 'font-size': '9', 'horizontal-align': 'middle', 'vertical-align': 'middle'}),
    html.Div(children=[html.Br()]),
    html.Button('Plot', id='show-button'),
    html.Div(id='plot-div', children=[], style={'width': '95%', 'font-size': '9', 'vertical-align': 'middle'}),
])

########### Callbacks:

'''
Callback for sorting/filtering table
'''
@app.callback(
[Output('event-table', 'data'),
 Output('event-table', 'page_count'),
 Output('event-stats', 'children')],
[Input('event-table', 'sort_by'), 
 Input('event-table', 'filter_query'),
 Input('event-table', 'page_current'),
 Input('event-table', 'page_size')])
def update_event_selection(sort_by, filter_query,page_current, page_size):
    dff = sort_filter_table(event_df, filter_query, sort_by) 
    res = dff.iloc[page_current*page_size: (page_current + 1)*page_size]
    page_count = int(dff.shape[0]/page_size)+1
    stat_str = '{} events in the table. Displaying page {} of {}'.format(dff.shape[0], page_current+1, page_count)
    return res.to_dict('records'), page_count, stat_str

@app.callback(
Output('data-storage-json','children'),
[Input('show-button', 'n_clicks')],
[State('event-table','selected_row_ids')
])
def prepare_data(n_clicks,selected_id):
    duration=1
    print('Selected id: ',selected_id)
    if n_clicks is None or  selected_id is None or len(selected_id)==0:
        raise PreventUpdate
    duration=int(duration)
    selected_id=selected_id[0]
    row=event_df.loc[selected_id,:]
    print(row)
    event_time=pd.to_datetime(row['Start'],errors='ignore')

    # sensors to load:
    flist=['ip_m','vp_m','f','df']
    print('Duration {}'.format(duration))
    res_df=get_event_data(interconnect,event_time,duration, feature_list=flist)

    print(res_df.shape)
    js=res_df.to_json(date_format='iso', orient='split')
    print('In Prep: ',len(js))
    return js

@app.callback(
Output('plot-div','children'),
[Input('data-storage-json','children')],
[State('event-table','selected_row_ids')])
def generate_plots(data_storage,selected_id):
    if data_storage is None:
        print('None!!!')
        raise PreventUpdate
    else:
        print('InDisplay -storage: '+str(len(data_storage)))
        res_df = pd.read_json(data_storage, orient='split')

    print('InDisplay ',res_df.shape)
    selected_id=selected_id[0]
    row=event_df.loc[selected_id,:]
    event_time=pd.to_datetime(row['Start'],errors='ignore')
    event_type=row['Event']+': '+row['Cause']
    event_pid=''

    # columns sorted in reverse alphabetical
    flist=sorted(np.unique([c.split('__')[1] for c in res_df.columns]))[::-1]
    print('To plot: ',res_df.shape)
    # generate plots for each type of sensor:
    fig_list=[]
    for feature in flist:
        col_list = [c for c in res_df.columns if not c.startswith('_') and c.endswith('_'+feature)] 
        temp_df = res_df[col_list]
        # plot results
        print('Preparing figure '+feature)
        fig=temp_df.iplot(kind='scatter',mode='markers',size=3, title="Plot {}: {} {} {}".format(feature,event_time,event_type,event_pid), asFigure=True)
        #fig_list.append(fig)
        fig_list.append((html.Div(children=[dcc.Graph(id=feature+'-scatter',figure=fig)])))
    print('Figure done')
    return fig_list


########### Run the app:

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=0, help='number of GPU to use for calculations')
    parser.add_argument('--port', type=int, default=8050, help='port on which to run (default: 8050)')
    options,_ = parser.parse_known_args()
    os.environ['CUDA_VISIBLE_DEVICES'] = str(options.gpu)

    app.run_server(debug=True, port = options.port)

UPD:事件_df类似于:

event_df = pd.DataFrame({"id": [0,1,2],
    "Start": ["2016-01-01 14:33","2016-01-01 16:45","2016-01-01 17:46"], 
    "Event": ["Line Outage","Line Outage","Line Outage"],
     })

我还在下面的答案中包含了一个独立的代码示例

软件包版本:

dash                      1.8.0                      py_0    conda-forge
dash-core-components      1.7.0                      py_0    conda-forge
dash-html-components      1.0.2                      py_0    conda-forge
dash-renderer             1.2.3                      py_0    conda-forge
dash-table                4.6.0                      py_0    conda-forge

更新:最终问题似乎是由于数据帧的大小。隐藏的div或Store只能处理几百行。因此,我转而使用Flask缓存/记忆:参见https://dash.plotly.com/sharing-data-between-callbacks 或https://dash.plotly.com/performance

共有3个答案

凌钊
2023-03-14

我把它固定到我试图存储在隐藏div中的数据帧的大小。(没花多少时间就造成了错误)。我也试过用dcc。存储和观察相同的行为。因此,我切换到使用烧瓶缓存/回忆录:https://dash.plotly.com/sharing-data-between-callbacks或https://dash.plotly.com/performance

濮君植
2023-03-14

更新:我在下面提供了一个完整的例子。本示例使用随机生成的数据。它的工作原理,如果在第38行五分钟的数据生成。如果生成十分钟,我得到错误。

# -*- coding: utf-8 -*-
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table 
from dash.exceptions import PreventUpdate
external_stylesheets = ['https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css',
                                      'https://codepen.io/chriddyp/pen/bWLwgP.css',
                                      'https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.min.css',
                                      'https://codepen.io/chriddyp/pen/bWLwgP.css']

import numpy as np
import pandas as pd
from functools import reduce
import cufflinks as cf
from datetime import datetime as dt
import os
import sys
import argparse
#import plotly.offline

########### Prepare Data
PAGE_SIZE = 10

event_df = pd.DataFrame({"id": [0,1,2],
    "Start": ["2016-01-01 14:33","2016-01-01 16:45","2016-01-01 17:46"], 
    "Event": ["Line Outage","Line Outage","Line Outage"],
    "Cause": ['','','']
     })

def list2dict(l):
    return [{'label': x, 'value':x} for x in l]


def make_random_data():#(useDates=True):
    #if useDates:
    date_rng = pd.date_range(start='1/01/2018 05:00:00', end='1/01/2018 05:05:00', freq='1S')
    #else:
    #    date_rng = pd.Series([10, 20, 30, 40, 50]) 
    df = pd.DataFrame(date_rng, columns=['date'])
    cols=['A__ip_m','B__ip_m','A__vp_m','B__vp_m']
    for c in cols:
        df[c] = np.random.randint(0,100,size=(len(date_rng)))
    df=df.set_index('date')
    return df

########### Layout:
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[
    html.Div(id='data-storage-json', style={'display': 'none'}),
    html.Div(children=[
                dash_table.DataTable(
                        id='event-table',
                        data=event_df.to_dict('records'),
                        style_data={'whiteSpace': 'normal'},
                        style_cell={'textAlign': 'center'},
                        css=[{
                            'selector': '.dash-cell div.dash-cell-value',
                            'rule': 'display: inline; white-space: inherit; overflow: inherit; text-overflow: inherit;'
                        }],
                        columns=[{"name": i, "id": i} for i in event_df.columns if i is not 'id'],
                        style_table={'overflowX': 'scroll'},
                        row_selectable='single',
                        selected_rows=[]
                  )]),
    html.Div(children=[html.Br()]),
    html.Button('Plot', id='show-button'),
    html.Div(id='plot-div', children=[], style={'width': '95%', 'font-size': '9', 'vertical-align': 'middle'}),
])

########### Callbacks:

#Output('data-storage-json','children'),
# Output('plot-div','children'),
@app.callback(
Output('data-storage-json','children'),
[Input('show-button', 'n_clicks')],
[State('event-table','selected_row_ids')])
def prepare_data(n_clicks,selected_id):
    if n_clicks is None or  selected_id is None or len(selected_id)==0:
        raise PreventUpdate
    duration=1
    selected_id=selected_id[0]
    row=event_df.loc[selected_id,:]
    print(row)
    event_time=pd.to_datetime(row['Start'],errors='ignore')

    res_df = make_random_data()#useDates=True)
    print(res_df.shape)
    print(res_df.head())
    js=res_df.to_json(date_format='iso', orient='split') #date_format='epoch'
    #res_df.to_json('epoch-sample.json',date_format='epoch', orient='split')
    #res_df.to_json('iso-sample.json',date_format='iso', orient='split')
    print('In Prep: ',len(js))
    return js

@app.callback(
Output('plot-div','children'),
[Input('data-storage-json','children')])
def generate_plots(data_storage):
    if data_storage is None:
        print('None!!!')
        raise PreventUpdate
    else:
        print('InDisplay -storage: '+str(len(data_storage)))
        res_df = pd.read_json(data_storage, orient='split')

    # columns sorted in reverse alphabetical
    flist=sorted(np.unique([c.split('__')[1] for c in res_df.columns]))[::-1]
    print('To plot: ',res_df.shape)
    # generate plots for each type of sensor:
    fig_list=[]
    for feature in flist:
        col_list = [c for c in res_df.columns if not c.startswith('_') and c.endswith('_'+feature)] 
        temp_df = res_df[col_list]
        # plot results
        print('Preparing figure '+feature)
        fig=temp_df.iplot(kind='scatter',mode='markers',size=3, title="Plot", asFigure=True)
        fig_list.append((html.Div(children=[dcc.Graph(id=feature+'-scatter',figure=fig)])))
    print('Figure done')
    return fig_list

########### Run the app:

if __name__ == '__main__':
    app.run_server(debug=True)
凌蕴藉
2023-03-14

下面的(简化)代码适合我。因为您没有提供event\u df不可能看到您的确切问题是什么,但我怀疑event\u df中的'id'无效(例如,不是从0开始),并且您在这里的地址超出范围:

selected_id=selected_id[0]
row=event_df.loc[selected_id,:]

尽管它可能是任何数量的其他问题。如果你仍然有问题,也许你可以提供一个示例event_dfDataFrame?

还包括软件包版本供参考

import dash
import pandas as pd
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table 
from dash.exceptions import PreventUpdate

########### Layout:
app = dash.Dash(__name__)

event_df = pd.DataFrame({"id": [0,1,2], "a": [11,21,31], "b": [41,51,61]})
PAGE_SIZE=1

app.layout = html.Div(children=[
    html.Div(id='data-storage-json', style={'display': 'none'}),
    html.Div(children=[
                dash_table.DataTable(
                        id='event-table',
                        style_data={'whiteSpace': 'normal'}, #'border': '1px solid blue'},
                        style_cell={'textAlign': 'center'},
                        #style_header={ 'border': '1px solid pink' },
                        css=[{
                            'selector': '.dash-cell div.dash-cell-value',
                            'rule': 'display: inline; white-space: inherit; overflow: inherit; text-overflow: inherit;'
                        }],
                        columns=[{"name": i, "id": i} for i in event_df.columns if i is not 'id'],
                        style_table={'overflowX': 'scroll'},
                        row_selectable='single',
                        selected_rows=[],
                        page_current=0,
                        page_size=PAGE_SIZE,
                        page_action='custom', 
                        filter_action='custom',
                        filter_query='',
                        sort_action='custom',
                        sort_mode='multi',
                        sort_by=[]                        
                  ),
                  html.Div(id='event-stats', style={'width': '80%', 'color': 'black', 'font-size': '9'})],
                  style={'width': '90%', 'margin-left': '20px', 'font-size': '9', 'horizontal-align': 'middle', 'vertical-align': 'middle'}),
    html.Div(children=[html.Br()]),
    html.Button('Plot', id='show-button'),
    html.Div(id='plot-div', children=[], style={'width': '95%', 'font-size': '9', 'vertical-align': 'middle'}),
])

########### Callbacks:

'''
Callback for sorting/filtering table
'''
@app.callback(
Output('event-table', 'data'),
[Input('event-table', 'sort_by'), 
 Input('event-table', 'filter_query'),
 Input('event-table', 'page_current'),
 Input('event-table', 'page_size')])
def update_event_selection(sort_by, filter_query,page_current, page_size):

    return event_df.to_dict('records')

@app.callback(
Output('data-storage-json','children'),
[Input('show-button', 'n_clicks')],
[State('event-table','selected_row_ids')
])
def prepare_data(n_clicks,selected_id):
    duration=1

    print('Selected id: ',selected_id)

    if n_clicks is None or  selected_id is None or len(selected_id)==0:
        raise PreventUpdate

    duration=int(duration)
    selected_id=selected_id[0]
    row=event_df.loc[selected_id,:]
    print(row)

    res_df = pd.DataFrame({"id": [0,1,2], "a": [11,21,31], "b": [41,51,61]})
    js=res_df.to_json(date_format='iso', orient='split')
    print('In Prep: ',len(js))
    return js

@app.callback(
Output('plot-div','children'),
[Input('data-storage-json','children')],
[State('event-table','selected_row_ids')])
def generate_plots(data_storage,selected_id):
    if data_storage is None:
        print('None!!!')
        raise PreventUpdate
    else:
        print('InDisplay -storage: '+str(len(data_storage)))
        res_df = pd.read_json(data_storage, orient='split')

    print('InDisplay ',res_df.shape)
    selected_id=selected_id[0]
    row=event_df.loc[selected_id,:]
    event_time=pd.to_datetime(row['Start'],errors='ignore')
    event_type=row['Event']+': '+row['Cause']
    event_pid=''

    # columns sorted in reverse alphabetical
    flist=sorted(np.unique([c.split('__')[1] for c in res_df.columns]))[::-1]
    print('To plot: ',res_df.shape)
    # generate plots for each type of sensor:
    fig_list=[]
    for feature in flist:
        col_list = [c for c in res_df.columns if not c.startswith('_') and c.endswith('_'+feature)] 
        temp_df = res_df[col_list]
        # plot results
        print('Preparing figure '+feature)
        fig=temp_df.iplot(kind='scatter',mode='markers',size=3, title="Plot {}: {} {} {}".format(feature,event_time,event_type,event_pid), asFigure=True)
        #fig_list.append(fig)
        fig_list.append((html.Div(children=[dcc.Graph(id=feature+'-scatter',figure=fig)])))
    print('Figure done')
    return fig_list


########### Run the app:

if __name__ == '__main__':

    app.run_server(debug=True)


Running on http://127.0.0.1:8050/
Debugger PIN: 361-595-854
Selected id:  None
Selected id:  [2]
id     2
a     31
b     61
Name: 2, dtype: int64
In Prep:  81
InDisplay -storage: 81
InDisplay  (3, 3)

# Name                    Version                   Build  Channel
dash                      1.4.0                      py_0    conda-forge
dash-bootstrap-components 0.8.1                    py36_0    conda-forge
dash-core-components      1.3.0                      py_0    conda-forge
dash-html-components      1.0.1                      py_0    conda-forge
dash-renderer             1.1.1                      py_0    conda-forge
dash-table                4.4.0                      py_0    conda-forge
 类似资料:
  • 当我计算抛物线的新系数时,抛物线的图没有更新。当老鼠是一条抛物线和移动时,计算出新的系数。将显示图形上的新系数,但绘图保持不变。为什么这样?

  • 我在研究JNI的回电 null

  • 问题内容: 我在这里需要一些建议或一些解释。我有一个jquery ajax调用, 一切正常。我的回调响应正确触发。但是,我注意到的是,即使我的呼叫返回成功状态200,每次也会触发我的回调。在上面的回调中,我看到该对象是200。 谁能解释什么地方出了问题或这里发生了什么?回调应该仅在我有404或非200响应时触发。我的假设正确吗? 谢谢。 问题答案: 错误回调将在http错误时调用,但 如果响应上的

  • 我正在尝试使用具有唯一约束“phone\u number\u key”的jooq和table实现save或update方法 我的代码: 但我得到了例外 错误:重复的键值违反唯一约束“phone_number_key”详细信息:key(“phoneNumber”)=(99999999)已存在。 我使用“.onDuplicateKeyUpdate()”方法时遇到了相同的错误 我使用postgres d

  • 问题内容: 我有一个典型的架构和模型: 当我执行此更新时,它仅在定义回调时才有效,否则仅执行但数据库中的值未更改: 这有效: 这是一个错误吗?我没有在文档中看到是否需要回调,但是要求这样做很奇怪……我认为我在这里缺少了一些东西。 注意:我通过电子邮件匹配以测试建议,我在NodeJS v0.8.17中使用猫鼬v3.5.4,并具有简单的Express v3.0.6设置。 提前致谢。 问题答案: 用猫鼬

  • 这是我第一次使用公钥,我收到回拨错误。我一直在试图找到一个解决方案,我不确定它是否与设置或我正在使用的东西。 我的设置是2个虚拟机(Mint Linux托管服务器,Mac OS X托管客户端)都安装在桥接适配器上,都是运行Ubuntu 20.04的虚拟机,都是新安装的这些都是用于设置公钥的说明(我已经非常熟练地执行了这一操作,我已经做了好几次) https://www.linuxbabe.com/