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

API for widgets to return possible bind values #1600

Merged
merged 23 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from 14 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
13 changes: 9 additions & 4 deletions frontend/common/SliderServerClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const slider_server_actions = ({ setStatePromise, launch_params, actions,
notebookfile_hash.then((x) => console.log("Notebook file hash:", x))

const bond_connections = notebookfile_hash
.then((hash) => fetch(trailingslash(launch_params.slider_server_url) + "bondconnections/" + encodeURIComponent(hash) + "/"))
.then((hash) => fetch(trailingslash(launch_params.slider_server_url) + "bondconnections/" + encodeURIComponent(hash) + ""))
.then((r) => r.arrayBuffer())
.then((b) => unpack(new Uint8Array(b)))

Expand All @@ -35,7 +35,12 @@ export const slider_server_actions = ({ setStatePromise, launch_params, actions,
console.debug("Requesting bonds", bonds_to_set.current, to_send)
bonds_to_set.current = new Set()

const mybonds_filtered = Object.fromEntries(Object.entries(mybonds).filter(([k, v]) => to_send.has(k)))
const mybonds_filtered = Object.fromEntries(
_.sortBy(
Object.entries(mybonds).filter(([k, v]) => to_send.has(k)),
([k, v]) => k
)
)

const packed = pack(mybonds_filtered)

Expand Down Expand Up @@ -75,11 +80,11 @@ export const slider_server_actions = ({ setStatePromise, launch_params, actions,
set_bond: async (symbol, value, is_first_value) => {
setStatePromise(
immer((state) => {
state.notebook.bonds[symbol] = { value: value, is_first_value: is_first_value }
state.notebook.bonds[symbol] = { value: value, is_first_value: true }
})
)
if (mybonds[symbol] == null || !_.isEqual(mybonds[symbol].value, value)) {
mybonds[symbol] = { value: _.cloneDeep(value), is_first_value: is_first_value }
mybonds[symbol] = { value: _.cloneDeep(value), is_first_value: true }
bonds_to_set.current.add(symbol)
await request_bond_response()
}
Expand Down
3 changes: 3 additions & 0 deletions src/evaluation/RunBonds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ function set_bond_values_reactive(; session::ServerSession, notebook::Notebook,

run_reactive_async!(session, notebook, to_reeval; deletion_hook=custom_deletion_hook, user_requested_run=false, run_async=false, kwargs...)
end

"Returns the set of all possible values for the binded variable `n` as returned by the widget implementation using `AbstractPlutoDingetjes.possible_bond_values(element)`"
possible_bond_values(session::ServerSession, notebook::Notebook, n::Symbol) = WorkspaceManager.possible_bond_values((session,notebook), n)
13 changes: 13 additions & 0 deletions src/evaluation/WorkspaceManager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ function bump_workspace_module(session_notebook::SN)
old_name, new_name
end

function possible_bond_values(session_notebook::SN, n::Symbol)
workspace = get_workspace(session_notebook)
pid = workspace.pid

possible_values = Distributed.remotecall_eval(Main, pid, quote
PlutoRunner.possible_bond_values($(QuoteNode(n)))
end)

@assert possible_values !== nothing "Tried to collect possible bind values when collection of possible bind values was not enabled"

possible_values
end

function create_emptyworkspacemodule(pid::Integer)::Symbol
Distributed.remotecall_eval(Main, pid, :(PlutoRunner.increment_current_module()))
end
Expand Down
2 changes: 1 addition & 1 deletion src/notebook/Notebook.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mutable struct BondValue
# This is only so the client can send this, the updater will always put this to `false`
is_first_value::Bool
end
function Base.convert(::Type{BondValue}, dict::Dict)
function Base.convert(::Type{BondValue}, dict::AbstractDict)
BondValue(dict["value"], something(get(dict, "is_first_value", false), false))
end

Expand Down
15 changes: 14 additions & 1 deletion src/runner/PlutoRunner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1297,12 +1297,14 @@ const integrations = Integration[
@assert v"1.0.0" <= AbstractPlutoDingetjes.MY_VERSION < v"2.0.0"
initial_value_getter_ref[] = AbstractPlutoDingetjes.Bonds.initial_value
transform_value_ref[] = AbstractPlutoDingetjes.Bonds.transform_value

possible_bond_values_ref[] = AbstractPlutoDingetjes.Bonds.possible_values

push!(supported_integration_features,
AbstractPlutoDingetjes,
AbstractPlutoDingetjes.Bonds,
AbstractPlutoDingetjes.Bonds.initial_value,
AbstractPlutoDingetjes.Bonds.transform_value,
AbstractPlutoDingetjes.Bonds.possible_values,
)
end,
),
Expand Down Expand Up @@ -1610,6 +1612,16 @@ function transform_bond_value(s::Symbol, value_from_js)
end
end

function possible_bond_values(s::Symbol)
element = get(registered_bond_elements, s, nothing)
return try
possible_bond_values_ref[](element)
catch e
@error "Could not collect possible bond values for $s" exception=(e, catch_backtrace())
Pangoraw marked this conversation as resolved.
Show resolved Hide resolved
rethrow("Could not collect possible bond values for $s, err = $(string(e))")
end
end

"""
_“The name is Bond, James Bond.”_

Expand Down Expand Up @@ -1652,6 +1664,7 @@ end

const initial_value_getter_ref = Ref{Function}(element -> missing)
const transform_value_ref = Ref{Function}((element, x) -> x)
const possible_bond_values_ref = Ref{Function}(_ -> throw("AbstractPlutoDingetjes is not loaded, could not collect possible bind values"))

"""
`@bind symbol element`
Expand Down
7 changes: 4 additions & 3 deletions test/Bonds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ import Pluto: update_run!, WorkspaceManager, ClientSession, ServerSession, Noteb
Base.show(io::IO, m::MIME"text/html", os::NewSlider) = write(io, "<input type=range value=1>")

Bonds.initial_value(os::NewSlider) = 1

Bonds.possible_values(s::NewSlider) = [1,2,3]
end
"""),
Cell("""
Expand Down Expand Up @@ -172,9 +174,8 @@ import Pluto: update_run!, WorkspaceManager, ClientSession, ServerSession, Noteb
@test notebook.cells[7].output.body == "false"
@test notebook.cells[8].output.body == "false"




@test Pluto.possible_bond_values(🍭, notebook, :x_new) == [1,2,3]

@test noerror(notebook.cells[9])
@test noerror(notebook.cells[10])
@test noerror(notebook.cells[11])
Expand Down