简体   繁体   中英

Dash Application Python Button for refresh the page

is there a possibility to write a callback function in Dash (Python) for a button to reload the page (like the updatebutton from the browser?

app.layout =html.Div([
            html.Button(id="refresh"),
            ])

@app.callback(Output('???', '???'),
              [Input('refresh', 'n_clicks')])
def refresh(n):

?
return
?

解决了!

html.A(html.Button('Refresh Data'),href='/'),

Another way to just make a command to refresh the page is: html.Meta(httpEquiv="refresh",content="60") .

This command tells the html to refresh the page after 60 seconds.

As stated by @hussam for a Multi-Page Dash app, you have to adjust it a bit.

Based on the simple Dash Multi app example , it would look like this:

from dash import Dash, dcc, html, callback, Input, Output

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

app = Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    # represents the browser address bar and doesn't render anything
    dcc.Location(id='url', refresh=False),

    dcc.Link('Navigate to "/"', href='/'),
    html.Br(),
    dcc.Link('Navigate to "/page-2"', href='/page-2'),

    # content will be rendered in this element
    html.Div(id='page-content')
])


@callback(
    Output('page-content', 'children'),
    [Input('url', 'pathname')])
def display_page(relative_pathname):
    return html.Div([
        html.H3(f'You are on page {relative_pathname}'),
        html.A(html.Button('Refresh Page'),href=relative_pathname),
    ])


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

With the input method for url :

[Input('url', 'pathname')])

You can get the relative url path, so everything after your main domain.

And with this input, you can setup aa refresh button for this page:

html.A(
    html.Button('Refresh Page'),
    href=relative_pathname

see example:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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