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

Feat/callback state #130

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions dash_uploader/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def wrapper(
uploaded_files_size,
total_files_size,
upload_id,
*args,
):
if not callbackbump:
raise PreventUpdate()
Expand All @@ -43,14 +44,15 @@ def wrapper(
total_size_mb=total_files_size,
upload_id=upload_id,
)
return callback(status)
return callback(status, *args)

return wrapper


def callback(
output,
id="dash-uploader",
state=None, # The state parameter is placed last in order to maintain better parameter order compatibility.
):
"""
Add a callback to dash application.
Expand Down Expand Up @@ -117,16 +119,22 @@ def add_callback(function):
# State: Pass along extra values without firing the callbacks.
#
# See also: https://dash.plotly.com/basic-callbacks
dash_callback = settings.app.callback(
output,
[Input(id, "dashAppCallbackBump")],
[
states = [
State(id, "uploadedFileNames"),
State(id, "totalFilesCount"),
State(id, "uploadedFilesSize"),
State(id, "totalFilesSize"),
State(id, "upload_id"),
],
]
if state:
if isinstance(state, list):
states.extend(state)
else:
states.append(state)
dash_callback = settings.app.callback(
output,
[Input(id, "dashAppCallbackBump")],
states,
**kwargs
)(dash_callback)

Expand Down
10 changes: 8 additions & 2 deletions usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import dash

if du.utils.dash_version_is_at_least("2.0.0"):
from dash import dcc
from dash import html # if dash <= 2.0.0, use: import dash_html_components as html
else:
import dash_html_components as html
import dash_core_components as dcc

from dash.dependencies import Output

from dash.dependencies import Output, State

app = dash.Dash(__name__)

Expand Down Expand Up @@ -49,6 +52,7 @@ def get_app_layout():
"display": "inline-block",
},
),
dcc.Store(id="example-token", storage_type="memory", data="some token")
],
style={
"textAlign": "center",
Expand All @@ -65,8 +69,10 @@ def get_app_layout():
@du.callback(
output=Output("callback-output", "children"),
id="dash-uploader",
state=State("example-token", "data"), # List of State also acceptable
)
def callback_on_completion(status: du.UploadStatus):
def callback_on_completion(status: du.UploadStatus, token):
print("example-token is ", token)
return html.Ul([html.Li(str(x)) for x in status.uploaded_files])


Expand Down