Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup & run all #46

Merged
merged 6 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Pluto"
uuid = "c3e4b0f8-55cb-11ea-2926-15256bba5781"
license = "MIT"
authors = ["Fons van der Plas <[email protected]>", "Mikołaj Bochenski <[email protected]>"]
version = "0.5.1"
version = "0.5.2"

[deps]
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Expand Down
2 changes: 1 addition & 1 deletion assets/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
/* MAIN */

preamble {
display: none;
display: block;
position: relative;
}

Expand Down
10 changes: 7 additions & 3 deletions src/Pluto.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ export Notebook, Cell, run
import Pkg

const PKG_ROOT_DIR = normpath(joinpath(@__DIR__, ".."))
const VERSION_STR = 'v' * Pkg.TOML.parsefile(joinpath(PKG_ROOT_DIR, "Project.toml"))["version"]
const PLUTO_VERSION = VersionNumber(Pkg.TOML.parsefile(joinpath(PKG_ROOT_DIR, "Project.toml"))["version"])
const PLUTO_VERSION_STR = 'v' * string(PLUTO_VERSION)

@info """\n
Welcome to Pluto $(VERSION_STR)! ⚡
Welcome to Pluto $(PLUTO_VERSION_STR)! ⚡

Let us know what you think:
https://github.com/fonsp/Pluto.jl
\n"""

include("./react/ExploreExpression.jl")
include("./webserver/FormatOutput.jl")
using .ExploreExpression
include("./webserver/FormatOutput.jl")

include("./react/Cell.jl")
include("./react/Notebook.jl")
include("./react/WorkspaceManager.jl")
include("./react/Errors.jl")
include("./react/React.jl")
include("./react/Run.jl")

include("./webserver/NotebookServer.jl")
include("./webserver/Static.jl")
Expand Down
18 changes: 6 additions & 12 deletions src/react/Cell.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using UUIDs
import UUIDs: UUID
import .ExploreExpression: SymbolsState
import JSON: lower

"The building block of `Notebook`s. Contains both code and output."
"The building block of `Notebook`s. Contains code, output and reactivity data."
mutable struct Cell
"because Cells can be reordered, they get a UUID. The JavaScript frontend indexes cells using the UUID."
uuid::UUID
Expand All @@ -11,16 +13,8 @@ mutable struct Cell
repr_mime::MIME
runtime::Union{Missing,UInt64}
symstate::SymbolsState
resolved_funccalls::Set{Symbol}
resolved_symstate::SymbolsState
module_usings::Set{Expr}
end

Cell(uuid, code) = Cell(uuid, code, nothing, nothing, nothing, MIME("text/plain"), missing, SymbolsState(), Set{Symbol}(), SymbolsState(), Set{Expr}())

"Turn a `Cell` into an object that can be serialized using `JSON.json`, to be sent to the client."
function serialize(cell::Cell)
Dict(:uuid => string(cell.uuid), :code => cell.code)
end

createcell_fromcode(code::String) = Cell(uuid1(), code)
Cell(uuid, code) = Cell(uuid, code, nothing, nothing, nothing, MIME("text/plain"), missing, SymbolsState(), Set{Expr}())
Cell(code) = Cell(uuid1(), code)
45 changes: 45 additions & 0 deletions src/react/Errors.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Base: showerror


abstract type ReactivityError <: Exception end


struct CyclicReferenceError <: ReactivityError
syms::Set{Symbol}
end

function CyclicReferenceError(cycle::Array{Cell, 1})
referenced_during_cycle = union((c.symstate.references for c in cycle)...)
assigned_during_cycle = union((c.symstate.assignments for c in cycle)...)

CyclicReferenceError(referenced_during_cycle ∩ assigned_during_cycle)
end

CyclicReferenceError(cycle::Set{Cell}) = collect(cycle) |> CyclicReferenceError


struct MultipleDefinitionsError <: ReactivityError
syms::Set{Symbol}
end

function MultipleDefinitionsError(cell::Cell, all_definers)
competitors = setdiff(all_definers, [cell])
union((cell.symstate.assignments ∩ c.symstate.assignments for c in competitors)...) |>
MultipleDefinitionsError
end


# TODO: handle case when cells are in cycle, but variables aren't
function showerror(io::IO, cre::CyclicReferenceError)
print(io, "Cyclic references among $(join(cre.syms, ", ", " and ")).")
end

