【问题标题】:Plotly Dash: How to reset the "n_clicks" attribute of a dash-html.button?Plotly Dash:如何重置 dash-html.button 的“n_clicks”属性?
【发布时间】:2020-07-01 06:55:16
【问题描述】:

我在 plotly/dash 中有一个基本数据表。我的目标是在按下上传按钮后上传(或打印示例......)。

问题是,我不知道如何将按钮的 n_clicks 属性恢复为零。所以发生的情况是,在我第一次单击按钮后,只要有变化(添加的行或更改/添加的数字),它就会连续打印,但我想要的是每当我单击按钮时它只打印一次。

这是代码:

import dash
from dash.dependencies import Input, Output, State
import dash_table
import dash_daq as daq

import dash_core_components as dcc
import dash_html_components as html
import pandas as pd

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


df = pd.read_csv('.../dummy_csv.csv')


app = dash.Dash(__name__)

app.layout = html.Div([
    html.Div(id='my-div'),

    dash_table.DataTable(
        id='adding-rows-table',
        style_data={
            'whiteSpace': 'normal',
            'height': 'auto'
        },
        style_table={
            'maxHeight': '800px'
            , 'overflowY': 'scroll'
        },
        columns=[
            {'name': i, 'id': i} for i in df.columns
        ],
        data=df.to_dict('records'),
        editable=True,
        row_deletable=True

    ),

    html.Button('+ Row', id='editing-rows-button', n_clicks=0),

    html.Button('Update', id='btn-nclicks-1', n_clicks=0 ),

])


@app.callback(
    Output(component_id='my-div', component_property='children'),
    [Input('btn-nclicks-1', 'n_clicks'), Input('adding-rows-table', 'data')]
)
def update_output_div(n_clicks, data):
    if n_clicks > 0:
        print(data)
        # n_clicks = 0
        # return n_clicks

    else:
        print("else loop")


@app.callback(
    Output('adding-rows-table', 'data'),
    [Input('editing-rows-button', 'n_clicks')],
    [State('adding-rows-table', 'data'),
     State('adding-rows-table', 'columns')])
def add_row(n_clicks, rows, columns):
    if n_clicks > 0:
        rows.append({c['id']: '' for c in columns})
    return rows


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

这是 csv:

a,b,c,d
1,1,5,1
2,2,5,1
2,33,6,2
3,4,6,2

and this the "faulty" output:

提前致谢,

最好的。

【问题讨论】:

    标签: python plotly-dash


    【解决方案1】:

    您可以使用dash.callback_context 属性仅在点击次数发生变化时触发回调,而不是在第一次点击后触发回调,请参阅 Dash 文档中关于“使用 callback_context 确定更改的按钮”部分:https://dash.plotly.com/dash-html-components/button .以下是如何更新回调的示例。

    @app.callback(Output(component_id='my-div', component_property='children'),
        [Input('btn-nclicks-1', 'n_clicks'), Input('adding-rows-table', 'data')])
    def update_output_div(n_clicks, data):
    
        changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
    
        if 'btn-nclicks-1' in changed_id:
    
            print(data)
            # n_clicks = 0
            # return n_clicks
    
        else:
    
            print("else loop")
    

    【讨论】:

    • 非常感谢!这行得通-尽管即使查看了您提到的文档,我也并不真正了解它的工作原理。您能否进一步详细说明您的解决方案?
    • @tristan,如果有帮助的话——dash.callback_context.triggered 会捕获自上次回调以来已更改的属性。这意味着即使在n_clicks 变为正数之后,它也可能不是dash.callback_context.triggered 的一部分。因此,使用后者可以防止不必要地触发基于n_clicks 的回调。
    猜你喜欢
    • 1970-01-01
    • 2021-03-03
    • 2019-04-08
    • 2023-03-05
    • 1970-01-01
    • 2020-03-07
    • 2019-03-12
    • 2022-01-17
    • 1970-01-01
    相关资源
    最近更新 更多