Plotly-Dash-datepickerage将不更新

2024-06-28 02:25:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为工作开发一个web应用程序,并尝试使用Dash DatePickerage特性创建一个动态过滤器。然而,当我运行代码时,应用程序可以正常打开,没有错误,但是当我尝试按日期筛选时,什么也没有发生。目前,我正在尝试将其应用于id为“timeWindow”的第一个图形。理想情况下,日期选择器将过滤Plotly.express图形使用的数据帧(timeF),仅过滤我选择的开始和结束日期

这是我的代码:

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

server = flask.Flask(__name__) # define flask app.server
app = dash.Dash(__name__, server=server, external_stylesheets=external_stylesheets)                                
  
# LAYOUT
app.layout = html.Div(children = [          # Full page container

    html.Div([                              # Header
        html.Div([], className = 'col-2'),
        html.Div([
            html.Img(src=app.get_asset_url('logo.png'), style={'float':'left'}),
            html.Img(src=app.get_asset_url('logo.png'),width='200', style={'float':'right'}),]),
        html.Div([
            html.H1(children='Title',
                style = {'textAlign':'center', 'padding-top' : '1%'},
                className = 'col-12'),  
                ]),
        html.Div([
            html.P('Instructions:',
                style = {'textAlign':'center', 'padding-left':'8%'},
                className = 'col-12'),
            html.P('Press the desired metric to adjust the graphs',
                style = {'textAlign':'center'},
                className = 'col-12'),
            html.P('3D Time Analysis: Double click to isolate a single newsletter. Double click again to return',
                style = {'textAlign':'center', 'padding-bottom' : '1%'},
                className = 'col-12'),
        ])

    ]),

    html.Hr(),

    # FILTERS 
    html.Div([
            dcc.RadioItems(
                id='yaxis-column',
                options=[{'label': i, 'value': i} for i in ['Median Open Rate (%)', 'Median Click Rate (%)']],
                value='Median Open Rate (%)',
                labelStyle={'display': 'inline-block'},
                className = 'container'
            ),
            dcc.DatePickerRange(
                    id='date-picker-range',
                    min_date_allowed=df['Date'].min(),
                    max_date_allowed=df['Date'].max(),
                    start_date_placeholder_text="Start Date",
                    end_date_placeholder_text="End Date",
                    initial_visible_month=date(2021, 3, 24),
                    start_date=df['Date'].min(),
                    end_date=df['Date'].max(),
                    style={'display':'inline-block', 'padding-left':'33%'}
                ),

            ],
            style={'width': '125%', 'display': 'inline-block'}),


    # GRAPHS
    html.Div(children = [
        html.Div([
            html.Div([
                dcc.Graph(id = 'timeWindow',figure = fig1, style={'display': 'inline-block', 'padding-left':'25%'}),
                dcc.Markdown('*Daily Time Windows*',
                    className = 'col-12',style={'textAlign':'center'})
            ], className = 'six columns'),

            html.Div([
                dcc.Graph(id='3dScatPlot', figure = fig4,style={'display': 'inline-block', 'padding-left':'25%'}),
                dcc.Markdown('*Interactive 3D Time Windows*',
                    className = 'col-12',style={'textAlign':'center'})
            ], className = 'six columns'),], className='row'),
        
        html.Hr(),

        html.Div([
            html.Div([
                dcc.Graph(id = 'trends',figure = fig5,style={'display': 'inline-block', 'padding-left':'26%'}),
                dcc.Markdown('*Newsletter Category Efficiencies*',
                    className = 'col-12',style={'textAlign':'center'})
            ], className = 'six columns'),

            html.Div([
                dcc.Graph(id = 'automated',figure = fig10,style={'display': 'inline-block', 'padding-left':'22%'}),
                dcc.Markdown('*Automated vs Written Newsletter Efficiencies*',
                    className = 'col-12',style={'textAlign':'center'})
            ], className = 'six columns')]),

        html.Hr(),

        html.Div([
            html.Div([
                dcc.Graph(id = 'wordAnalysis',figure = fig8, style={'display': 'inline-block', 'padding-left':'70%'}),
                dcc.Markdown('*Subject Title Efficiencies*',
                    style={'padding-left':'90%', 'textAlign':'center'})
            ], className = 'six columns')],  className='row'),
        
        ], id = 'graphs')

])

####### CALLBACKS ######
##### MOR vs. MCR Update
@app.callback(Output('timeWindow', 'figure'),
              [Input('yaxis-column', 'value'),
              Input('date-picker-range','start_date'),
              Input('date-picker-range','end_date')])

def update_graph1(value, start_date, end_date):
    dff = df[(df['Date'] >= pd.to_datetime([start_date])) & (df['Date'] <= pd.to_datetime([end_date]))]
    timeF = dff.copy()

    timeF = timeF.groupby(['Weekday','Hour']).agg({'Open %':'median','Click %':'median','Sends':'median'})
    timeF = timeF.reset_index()
    timeF['Weekday'] = timeF['Weekday'].apply(str)
    timeF['Weekday'] = timeF['Weekday'].replace(['1','2','3','4','5','6','7'],['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'])

    timeF = timeF[timeF['Hour'] > 5]
    timeF = timeF[timeF['Open %'] < 50]

    timeF.columns = ['Weekday','Hour','Median Open Rate (%)','Median Click Rate (%)','Sends']   
    

    if value == 'Median Open Rate (%)': 
        fig1 = px.bar(timeF, x = 'Hour', y = 'Median Open Rate (%)', 
                        facet_col='Weekday', color = "Sends", facet_col_wrap=4,facet_col_spacing=0.1, facet_row_spacing=0.15,
                        title = 'Time Windows', width = 625, height = 500)
        fig1.update_xaxes(tick0 = 6, dtick = 4)
        fig1.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
        return fig1
    elif value == 'Median Click Rate (%)':
        fig1 = px.bar(timeF, x = 'Hour', y = 'Median Click Rate (%)', 
                        facet_col='Weekday', color = "Sends", facet_col_wrap=4,facet_col_spacing=0.1, facet_row_spacing=0.15,
                        title = 'Time Windows', width = 625, height = 500)
        fig1.update_xaxes(tick0 = 6, dtick = 4)
        fig1.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
        return fig1
    else:
        fig1 = px.bar(timeF, x = 'Hour', y = 'Median Open Rate (%)', 
                        facet_col='Weekday', color = "Sends", facet_col_wrap=4,facet_col_spacing=0.1, facet_row_spacing=0.15,
                        title = 'Time Windows', width = 625, height = 500)
        fig1.update_xaxes(tick0 = 6, dtick = 4)
        fig1.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
        return fig1

我相信我的app.callback中有一个问题,我过去在使用多个过滤器时遇到过困难。此外,如果你看到一个更简单的方法来做任何事情,请让我知道

谢谢大家!


Tags: dividdateratestylehtmlcolleft