Skip to content

Commit

Permalink
Use the server session to get bond names instead of parsing the noteb…
Browse files Browse the repository at this point in the history
…ook code (#97)

Co-authored-by: Fons van der Plas <[email protected]>
  • Loading branch information
disberd and fonsp authored Dec 13, 2022
1 parent 71e9041 commit ddd9e72
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ GitHubActions = "0.1"
Glob = "1"
HTTP = "^1.0.2"
JSON = "0.21"
Pluto = "0.19.13"
Pluto = "0.19.17"
TerminalLoggers = "0.1"
julia = "1.6"

Expand Down
3 changes: 2 additions & 1 deletion src/Actions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ function process(
end
try_tocache(settings.Export.cache_dir, new_hash, original_state)
if keep_running
bond_connections = bound_variable_connections_graph(notebook)
bond_connections =
bound_variable_connections_graph(server_session, notebook)
@info "Bond connections" s.path showall(collect(bond_connections))

RunningNotebook(; path, notebook, original_state, bond_connections)
Expand Down
38 changes: 3 additions & 35 deletions src/MoreAnalysis.jl
Original file line number Diff line number Diff line change
@@ -1,35 +1,5 @@
import Pluto
import Pluto: Cell, Notebook, NotebookTopology, ExpressionExplorer

"Find all subexpressions of the form `@bind symbol something`, and extract the `symbol`s."
function find_bound_variables(expr)
found = Set{Symbol}()
find_bound_variables!(
found,
ExpressionExplorer.maybe_macroexpand(expr; recursive=true, expand_bind=false),
)
found
end

function find_bound_variables!(found::Set{Symbol}, expr::Expr)
if expr.head === :macrocall &&
expr.args[1] === Symbol("@bind") &&
length(expr.args) == 4 &&
expr.args[3] isa Symbol
push!(found, expr.args[3])
find_bound_variables!(found, expr.args[4])
elseif expr.args === :quote
found
else
for a in expr.args
find_bound_variables!(found, a)
end
end
end

function find_bound_variables!(found::Set{Symbol}, expr::Any) end


import Pluto: Cell, Notebook, NotebookTopology, ExpressionExplorer, ServerSession


"Return the given cells, and all cells that depend on them (recursively)."
Expand Down Expand Up @@ -105,11 +75,9 @@ end
"Return a `Dict{Symbol,Vector{Symbol}}` where the _keys_ are the bound variables of the notebook.
For each key (a bound symbol), the value is the list of (other) bound variables whose values need to be known to compute the result of setting the bond."
function bound_variable_connections_graph(notebook::Notebook)::Dict{Symbol,Vector{Symbol}}
function bound_variable_connections_graph(session::ServerSession, notebook::Notebook)::Dict{Symbol,Vector{Symbol}}
topology = notebook.topology
bound_variables = union(map(notebook.cells) do cell
find_bound_variables(topology.codes[cell].parsedcode)
end...)
bound_variables = Pluto.get_bond_names(session, notebook)
Dict{Symbol,Vector{Symbol}}(
var => let
cells = codependents(notebook, topology, var)
Expand Down
40 changes: 21 additions & 19 deletions test/Bond connections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,41 @@ using Test
Pluto.readwrite(file, newpath)

notebook = Pluto.load_notebook(newpath)
# Run pluto's analysis. This is like opening the notebook, without actually running it
# s = Pluto.ServerSession()
# Pluto.update_save_run!(s, notebook, notebook.cells; run)
notebook.topology = Pluto.updated_topology(notebook.topology, notebook, notebook.cells)

s = Pluto.ServerSession()
s.options.evaluation.workspace_use_distributed = false
Pluto.update_run!(s, notebook, notebook.cells)
# notebook.topology = Pluto.updated_topology(notebook.topology, notebook, notebook.cells)

# bound_variables = (map(notebook.cells) do cell
# MoreAnalysis.find_bound_variables(cell.parsedcode)
# end)

# @show bound_variables

connections = bound_variable_connections_graph(notebook)
connections = bound_variable_connections_graph(s, notebook)
# @show connections

@test !isempty(connections)
wanted_connections = Dict(
:x => [:y, :x],
:y => [:y, :x],
:x => [:y, :x],
:y => [:y, :x],
:show_dogs => [:show_dogs],
:b => [:b],
:c => [:c],
:five1 => [:five1],
:five2 => [:five2],
:six1 => [:six2, :six1],
:six2 => [:six3, :six2, :six1],
:six3 => [:six3, :six2],
:cool1 => [:cool1, :cool2],
:cool2 => [:cool1, :cool2],
:world => [:world],
:boring => [:boring],
:b => [:b],
:c => [:c],
:five1 => [:five1],
:five2 => [:five2],
:six1 => [:six2, :six1],
:six2 => [:six3, :six2, :six1],
:six3 => [:six3, :six2],
:cool1 => [:cool1, :cool2],
:cool2 => [:cool1, :cool2],
:world => [:world],
:boring => [:boring],
:custom_macro => [:custom_macro],
)

transform(d) = Dict(k => sort(v) for (k, v) in d)

@test transform(connections) == transform(wanted_connections)
end
end
1 change: 1 addition & 0 deletions test/Folder watching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ end
"cool2",
"world",
"boring",
"custom_macro",
])
end

Expand Down
24 changes: 22 additions & 2 deletions test/parallelpaths4.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
### A Pluto.jl notebook ###
# v0.12.20
# v0.19.15

using Markdown
using InteractiveUtils

# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
el
end
end
Expand Down Expand Up @@ -186,6 +187,22 @@ md"""
# ╔═╡ 22659c85-700f-4dad-a22a-7aafa71225c0
# boring is never referenced

# ╔═╡ 5bea1d75-a8d5-4285-b0cf-234fcfe8122f
md"""
## Path 12
"""

# ╔═╡ 3d96cc48-9c73-46e9-869b-eb72231f283e
macro bindname(name::Symbol, ex::Expr)
name_str = "$name: "
quote
Markdown.MD([Markdown.Paragraph([Markdown.Bold($name_str), (@bind $name html"<input>")])])
end
end

# ╔═╡ dc5d8314-d2d1-477c-9ccd-882069ee4210
@bindname custom_macro html"<input>"

# ╔═╡ Cell order:
# ╟─8aac8df3-1551-4c9f-a8bd-a62751a29b2a
# ╠═03307e43-cb61-4321-95ac-7bbb16e0cfc6
Expand Down Expand Up @@ -226,3 +243,6 @@ md"""
# ╟─c143b2de-78c5-46ad-852f-3c2a9115cb72
# ╠═cf628a57-933b-4984-a317-63360c345534
# ╠═22659c85-700f-4dad-a22a-7aafa71225c0
# ╟─5bea1d75-a8d5-4285-b0cf-234fcfe8122f
# ╠═3d96cc48-9c73-46e9-869b-eb72231f283e
# ╠═dc5d8314-d2d1-477c-9ccd-882069ee4210

0 comments on commit ddd9e72

Please sign in to comment.