"Dash plotly":我在将两个输入连接到一个输出时遇到问题,而且 bas x 轴未排序和正确分类

"Dash plotly": I face a problem in connecting two inputs to an output also the bas x axis is not sorted and correctly categorized

这是 100% 错误的自定义函数。根据两个不同的输入过滤图形的正确代码是什么?

app.layout = html.Div([
html.Div([
    html.Div([
        html.H3('Select a Country'),
        dcc.Dropdown(id = 'drop',
options =[{'label': i, 'value': i} for i in df["Country"].unique()] + [{'label': 'Select all', 'value': 'All'}], 
value='All')
    ],style={'width': '35%','display':'inline-block'}),

    html.Div([
        html.H3('Select a Reason'),
        dcc.Dropdown(id = 'drop1',
options =[{'label': i, 'value': i} for i in df["Source"].unique()] + [{'label': 'Select all', 'value': 'All'}], 
value='All')
    ],style={'width': '35%','display':'inline-block'}),
], className="row"),
  html.Div(dcc.Graph(
 id = "graph"))
],style={'width': '100%', 'display': 'inline-block'})

@app.callback(Output('graph','figure'),
          [Input('drop', 'value'),Input('drop1', 'value')])
          



def update_graph(country,reason):
if (country == 'value') | (reason == 'value'):
    dff=df

else:
    
    dff = df[(df['Country']==country) &
            (df['Source']==reason)]


fig = go.Figure([go.Bar(x=dff["Week"], y=dff["Source"].value_counts())]) 
      
fig.update_layout(barmode='relative', title_text='Number of Orders')
fig.update_xaxes(type='category')
return fig
  • 已经模拟了你的数据框
  • 你有逻辑炸弹而不是结构问题
    1. if (country == 'All') or (reason == 'All'): 回调 数据帧过滤的条件逻辑
    2. 人物创作,没有意义所以更换
import dash
from dash import html, dcc
from jupyter_dash import JupyterDash
import plotly.graph_objects as go
from dash.dependencies import Input, Output, State
import pandas as pd
import numpy as np

app = JupyterDash(__name__)

S = 100
df = pd.DataFrame({"Country":np.random.choice(["UK","US"], S), "Source":np.random.choice(list("ABC"), S),
                  "Date":pd.date_range("1-jan-2021",freq="D",periods=S)}).pipe(lambda d: d.assign(Week=d["Date"].dt.week))

app.layout = html.Div(
    [
        html.Div(
            [
                html.Div(
                    [
                        html.H3("Select a Country"),
                        dcc.Dropdown(
                            id="drop",
                            options=[
                                {"label": i, "value": i} for i in df["Country"].unique()
                            ]
                            + [{"label": "Select all", "value": "All"}],
                            value="All",
                        ),
                    ],
                    style={"width": "35%", "display": "inline-block"},
                ),
                html.Div(
                    [
                        html.H3("Select a Reason"),
                        dcc.Dropdown(
                            id="drop1",
                            options=[
                                {"label": i, "value": i} for i in df["Source"].unique()
                            ]
                            + [{"label": "Select all", "value": "All"}],
                            value="All",
                        ),
                    ],
                    style={"width": "35%", "display": "inline-block"},
                ),
            ],
            className="row",
        ),
        html.Div(dcc.Graph(id="graph")),
    ],
    style={"width": "100%", "display": "inline-block"},
)

@app.callback(Output('graph','figure'),
          [Input('drop', 'value'),Input('drop1', 'value')])
def update_graph(country,reason):
    if (country == 'All') or (reason == 'All'):
        dff=df
    else:
        dff = df[(df['Country']==country) &
            (df['Source']==reason)]

    dff = dff.drop(columns=["Date"]).value_counts().reset_index()
    fig = go.Figure([go.Bar(x=dff["Week"], y=dff[0])])
#     fig = go.Figure([go.Bar(x=dff["Week"], y=dff["Source"].value_counts())])

    fig.update_layout(barmode='relative', title_text='Number of Orders')
    fig.update_xaxes(type='category')
    return fig

app.run_server(mode="inline")