如何创建一个回调函数,使其在 Python Dash 按钮工作时绘制图表。

huangapple go评论80阅读模式
英文:

How to make a callback function that draws a graph with a python dash button works

问题

我想在按下Python Dash按钮时绘制一个图表。
所以我创建了一个名为btn-run的按钮和一个名为btn_run_click的回调函数。
然而,当按下按钮时,不会绘制新的图表。下面是附加的代码。
有人可以帮忙吗?

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

app = dash.Dash(__name__)

app.layout = html.Div([
    html.Br(),
    html.Button('start', id='btn-run', n_clicks=0,
                style={'width':'150px','height':'30px'}),
    dcc.Graph(id='main-plot', figure={}),
    html.Br()
])

@app.callback(
    Output('main-plot', 'children'),
    Input('btn-run','n_clicks')
)
def btn_run_click(btn_run):
    
    if btn_run == 0 : return
    df = pd.DataFrame({
        "Fruits":['Apples', 'Oranges', 'Bananas'],
        "Amount":[4,3,6]
    })
    
    fig = px.bar(df, x="Fruits", y="Amount", barmode='group')
    return dcc.Graph(figure=fig)    

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

希望这能帮助您解决问题。

英文:

I want to draw a graph when I press the python dash button.
So I created a button called btn-run and a callback function called btn_run_click.
However, a new plot is not drawn when the button is pressed. The code is attached below.
Is there someone who can help?

import dash
from dash import Dash, dcc, html, Input, Output
import plotly.express as px
import pandas as pd
    
app = dash.Dash(__name__)

app.layout = html.Div([
    html.Br(),
    html.Button('start', id='btn-run', n_clicks=0,
                style={'width':'150px','height':'30px'}),
    dcc.Graph(id='main-plot', figure={}),
    html.Br()    
])

@app.callback(
    Output('main-plot', 'children'),
    Input('btn-run','n_clicks')
)
def btn_run_click(btn_run):
    
    if btn_run == 0 : return
    df = pd.DataFrame({
        "Fruits":['Apples', 'Oranges', 'Bananas'],
        "Amount":[4,3,6]
    })
    
    fig = px.bar(df, x = "Fruits", y = "Amount", barmode='group')
    return dcc.Graph(figure=fig)    

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

答案1

得分: 0

我对你的代码做了一些次要的更改。

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

app = dash.Dash(__name__)

app.layout = html.Div([
    html.Br(),
    html.Button('开始', id='btn-run', n_clicks=0,
                style={'width': '150px', 'height': '30px'}),
    dcc.Graph(id='main-plot', figure={}),
    html.Br()
])

@app.callback(
    # 将输出更改为 main-plot.figure
    Output('main-plot', 'figure'),
    Input('btn-run', 'n_clicks')
)
def btn_run_click(btn_run):
    #if btn_run == 0: return
    df = pd.DataFrame({
        "水果": ['苹果', '橙子', '香蕉'],
        "数量": [4, 3+btn_run, 6] 
    })

    fig = px.bar(df, x="水果", y="数量", barmode='group')
    # 只返回图形
    return fig

if __name__ == '__main__':
    app.run_server(debug=False, port=8052)
英文:

i made some minor changes to your code

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

app = dash.Dash(__name__)

app.layout = html.Div([
    html.Br(),
    html.Button('start', id='btn-run', n_clicks=0,
                style={'width': '150px', 'height': '30px'}),
    dcc.Graph(id='main-plot', figure={}),
    html.Br()
])


@app.callback(
    # change output to main-plot.figure
    Output('main-plot', 'figure'),
    Input('btn-run', 'n_clicks')
)
def btn_run_click(btn_run):
    #if btn_run == 0: return
    df = pd.DataFrame({
        "Fruits": ['Apples', 'Oranges', 'Bananas'],
        "Amount": [4, 3+btn_run, 6] 
    })

    fig = px.bar(df, x="Fruits", y="Amount", barmode='group')
    # just return the figure
    return fig


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

huangapple
  • 本文由 发表于 2023年3月12日 10:58:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定