Skip to content

Commit

Permalink
🔍 InteractiveUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Jul 8, 2020
1 parent b71513a commit fa08dbf
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
FileWatching = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand Down
3 changes: 3 additions & 0 deletions src/notebookserver/Notebook.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ function save_notebook(io, notebook::Notebook)
# Anything between the version string and the first UUID delimiter will be ignored by the notebook loader.
println(io, "")
println(io, "using Markdown")
println(io, "using InteractiveUtils")
# Super Advanced Code Analysis™ to add the @bind macro to the saved file if it's used somewhere.
if any(occursin("@bind", c.code) for c in notebook.cells)
println(io, "")
println(io, "# 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).")
println(io, PlutoRunner.fake_bind)
end
println(io)
Expand Down
8 changes: 4 additions & 4 deletions src/react/WorkspaceManager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ end

"These expressions get evaluated inside every newly create module inside a `Workspace`."
const workspace_preamble = [
:(using Markdown, Main.PlutoRunner),
:(using Markdown, InteractiveUtils, Main.PlutoRunner),
:(ENV["GKSwstype"] = "nul"),
:(show, showable, showerror, repr, string, print, println), # https://github.com/JuliaLang/julia/issues/18181
]
Expand All @@ -37,7 +37,7 @@ workspaces = Dict{UUID,Workspace}()
"""Create a workspace for the notebook, optionally in a separate process.
`new_process`: Should future workspaces be created on a separate process (`true`) or on the same one (`false`)? Only workspaces on a separate process can be stopped during execution. Windows currently supports `true` only partially: you can't stop cells on Windows. _Defaults to `ENV["PLUTO_WORKSPACE_USE_DISTRIBUTED"]`_"""
function make_workspace(notebook::Notebook, new_process = (ENV["PLUTO_WORKSPACE_USE_DISTRIBUTED"] == "true"))::Workspace
function make_workspace(notebook::Notebook, new_process=(ENV["PLUTO_WORKSPACE_USE_DISTRIBUTED"] == "true"))::Workspace
pid = if new_process
create_workspaceprocess()
else
Expand Down Expand Up @@ -115,11 +115,11 @@ end
"Evaluate expression inside the workspace - output is fetched and formatted, errors are caught and formatted. Returns formatted output and error flags.
`expr` has to satisfy `ExploreExpression.is_toplevel_expr`."
function eval_fetch_in_workspace(notebook::Notebook, expr::Expr, cell_id::UUID, ends_with_semicolon::Bool=false)::NamedTuple{(:output_formatted, :errored, :interrupted, :runtime),Tuple{Tuple{String,MIME},Bool,Bool,Union{UInt64, Missing}}}
function eval_fetch_in_workspace(notebook::Notebook, expr::Expr, cell_id::UUID, ends_with_semicolon::Bool=false)::NamedTuple{(:output_formatted, :errored, :interrupted, :runtime),Tuple{Tuple{String,MIME},Bool,Bool,Union{UInt64,Missing}}}
eval_fetch_in_workspace(get_workspace(notebook), expr, cell_id, ends_with_semicolon)
end

function eval_fetch_in_workspace(workspace::Workspace, expr::Expr, cell_id::UUID, ends_with_semicolon::Bool=false)::NamedTuple{(:output_formatted, :errored, :interrupted, :runtime),Tuple{Tuple{String,MIME},Bool,Bool,Union{UInt64, Missing}}}
function eval_fetch_in_workspace(workspace::Workspace, expr::Expr, cell_id::UUID, ends_with_semicolon::Bool=false)::NamedTuple{(:output_formatted, :errored, :interrupted, :runtime),Tuple{Tuple{String,MIME},Bool,Bool,Union{UInt64,Missing}}}
# We wrap the expression in a try-catch block, because we want to capture and format the exception on the worker itself.
wrapped = trycatch_expr(expr, workspace.module_name, cell_id)

Expand Down
3 changes: 3 additions & 0 deletions test/Notebook.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function basic_notebook()
Cell("💩 = :💩"), # ends with 4-byte character
Cell("b = let\n\tx = a + a\n\tx*x\nend"),
Cell("html\"<h1>Hoi!</h1>\n<p>My name is <em>kiki</em></p>\""),
# test included Markdown import
Cell("""md"# Cześć!
My name is **baba** and I like \$\\LaTeX\$ _support!_
Expand All @@ -24,6 +25,8 @@ function basic_notebook()
### The spectacle before us was indeed sublime.
Apparently we had reached a great height in the atmosphere, for the sky was a dead black, and the stars had ceased to twinkle. By the same illusion which lifts the horizon of the sea to the level of the spectator on a hillside, the sable cloud beneath was dished out, and the car seemed to float in the middle of an immense dark sphere, whose upper half was strewn with silver. Looking down into the dark gulf below, I could see a ruddy light streaming through a rift in the clouds."
"""),
# test included InteractiveUtils import
Cell("subtypes(Number)"),
])
end

Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
include("./helpers.jl")

include("./ExploreExpression.jl")
include("./React.jl")
include("./Notebook.jl")
include("./React.jl")
include("./WorkspaceManager.jl")

# TODO: test PlutoRunner functions like:
Expand Down

1 comment on commit fa08dbf

@fonsp
Copy link
Owner Author

@fonsp fonsp commented on fa08dbf Jul 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.