Skip to content

Commit

Permalink
πŸƒβ€β™€οΈ List running sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Mar 22, 2020
1 parent ef8f96b commit 0ee82d7
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 30 deletions.
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.3.3"
version = "0.3.4"

[deps]
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
Expand Down
2 changes: 1 addition & 1 deletion assets/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PlutoConnection {
clientID: this.clientID,
body: body,
}
if (notebookID) {
if (this.notebookID) {
toSend.notebookID = this.notebookID
}
if (cellUUID) {
Expand Down
4 changes: 2 additions & 2 deletions assets/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@
<body class="loading no-MΞ±ΞΈJax">
<header>
<div id="logocontainer">
<h1>
<img src="/assets/logo.svg" alt="Pluto.jl" /></h1>
<a href="/"><h1>
<img src="/assets/logo.svg" alt="Pluto.jl" /></h1></a>
<h2>
<span id="printernametitle">Loading...</span></h2>
</div>
Expand Down
2 changes: 1 addition & 1 deletion assets/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ document.addEventListener("DOMContentLoaded", () => {
document.title = cuteName
}

remoteNotebookList = null
window.remoteNotebookList = null

function updateRemoteNotebooks(list) {
remoteNotebookList = list
Expand Down
50 changes: 36 additions & 14 deletions assets/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,36 @@

body {
margin: 0px;
}

main {
position: absolute;
width: 100%;
height: 100%;
background: white;
}

ul {
padding-left: 0px;
main {

width: 15em;
margin: 0 auto;
margin-top: 25vh;
margin-top: 20vh;

text-align: left;
list-style: none;
font-family: 'Roboto Mono', monospace;
color: hsl(0, 0%, 60%);
}

p {
color: hsl(0, 0%, 30%);
}

ul {
padding-left: .5em;
list-style: none;
}

ul.running {
margin-bottom: 10em;
}

li {
margin-bottom: .9em;
}
Expand All @@ -97,14 +107,20 @@
width: 20%;
font-family: inherit;
}

body.loading .running,
body.nosessions .running{
display: none;
}
</style>
</head>

<body>
<main>
<div id="title">
<h1>welcome to <img src="/assets/logo.svg"><span class="phantom"> ot emoclew</span></h1>
<ul>
<body class="loading nosessions">
<div id="title">
<h1>welcome to <img src="/assets/logo.svg"><span class="phantom"> ot emoclew</span></h1>
<main>
<p class="new">New session:</p>
<ul class="new">
<li>Open a <a href="/sample">sample notebook</a></li>
<li>Create a <a href="/new">new notebook</a></li>
<li>Open from file:
Expand All @@ -114,8 +130,14 @@ <h1>welcome to <img src="/assets/logo.svg"><span class="phantom"> ot emoclew</sp
</form>
</li>
</ul>
</div>
</main>
<br class="running" />
<p class="running">Running sessions:</p>
<ul class="running"></ul>
</main>
</div>

<script src="/assets/client.js"></script>
<script src="/assets/welcome.js"></script>
</body>

</html>
60 changes: 60 additions & 0 deletions assets/welcome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
document.addEventListener("DOMContentLoaded", () => {

/* REMOTE NOTEBOOK LIST */

window.remoteNotebookList = null

function updateRemoteNotebooks(list) {
remoteNotebookList = list

var listEl = document.querySelector("ul.running")
listEl.innerHTML = ""
document.body.classList.add("nosessions")
list.forEach(nb => {
document.body.classList.remove("nosessions")
var a = document.createElement("a")
a.href = "/edit?uuid=" + nb.uuid
a.innerText = nb.shortpath

var li = document.createElement("li")
li.appendChild(a)

listEl.appendChild(li)
})
console.log(list)
}

/* SERVER CONNECTION */

function onUpdate(update, byMe) {
var message = update.message

switch (update.type) {
case "notebook_list":
// TODO: catch exception
updateRemoteNotebooks(message.notebooks)
break
default:
console.error("Received unknown update type!")
break
}
}

function onEstablishConnection(){
// on socket success
// TODO: we should when exactly this happens
document.body.classList.remove("loading")
}

function onConnect() {
console.info("connected")
}

function onDisconnect() {
console.info("disconnected")
}

window.client = new PlutoConnection(onUpdate, onEstablishConnection, onConnect, onDisconnect)
client.initialize()
});

85 changes: 80 additions & 5 deletions src/webserver/Dynamic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,84 @@ function clientupdate_cell_running(initiator::Client, notebook::Notebook, cell::
Dict(), notebook, cell, initiator)
end

"To be used in `make_distinct!`"
mutable struct NotebookPath
uuid
path_split
current_path
current_depth
end

function count_occurances(vals)
counts = Dict()
for v in vals
old = get(counts, v, 0)
counts[v] = old + 1
end
counts
end

"""For internal use. Takes a Set of `NotebookPath`s and gives each a short path (e.g. `to/file.jl` from `/path/to/file.jl`), with the guarantee that all final short paths will be distinct.
For example, the set
`/a/b/c.jl`, `/a/P/c.jl`, `/Q/b/c.jl`, '/a/b/R.jl'
will become
`/a/b/c.jl`, `P/c.jl`, `/Q/b/c.jl`, 'R.jl'"""
function make_distinct!(notebookpaths::Set{NotebookPath})
counts = count_occurances(np.current_path for np in notebookpaths)
for (current_path, count) in counts
if count == 1 && !isempty(current_path)
# done!
else
# these need to be made distinct by extending their paths

not_yet_distinct = filter(notebookpaths) do np
np.current_path == current_path
end

for np in not_yet_distinct
np.current_depth += 1
np.current_path = joinpath(np.path_split[end-np.current_depth : end]...)
if np.current_depth == length(np.path_split) - 1
delete!(not_yet_distinct, np)
if !Sys.iswindows()
np.current_path = '/' * np.current_path
end
end
end

make_distinct!(not_yet_distinct)
end
end
end

function clientupdate_notebook_list(initiator::Client, notebook_list)
short_paths = Dict()

notebookpaths = map(notebook_list) do notebook
pathsep = Sys.iswindows() ? '\\' : '/'
path_split = split(notebook.path, pathsep)
if path_split[1] == ""
path_split = path_split[2:end]
end
NotebookPath(notebook.uuid, path_split, "", -1)
end

make_distinct!(Set(notebookpaths))

short_paths = Dict(map(notebookpaths) do np
np.uuid => np.current_path
end...)

return UpdateMessage(:notebook_list,
Dict(:notebooks => [Dict(:uuid => string(notebook.uuid),
:path => notebook.path,
) for notebook in notebook_list]), nothing, nothing, initiator)
Dict(:notebooks => [Dict(
:uuid => string(notebook.uuid),
:path => notebook.path,
:shortpath => short_paths[notebook.uuid]
) for notebook in notebook_list]), nothing, nothing, initiator)
end


Expand Down Expand Up @@ -173,9 +246,11 @@ addresponse(:getallcells) do (initiator, body, notebook)
end
# [clientupdate_cell_added(notebook, c, i) for (i, c) in enumerate(notebook.cells)]

updates
putnotebookupdates!(notebook, updates...)
nothing
end

addresponse(:getallnotebooks) do (initiator, body)
[clientupdate_notebook_list(initiator, values(notebooks))]
putplutoupdates!(clientupdate_notebook_list(initiator, values(notebooks)))
nothing
end
16 changes: 10 additions & 6 deletions src/webserver/NotebookServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ notebooks = Dict{UUID,Notebook}()


function putnotebookupdates!(notebook, messages...)
listeners = filter(c->c.connected_notebook.uuid == notebook.uuid, collect(values(connectedclients)))
listeners = filter(collect(values(connectedclients))) do c
c.connected_notebook !== nothing &&
c.connected_notebook.uuid == notebook.uuid
end
if isempty(listeners)
@info "no clients connected to this notebook!"
else
Expand All @@ -30,7 +33,7 @@ function putnotebookupdates!(notebook, messages...)
end


function putplutoupdates!(notebook, messages...)
function putplutoupdates!(messages...)
listeners = collect(values(connectedclients))
if isempty(listeners)
@info "no clients connected to pluto!"
Expand Down Expand Up @@ -159,9 +162,9 @@ function run(port = 1234, launchbrowser = false)
if haskey(responses, messagetype)
responsefunc = responses[messagetype]
response = responsefunc((client, body, args...))
if response !== nothing
putplutoupdates!(notebook, response...)
end
# if response !== nothing
# putplutoupdates!(response...)
# end
else
@warn "Message of type $(messagetype) not recognised"
end
Expand All @@ -175,7 +178,8 @@ function run(port = 1234, launchbrowser = false)
# that's fine! this is a (fixed) HTTP.jl bug: https://github.com/JuliaWeb/HTTP.jl/issues/471
# TODO: remove this switch
else
@warn "Reading WebSocket client stream failed for unknown reason:" e
bt = stacktrace(catch_backtrace())
@warn "Reading WebSocket client stream failed for unknown reason:" e bt
end
end
end
Expand Down

3 comments on commit 0ee82d7

@fonsp
Copy link
Owner Author

@fonsp fonsp commented on 0ee82d7 Mar 22, 2020

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/11371

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.4 -m "<description of version>" 0ee82d7bc18f10d52f21ce774f27b66625c36927
git push origin v0.3.4

@fonsp
Copy link
Owner Author

@fonsp fonsp commented on 0ee82d7 Mar 23, 2020

Choose a reason for hiding this comment

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

#30

Please sign in to comment.