Skip to content

Commit

Permalink
WIP reformat cells fonsp#326
Browse files Browse the repository at this point in the history
  • Loading branch information
cmcaine committed Sep 7, 2020
1 parent cc7248d commit d490900
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/evaluation/Run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ function run_reactive!(session::ServerSession, notebook::Notebook, old_topology:
putnotebookupdates!(session, notebook, clientupdate_cell_output(notebook, cell))
end

# reformat outputs if a show method is (re)defined
# WIP: just reformat every time.
for cell in new_order.runnable
format_single!(notebook, cell)
putnotebookupdates!(session, notebook, clientupdate_cell_output(notebook, cell))
end

# allow other `run_reactive!` calls to be executed
put!(notebook.executetoken)
return new_order
Expand All @@ -97,6 +104,15 @@ function run_single!(notebook::Union{Notebook,WorkspaceManager.Workspace}, cell:
return run
end

function format_single!(notebook::Union{Notebook,WorkspaceManager.Workspace}, cell::Cell)
run = WorkspaceManager.format_fetch_in_workspace(notebook, cell.cell_id, ends_with_semicolon(cell.code))

cell.output_repr = run.output_formatted[1]
cell.repr_mime = run.output_formatted[2]

return run
end

###
# CONVENIENCE FUNCTIONS
###
Expand Down
13 changes: 13 additions & 0 deletions src/evaluation/WorkspaceManager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ function eval_format_fetch_in_workspace(notebook::Union{Notebook,Workspace}, exp
end
end

function format_fetch_in_workspace(notebook::Union{Notebook,Workspace}, cell_id::UUID, ends_with_semicolon::Bool)::NamedTuple{(:output_formatted, :errored, :interrupted, :runtime),Tuple{PlutoRunner.MimedOutput,Bool,Bool,Union{UInt64,Missing}}}
workspace = get_workspace(notebook)

# if multiple notebooks run on the same process, then we need to `cd` between the different notebook paths
if workspace.pid == Distributed.myid() && notebook isa Notebook
cd_workspace(workspace, notebook.path)
end

withtoken(workspace.dowork_token) do
Distributed.remotecall_eval(Main, workspace.pid, :(PlutoRunner.formatted_result_of($cell_id, $ends_with_semicolon)))
end
end

"Evaluate expression inside the workspace - output is not fetched, errors are rethrown. For internal use."
function eval_in_workspace(notebook::Union{Notebook,Workspace}, expr)
workspace = get_workspace(notebook)
Expand Down
20 changes: 20 additions & 0 deletions test/React.jl
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,26 @@ withenv("PLUTO_WORKSPACE_USE_DISTRIBUTED" => "false") do

WorkspaceManager.unmake_workspace(notebook)
end

@testset "#326 update display when `show()` methods defined" begin
notebook = Notebook([
Cell("1"),
Cell("""Base.show(io::IO, x::Int) = print(io, "Wat")"""),
])
fakeclient.connected_notebook = notebook

update_run!(🍭, notebook, notebook.cells[1])
@test notebook.cells[1].output_repr == "1"

update_run!(🍭, notebook, notebook.cells[2])
@test notebook.cells[1].output_repr == "Wat"

setcode(notebook.cells[2], "")
update_run!(🍭, notebook, notebook.cells[2])
@test notebook.cells[1].output_repr == "1"

WorkspaceManager.unmake_workspace(notebook)
end
end

end

0 comments on commit d490900

Please sign in to comment.