【问题标题】:html.Button not worling with JupyterDash in Google colaboratoryhtml.Button 在 Google colaboratory 中无法与 Jupyter Dash 一起使用
【发布时间】:2021-02-18 11:23:48
【问题描述】:

我正在尝试创建一个带有按钮的仪表板,以在 google Colab 笔记本中触发破折号中的操作。

我有以下代码,使用 Dash(在 Pycharm 中)运行良好:

import dash
from dash.dependencies import Input, Output
import dash_html_components as html


app.layout = html.Div([
    html.Button('Button 1', id='btn-nclicks-1', n_clicks=0),
    html.Button('Button 2', id='btn-nclicks-2', n_clicks=0),
    html.Button('Button 3', id='btn-nclicks-3', n_clicks=0),
    html.Div(id='container-button-timestamp')
])

@app.callback(Output('container-button-timestamp', 'children'),
              [Input('btn-nclicks-1', 'n_clicks'),
               Input('btn-nclicks-2', 'n_clicks'),
               Input('btn-nclicks-3', 'n_clicks')])
def displayClick(btn1, btn2, btn3):
    changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
    if 'btn-nclicks-1' in changed_id:
        msg = 'Button 1 was most recently clicked'
    elif 'btn-nclicks-2' in changed_id:
        msg = 'Button 2 was most recently clicked'
    elif 'btn-nclicks-3' in changed_id:
        msg = 'Button 3 was most recently clicked'
    else:
        msg = 'None of the buttons have been clicked yet'
    return html.Div(msg)

app.run_server()

我有一个google colab,我使用 JupyterDash 而不是 Dash(如下):

!pip install jupyter_dash
from jupyter_dash import JupyterDash
from dash.dependencies import Input, Output
import dash_html_components as html

app = JupyterDash()

app.layout = html.Div([
    html.Button('Button 1', id='btn-nclicks-1', n_clicks=0),
    html.Button('Button 2', id='btn-nclicks-2', n_clicks=0),
    html.Button('Button 3', id='btn-nclicks-3', n_clicks=0),
    html.Div(id='container-button-timestamp')
])

@app.callback(Output('container-button-timestamp', 'children'),
              [Input('btn-nclicks-1', 'n_clicks'),
               Input('btn-nclicks-2', 'n_clicks'),
               Input('btn-nclicks-3', 'n_clicks')])
def displayClick(btn1, btn2, btn3):
    changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
    if 'btn-nclicks-1' in changed_id:
        msg = 'Button 1 was most recently clicked'
    elif 'btn-nclicks-2' in changed_id:
        msg = 'Button 2 was most recently clicked'
    elif 'btn-nclicks-3' in changed_id:
        msg = 'Button 3 was most recently clicked'
    else:
        msg = 'None of the buttons have been clicked yet'
    return html.Div(msg)

app.run_server(mode='inline')

当我运行它时,我得到一个带有三个按钮的仪表板,没有错误消息,但在单击按钮时它什么也不做。例如,我希望在单击按钮 3 后收到诸如“尚未单击任何按钮”(启动时)或“最近单击按钮 3”之类的消息。

是不是按钮在 JupityerDahs 中不起作用? 谢谢。

【问题讨论】:

    标签: button google-colaboratory dashboard plotly-dash html-components


    【解决方案1】:

    我修复了它(基于此colab)。

    !pip install -q jupyter-dash==0.3.0rc1 dash-bootstrap-components transformers
    import dash
    import dash_html_components as html
    import dash_core_components as dcc
    import dash_bootstrap_components as dbc
    from dash.dependencies import Input, Output, State
    from jupyter_dash import JupyterDash
    
    #app = JupyterDash()
    # Define app
    app = JupyterDash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
    server = app.server
    
    app.layout = html.Div([
        html.Button('Button 1', id='btn-nclicks-1', n_clicks=0),
        html.Button('Button 2', id='btn-nclicks-2', n_clicks=0),
        html.Button('Button 3', id='btn-nclicks-3', n_clicks=0),
        html.Div(id='container-button-timestamp')
    ])
    
    @app.callback(Output('container-button-timestamp', 'children'),
                  [Input('btn-nclicks-1', 'n_clicks'),
                   Input('btn-nclicks-2', 'n_clicks'),
                   Input('btn-nclicks-3', 'n_clicks')])
    def displayClick(btn1, btn2, btn3):
        changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
        if 'btn-nclicks-1' in changed_id:
            msg = 'Button 1 was most recently clicked'
        elif 'btn-nclicks-2' in changed_id:
            msg = 'Button 2 was most recently clicked'
        elif 'btn-nclicks-3' in changed_id:
            msg = 'Button 3 was most recently clicked'
        else:
            msg = 'None of the buttons have been clicked yet'
        return html.Div(msg)
    
    app.run_server(mode='external')
    

    【讨论】:

      猜你喜欢
      • 2020-07-24
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 2020-12-14
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多