Skip to content

Commit

Permalink
AbstractPlutoDingetjes. Display.with_js_link to request calculations …
Browse files Browse the repository at this point in the history
…and data from Julia dynamically
  • Loading branch information
fonsp committed Nov 21, 2023
1 parent 6224601 commit a153ec7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions frontend/components/CellOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ const execute_scripttags = async ({ root_node, script_nodes, previous_results_ma
// @ts-ignore
getPublishedObject: (id) => cell.getPublishedObject(id),

_internal_getJSLinkResponse: (cell_id, link_id) => (input) => pluto_actions.request_js_link_response(cell_id, link_id, input),

getBoundElementValueLikePluto: get_input_value,
setBoundElementValueLikePluto: set_input_value,
getBoundElementEventNameLikePluto: eventof,
Expand Down
13 changes: 13 additions & 0 deletions frontend/components/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,19 @@ export class Editor extends Component {
false
)
},
request_js_link_response: (cell_id, link_id, input) => {
return this.client
.send(
"request_js_link_response",
{
cell_id,
link_id,
input,
},
{ notebook_id: this.state.notebook.notebook_id }
)
.then((r) => r.message)
},
/** This actions avoids pushing selected cells all the way down, which is too heavy to handle! */
get_selected_cells: (cell_id, /** @type {boolean} */ allow_other_selected_cells) =>
allow_other_selected_cells ? this.state.selected_cells : [cell_id],
Expand Down
42 changes: 42 additions & 0 deletions src/runner/PlutoRunner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,9 @@ function run_expression(

# reset registered bonds
cell_registered_bond_names[cell_id] = Set{Symbol}()

# reset JS links
cell_js_links[cell_id] = Dict{String,Any}()

# If the cell contains macro calls, we want those macro calls to preserve their identity,
# so we macroexpand this earlier (during expression explorer stuff), and then we find it here.
Expand Down Expand Up @@ -990,6 +993,7 @@ const default_iocontext = IOContext(devnull,
:is_pluto => true,
:pluto_supported_integration_features => supported_integration_features,
:pluto_published_to_js => (io, x) -> core_published_to_js(io, x),
:pluto_with_js_link => (io, callback) -> core_with_js_link(io, callback),
)

const default_stdout_iocontext = IOContext(devnull,
Expand Down Expand Up @@ -1703,6 +1707,9 @@ const integrations = Integration[
if isdefined(AbstractPlutoDingetjes.Display, :published_to_js)
supported!(AbstractPlutoDingetjes.Display.published_to_js)
end
if isdefined(AbstractPlutoDingetjes.Display, :with_js_link)
supported!(AbstractPlutoDingetjes.Display.with_js_link)
end
end

end,
Expand Down Expand Up @@ -2486,6 +2493,41 @@ function Base.show(io::IO, m::MIME"text/html", e::DivElement)
Base.show(io, m, embed_display(e))
end


###
# JS LINK
###

const cell_js_links = Dict{UUID,Dict{String,Any}}()

function core_with_js_link(io, callback)

_notebook_id = get(io, :pluto_notebook_id, notebook_id[])::UUID
_cell_id = get(io, :pluto_cell_id, currently_running_cell_id[])::UUID

# TODO is this okay? prob not
# link_id = objectid2str(callback)
link_id = String(rand('a':'z', 16))

links = get!(() -> Dict{String,Any}(), cell_js_links, _cell_id)
links[link_id] = callback

write(io, "/* See the documentation for AbstractPlutoDingetjes.Display.with_js_link */ _internal_getJSLinkResponse(\"$(_cell_id)\", \"$(link_id)\")")
end

function evaluate_js_link(cell_id::UUID, link_id::String, input::Any)
links = get(() -> Dict{String,Any}(), cell_js_links, cell_id)
callback = get(links, link_id, nothing)
if callback === nothing
@error "🚨 AbstractPlutoDingetjes: JS link not found." link_id
else
result = callback(input)
assertpackable(result)

result
end
end

###
# LOGGING
###
Expand Down
18 changes: 18 additions & 0 deletions src/webserver/Dynamic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,24 @@ responses[:reshow_cell] = function response_reshow_cell(πŸ™‹::ClientRequest)
send_notebook_changes!(πŸ™‹ |> without_initiator)
end

responses[:request_js_link_response] = function response_request_js_link_response(πŸ™‹::ClientRequest)
require_notebook(πŸ™‹)
@assert will_run_code(πŸ™‹.notebook)

result = WorkspaceManager.eval_fetch_in_workspace(
(πŸ™‹.session, πŸ™‹.notebook),
quote
PlutoRunner.evaluate_js_link(
$(UUID(πŸ™‹.body["cell_id"])),
$(πŸ™‹.body["link_id"]),
$(πŸ™‹.body["input"]),
)
end
)

putclientupdates!(πŸ™‹.session, πŸ™‹.initiator, UpdateMessage(:🐀, result, nothing, nothing, πŸ™‹.initiator))
end

responses[:nbpkg_available_versions] = function response_nbpkg_available_versions(πŸ™‹::ClientRequest)
# require_notebook(πŸ™‹)
all_versions = PkgCompat.package_versions(πŸ™‹.body["package_name"])
Expand Down

0 comments on commit a153ec7

Please sign in to comment.