Eric Stralsund
Eric Stralsund

Reputation: 559

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
?

Upvotes: 9

Views: 22210

Answers (3)

Rene
Rene

Reputation: 1214

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 a a refresh button for this page:

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

see example:

enter image description here

Upvotes: 3

OOCAZ
OOCAZ

Reputation: 53

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.

Upvotes: 4

Eric Stralsund
Eric Stralsund

Reputation: 559

solved!

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

Upvotes: 12

Related Questions