python - 绘图破折号,带有 2 个按钮输入和一个下拉输入的回调

标签 python plotly-dash

如何编写一个回调,以便如果单击按钮 1,则执行 A;如果单击按钮 2,则执行 B;如果下拉值改变了,C 会做什么?

import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash(__name__)
app.layout = html.Div([
        html.H1('initial', id = 'h1'),
        html.Button('to upper', id = 'upper button', n_clicks_timestamp = '0'),
        html.Button('to lower', id = 'lower button', n_clicks_timestamp = '0'),
        dcc.Dropdown(options = [{'value':s, 'label': s} for s in ['good','bad']], value = 'good', id = 'set string')
    ])

@app.callback(
         dash.dependencies.Output('h1', 'children'),
         [dash.dependencies.Input('upper button', 'n_clicks_timestamp'), 
          dash.dependencies.Input('lower button', 'n_clicks_timestamp'), 
          dash.dependencies.Input('set string', 'value')],
         [dash.dependencies.State('h1', 'children')]
    )
def update(upper, lower, newstring, currentstring):
    upper, lower = int(upper), int(lower)

    # ???
    # if dropdown changed, return newstring
    # ???

    if upper > lower:
        return currentstring.upper()
    if lower > upper:
        return currentstring.lower()

    return newstring

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

由于下拉列表没有 timestamp 属性,因此无法确定它是否是最新更改。

最佳答案

在回调中按钮是否会被按下多次?如果没有,当下拉菜单触发回调时,按钮的 n_clicks 将为 0,n_clicks_timestamp 为 None(或者也是 0,我不记得了)。因此,您可以通过消除过程推断下拉菜单触发了回调。

如果按钮被按下多次,您需要在包含按钮的 Div 的 children 属性上创建另一个回调,然后简单地重新实例化按钮,以便它们仍然可以位于 0 n_clicks 和下一次回调的时间戳。

关于python - 绘图破折号,带有 2 个按钮输入和一个下拉输入的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54041817/

相关文章:

python - 使用深色主题 Bootstrap css 样式破折号组件

python - Dash CallBacks 一种获取函数的输出 Id 列表或静态变量的方法

python - 在 LINUX Redhat 服务器上本地部署基于 Python Flask 的 REST API

python - 将log转成json存入内存

python - 为什么 Flask Migrations 没有检测到字段的长度变化?

python - 如何在多页面应用程序上使用plotly-dash下载文件?

Python 将列表中的数据保存到 CSV 的列中

python - 如何按列名称对数据框的多个部分进行切片?

python - 从 Dash/Flask 应用程序下载动态生成的文件

datatable - Dash 数据表中的条件格式值 (Julia)