9

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
?
0

3 Answers 3

12

solved!

html.A(html.Button('Refresh Data'),href='/'),
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! I think a looked for an hour for this nice workaround =)
This will not work is a more advance template that has a multi-page webapp.
4

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.

1 Comment

This should be the accepted answer for ease of use and least overhead.
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.