Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] background callback - progress key not deleted until the end #2408

Closed
Coding-with-Adam opened this issue Feb 1, 2023 · 0 comments · Fixed by #2415
Closed

[BUG] background callback - progress key not deleted until the end #2408

Coding-with-Adam opened this issue Feb 1, 2023 · 0 comments · Fixed by #2415

Comments

@Coding-with-Adam
Copy link

Community reported bug by Tphil10, as a DM to me. Bug description below:

We’ve [community members, Tim and Bryan] been tinkering around with some cool download animations for buttons but the way we’ve implemented is shines an odd issue with the background callback module.

The below code works great except for the time.sleep(1) line. If you increase this by more than 1 second, the download function downloads the file once a second, every second lol. I wanted to increase it so the checkmark animation stays a bit longer but alas, we can’t cause the file downloads multiple times. Thoughts? I think the problem is with the background callback.

import dash
import dash_mantine_components as dmc
from dash import html, Input, Output, DiskcacheManager, dcc
import diskcache
import time
import plotly.express as px

cache = diskcache.Cache('./cache')
background_callback_manager = DiskcacheManager(cache)

app = dash.Dash(
    __name__,
    suppress_callback_exceptions=True,
    background_callback_manager=background_callback_manager,
    external_stylesheets=['https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css']
)

download = html.I(className='fas fa-download fa-fw fa-2x')
loading = html.I(className='fas fa-spinner fa-fw fa-2x fa-spin')
complete = html.I(className='fas fa-check fa-fw fa-2x')


download_btn = dmc.ActionIcon(
        download,
        id='dload-btn',
        variant='filled',
        size='xl',
        p='1.5rem',
        style={'transition': 'all 0.5s ease-in-out'},
        color='blue',
    )

app.layout = html.Div(id='container', 
children=[
    download_btn,
    dcc.Download(id='download')
])


@app.callback(
    Output('dload-btn', 'n_clicks'),
    Input('dload-btn', 'n_clicks'),
    prevent_initial_call=True,
    background=True,
    running=[
         (Output('dload-btn', 'disabled'), True, False),
         (Output('dload-btn','children'), loading, download),
         (Output('dload-btn','className'), None, None)
    ],
    progress=[Output('dload-btn','children'),
              Output('dload-btn','className'),
              Output('download','data')],
)
def downloader(set_progress, _):

    time.sleep(3)

    df = px.data.gapminder()
    set_progress([complete, 'success', dcc.send_data_frame(df.to_csv, "mydf.csv")])
    time.sleep(1)

    return dash.no_update


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

style.css file:

.success {
    border-radius: 50%;
    color: #fff !important;
    background-color: #80bca3 !important;
}

Plotly staff member, Philippe, looked into this problem. The ostensible culprit is:

I've found the reason why it does it twice, we just never delete the progress key until the end, for most case it doesn't matter, but with the download it triggers it everytime it's updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant