Skip to content

Commit

Permalink
add background field
Browse files Browse the repository at this point in the history
  • Loading branch information
BeastyBlacksmith committed Apr 3, 2024
1 parent 5b83d43 commit cf15afa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/app/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ function callback!(func::Union{Function, ClientsideFunction, String},
output::Union{Vector{<:Output}, Output},
input::Union{Vector{<:Input}, Input},
state::Union{Vector{<:State}, State} = State[];
prevent_initial_call = nothing
kwargs...
)
return _callback!(func, app, CallbackDeps(output, input, state), prevent_initial_call = prevent_initial_call)
return _callback!(func, app, CallbackDeps(output, input, state); kwargs...)
end

"""
Expand Down Expand Up @@ -99,13 +99,13 @@ end
function callback!(func::Union{Function, ClientsideFunction, String},
app::DashApp,
deps::Dependency...;
prevent_initial_call = nothing
kwargs...
)
output = Output[]
input = Input[]
state = State[]
_process_callback_args(deps, (output, input, state))
return _callback!(func, app, CallbackDeps(output, input, state, length(output) > 1), prevent_initial_call = prevent_initial_call)
return _callback!(func, app, CallbackDeps(output, input, state, length(output) > 1); kwargs...)
end

function _process_callback_args(args::Tuple{T, Vararg}, dest::Tuple{Vector{T}, Vararg}) where {T}
Expand All @@ -124,7 +124,10 @@ function _process_callback_args(args::Tuple{}, dest::Tuple{})
end


function _callback!(func::Union{Function, ClientsideFunction, String}, app::DashApp, deps::CallbackDeps; prevent_initial_call)
function _callback!(func::Union{Function, ClientsideFunction, String}, app::DashApp, deps::CallbackDeps;
prevent_initial_call = nothing,
background = false,
manager = nothing)

check_callback(func, app, deps)

Expand All @@ -138,7 +141,8 @@ function _callback!(func::Union{Function, ClientsideFunction, String}, app::Dash
deps,
isnothing(prevent_initial_call) ?
get_setting(app, :prevent_initial_callbacks) :
prevent_initial_call
prevent_initial_call,
background
)
)
end
Expand Down
1 change: 1 addition & 0 deletions src/app/supporttypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ struct Callback
func ::Union{Function, ClientsideFunction}
dependencies ::CallbackDeps
prevent_initial_call ::Bool
background ::Bool
end

is_multi_out(cb::Callback) = cb.dependencies.multi_out == true
Expand Down
18 changes: 18 additions & 0 deletions test/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,21 @@ end
@test occursin("clientside[\"_dashprivate_my-div\"]", body)
@test occursin("ns[\"children\"]", body)
end

@testset "Background callbacks" begin
app = dash()
app.layout = html_div() do
html_div(html_p(id = "paragraph_id", children = ["Button not clicked"]))
html_button(id="buttton_id", children = "Run Job!")
end

callback!(app, Output("paragraph_id", "children"), Input("button_id", "n_clicks"), background = true) do clicks
sleep(2)
return string("Clicked ", clicks, " times")
end

handler = make_handler(app)
request = HTTP.Request("GET", "/_dash-dependencies")
resp = Dash.HttpHelpers.handle(handler, request)
deps = JSON3.read(String(resp.body))
end

0 comments on commit cf15afa

Please sign in to comment.