function showerror(io::IO, mde::MultipleDefinitionsError)
print(io, "Multiple definitions for $(join(mde.syms, ", ", " and ")).\nCombine all definitions into a single reactive cell using a `begin` ... `end` block.") # TODO: hint about mutable globals
end

"Send `error` to the frontend without backtrace. Runtime errors are handled by `WorkspaceManager.eval_fetch_in_workspace` - this function is for Reactivity errors."
function relay_reactivity_error!(cell::Cell, error::Exception)
cell.output_repr = nothing
cell.error_repr, cell.repr_mime = format_output(error)
end
4 changes: 2 additions & 2 deletions src/react/ExploreExpression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import Base: union, ==
const modifiers = [:(+=), :(-=), :(*=), :(/=), :(//=), :(^=), :(÷=), :(%=), :(<<=), :(>>=), :(>>>=), :(&=), :(⊻=), :(≔), :(⩴), :(≕)]


"SymbolsState trickels _down_ the ASTree: it carries referenced and defined variables from endpoints down to the root"
"SymbolsState trickles _down_ the ASTree: it carries referenced and defined variables from endpoints down to the root"
mutable struct SymbolsState
references::Set{Symbol}
assignments::Set{Symbol}
funccalls::Set{Symbol}
funcdefs::Dict{Symbol,SymbolsState}
funcdefs::Dict{Symbol, SymbolsState}
end

SymbolsState(references, assignments, funccalls) = SymbolsState(references, assignments, funccalls, Dict{Symbol,SymbolsState}())
Expand Down
22 changes: 11 additions & 11 deletions src/react/Notebook.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mutable struct Notebook
cells::Array{Cell,1}

uuid::UUID
combined_funcdefs::Union{Nothing,Dict{Symbol, SymbolsState}}
combined_funcdefs::Dict{Symbol, SymbolsState}

# buffer must contain all undisplayed outputs
pendingupdates::Channel
Expand All @@ -19,7 +19,7 @@ end
Notebook(path::String, cells::Array{Cell,1}, uuid) = let
et = Channel{Nothing}(1)
put!(et, nothing)
Notebook(path, cells, uuid, nothing, Channel(128), et)
Notebook(path, cells, uuid, Dict{Symbol, SymbolsState}(), Channel(128), et)
end
Notebook(path::String, cells::Array{Cell,1}) = Notebook(path, cells, uuid1())

Expand All @@ -37,17 +37,17 @@ _uuid_delimiter = "# ⋐⋑ "
_order_delimited = "# ○ "
_cell_appendix = "\n\n"

emptynotebook(path) = Notebook(path, [createcell_fromcode("")])
emptynotebook(path) = Notebook(path, [Cell("")])
emptynotebook() = emptynotebook(tempname() * ".jl")

function samplenotebook()
cells = Cell[]

push!(cells, createcell_fromcode("100*a + b"))
push!(cells, createcell_fromcode("a = 1"))
push!(cells, createcell_fromcode("b = let\n\tx = a + a\n\tx*x\nend"))
push!(cells, createcell_fromcode("html\"<h1>Hoi!</h1>\n<p>My name is <em>kiki</em></p>\""))
push!(cells, createcell_fromcode("""md"# Cześć!
push!(cells, Cell("100*a + b"))
push!(cells, Cell("a = 1"))
push!(cells, Cell("b = let\n\tx = a + a\n\tx*x\nend"))
push!(cells, Cell("html\"<h1>Hoi!</h1>\n<p>My name is <em>kiki</em></p>\""))
push!(cells, Cell("""md"# Cześć!
My name is **baba** and I like \$\\LaTeX\$ _support!_

\$\$\\begin{align}
Expand All @@ -64,7 +64,7 @@ end

function save_notebook(io, notebook)
write(io, "### A Pluto.jl notebook ###\n")
write(io, "# " * VERSION_STR * "\n")
write(io, "# " * PLUTO_VERSION_STR * "\n")

# TODO: order cells
cells_ordered = notebook.cells
Expand Down Expand Up @@ -97,8 +97,8 @@ function load_notebook(io, path)
end

file_VERSION_STR = readline(io)[3:end]
if file_VERSION_STR != VERSION_STR
@warn "Loading a notebook saved with Pluto $(file_VERSION_STR). This is Pluto $(VERSION_STR)."
if file_VERSION_STR != PLUTO_VERSION_STR
@warn "Loading a notebook saved with Pluto $(file_VERSION_STR). This is Pluto $(PLUTO_VERSION_STR)."
end


Expand Down
Loading