From d4909000423f89f8e0d8a2a09cb66fa8be0d2a3c Mon Sep 17 00:00:00 2001 From: Colin Caine Date: Tue, 8 Sep 2020 00:23:08 +0100 Subject: [PATCH] WIP reformat cells #326 --- src/evaluation/Run.jl | 16 ++++++++++++++++ src/evaluation/WorkspaceManager.jl | 13 +++++++++++++ test/React.jl | 20 ++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/src/evaluation/Run.jl b/src/evaluation/Run.jl index ed942a85cb..2b8d1eba2d 100644 --- a/src/evaluation/Run.jl +++ b/src/evaluation/Run.jl @@ -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 @@ -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 ### diff --git a/src/evaluation/WorkspaceManager.jl b/src/evaluation/WorkspaceManager.jl index c79937e52e..2f7de9b12e 100644 --- a/src/evaluation/WorkspaceManager.jl +++ b/src/evaluation/WorkspaceManager.jl @@ -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) diff --git a/test/React.jl b/test/React.jl index b75f7f9721..86fd264012 100644 --- a/test/React.jl +++ b/test/React.jl @@ -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 \ No newline at end of file