【问题标题】:How to use a boolean switch to update a graph in Dash?如何使用布尔开关更新 Dash 中的图形?
【发布时间】:2021-10-17 05:36:41
【问题描述】:

我是 Dash 的新手。我正在尝试使用daq.BooleanSwitch() 之类的输入来回调图形。我可以显示一条消息,但图表有问题。

有人有什么建议可以帮助我吗?

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

app = dash.Dash(__name__)

app.layout = html.Div([

    html.H1("Here we go"),

    daq.BooleanSwitch(id='my_pb', on=False,color="red"),
    
    html.Div(id='power-button-result-1'),
    
    dcc.Graph(id="plot")

])

@app.callback(
    Output('power-button-result-1', 'children'),
    Input('my_pb', 'on')
)
def update_output(on):
    x = '{}'.format(on)
    if x == "True":
        return "Hi Iḿ using DASH"

@app.callback(
    Output('plot', 'figure'),
    Input('my_pb', 'on')
)
    
def figura(on):
    x = '{}'.format(on)
    if x == "True":
        # fig1 = Code to do a nice plot 
        return fig1

if __name__ == "__main__":             
    app.run_server(port = 1895)  
 

我的 DASH 输出如下所示:

【问题讨论】:

    标签: python plotly plotly-dash plotly-python


    【解决方案1】:

    我查看了您的代码,需要进行一些更改:

    import dash
    import dash_daq as daq
    
    from dash import dcc
    from dash import html
    
    from dash.dependencies import Input
    from dash.dependencies import Output
    
    
    app = dash.Dash(__name__)
    
    app.layout = html.Div(
        [
            html.H1("Here we go"),
            daq.BooleanSwitch(id="my_pb", on=False, color="red"),
            html.Div(id="power-button-result-1"),
        ]
    )
    
    
    @app.callback(
        Output("power-button-result-1", "children"),
        Input("my_pb", "on"),
    )
    def update_output(on):
        x = "{}".format(on)
        if x == "True":
            return [dcc.Graph(id="plot")]
    
    
    if __name__ == "__main__":
        app.run_server(debug=True)
    
    

    你非常接近 - 我认为你只需要一个回调。在这里,您可以看到布尔开关现在切换 dcc.Graph 对象的显示(或不显示)。这是你要找的吗?

    ↓(切换开关)

    如果您希望图表已经显示,然后在切换时更新,这里是上面相同代码的稍微修改的扩展版本:

    import dash
    import dash_daq as daq
    
    from dash import dcc
    from dash import html
    from dash import no_update
    
    from dash.dependencies import Input
    from dash.dependencies import Output
    
    import plotly.express as px
    import pandas as pd
    
    
    app = dash.Dash(__name__)
    
    app.layout = html.Div(
        [
            html.H1("Here we go"),
            daq.BooleanSwitch(id="my_pb", on=False, color="red"),
            html.Div(
                [dcc.Graph(id="plot")], id="power-button-result-1"
            ),
        ]
    )
    
    
    @app.callback(
        Output("power-button-result-1", "children"),
        Input("my_pb", "on"),
    )
    def update_output(on):
        df = px.data.iris()
        if on:
            fig = px.scatter(df, x="sepal_width", y="sepal_length")
            dcc.Graph(figure=fig)
            return [dcc.Graph(figure=fig)]
        else:
            fig = px.scatter()
            return [dcc.Graph(figure=fig)]
    
    
    if __name__ == "__main__":
        app.run_server(debug=True)
    

    那里 - 好多了,希望有帮助吗?

    【讨论】:

    • 谢谢,这很有用:) 感谢您使用示例
    • 是的,我试过了,但显然我需要更多的声誉。对不起
    • 是的,我会回来的,我保证
    • 我做到了,谢谢约翰
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多