简体   繁体   English

需要 plotly 破折号 python 回调与输入、输出、按钮、图形的帮助

[英]Need help on plotly dash python callback with input, out, button, graph

I'm new at plotly dash python.我是 plotly 破折号 python 的新手。 I need help updating the graph after input value and clicking the Set button.在输入值并单击“设置”按钮后,我需要帮助更新图表。 I tried many ways but I can't make it right.我尝试了很多方法,但我无法使它正确。 Also, the refresh button is to refresh the graph back to 0 input value.此外,刷新按钮用于将图形刷新回 0 输入值。 I'm so stuck here.我被困在这里了。 Do I have to update the graph after the input or just update the value?我必须在输入后更新图表还是只更新值?

from dash import html, Dash, dcc, Input, Output, State
import plotly.express as px
import pandas as pd



app = Dash(__name__)



df = pd.DataFrame({
    "Minimum/Maximum":["Minimum", "Maximum"],
    "Numbers of Beat": [2,60]
    })
fig = px.bar(df, x = "Minimum/Maximum", y = "Numbers of Beat", color="Minimum/Maximum")

app.layout = html.Div(
    children=[
    html.H1(children= 'HTML Dashboard Application'),
    html.Div(children= 
             '''
             Dash: Minimum/Maximum Bar Graph
             '''),
    dcc.Graph(
        id='dash_graph',
        figure = fig
        ),
    html.Div(dcc.Input(placeholder='Enter a min value...' ,id='min_value', type='text')),
    html.Div(dcc.Input(placeholder='Enter a max value...' ,id='max_value', type='text')),
    html.Button(id='Set-val', n_clicks=0, children= 'Set'),
    html.Button(id='Refresh_current_BPM', n_clicks=0, children= 'Refresh'),
    ])
             
@app.callback(
    Output('dash_graph', 'figure'),
    Input('Set-val', 'n_clicks'),
    State('min_value', 'value'),
    State('max_value', 'value')
)
def update_value(min_value, max_value, n_clicks):
    #new value should appear in the graph here
    return fig
    
    
    

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

I need help updating the graph after input value and clicking the Set button.在输入值并单击“设置”按钮后,我需要帮助更新图表。 I tried many ways but I can't make it right.我尝试了很多方法,但我无法使它正确。

You have the design right for your callback (although in your provided code, you mixed the order of the arguments).您拥有回调的设计权(尽管在您提供的代码中,您混合了参数的顺序)。 You just have to reconstruct the dataframe with the appropriate values you capture from the inputs.您只需使用从输入中捕获的适当值重建 dataframe。 Since you pass both inputs min and max to the callback, you can reconstruct the dataframe, pass that into the plotly figure and return it back to the dcc.Graph component.由于您将输入minmax都传递给回调,因此您可以重建 dataframe,将其传递到 plotly 图并将其返回给dcc.Graph组件。

Also, the refresh button is to refresh the graph back to 0 input value.此外,刷新按钮用于将图形刷新回 0 输入值。 I'm so stuck here.我被困在这里了。 Do I have to update the graph after the input or just update the value?我必须在输入后更新图表还是只更新值?

You want to refresh the graph so that both subplots are zero, in which case you have 2 options:您想刷新图形,使两个子图都为零,在这种情况下,您有 2 个选项:

  1. You can set the inputs back to 0 in a new callback, which in turn will trigger the original callback you have and set the graphs to 0.您可以在新回调中将输入设置回 0,这反过来会触发您拥有的原始回调并将图表设置为 0。

  2. In your original callback, use dash.ctx.triggered to establish which button was clicked on, then set the dataframe values to 0 for both min and max.在您的原始回调中,使用dash.ctx.triggered确定单击了哪个按钮,然后将 dataframe 的最小值和最大值设置为 0。

The first solution might be more elegant in terms of design.第一个解决方案在设计方面可能更优雅。

Here is an example modified from original code:这是一个从原始代码修改的示例:

from dash import html, Dash, dcc, Input, Output, State
import plotly.express as px
import pandas as pd
import dash

app = Dash(__name__)

df = pd.DataFrame({
    "Minimum/Maximum":["Minimum", "Maximum"],
    "Numbers of Beat": [2,60]
    })
fig = px.bar(df, x = "Minimum/Maximum", y = "Numbers of Beat", color="Minimum/Maximum")

app.layout = html.Div(
    children=[
    html.H1(children= 'HTML Dashboard Application'),
    html.Div(children= 
             '''
             Dash: Minimum/Maximum Bar Graph
             '''),
    dcc.Graph(
        id='dash_graph'
    ),
    html.Div(dcc.Input(placeholder='Enter a min value...', id='min_value', type='number', value=0)),
    html.Div(dcc.Input(placeholder='Enter a max value...', id='max_value', type='number', value=0)),
    html.Button(id='Set-val', n_clicks=0, children= 'Set'),
    html.Button(id='Refresh_current_BPM', n_clicks=0, children= 'Refresh'),
    ])
             
@app.callback(
    Output('dash_graph', 'figure'),
    State('min_value', 'value'),
    State('max_value', 'value'),
    Input('Set-val', 'n_clicks'),
    Input('Refresh_current_BPM', 'n_clicks'),

)
def update_value(min_value, max_value, *_):
    # check if a button was triggered, if not then just render both plots with 0
    ctx = dash.callback_context
    if ctx.triggered:
        # grab the ID of the button that was triggered
        button_id = ctx.triggered[0]['prop_id'].split('.')[0]
        # if the button that was triggered is the refresh one, then set min,max to 0
        # otherwise, the set button was triggered and so carry on normally
        if button_id == "Refresh_current_BPM":
            min_value = 0
            max_value = 0

    df = pd.DataFrame({
    "Minimum/Maximum":["Minimum", "Maximum"],
    "Numbers of Beat": [min_value, max_value]
    })
    fig = px.bar(df, x = "Minimum/Maximum", y = "Numbers of Beat", color="Minimum/Maximum")
    return fig

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

NOTE笔记

Try not to use a global dataframe or variable in your dash app.尽量不要在仪表板应用程序中使用全局 dataframe 或变量。 What I did in the example is set the inputs to a default of 0, and just construct the graph in the callback that will be triggered upon the initial loading of the app.我在示例中所做的是将输入设置为默认值 0,并在回调中构造图形,该图形将在应用程序初始加载时触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM