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

🧸 Open your own files as samples #828

Merged
merged 2 commits into from
Jan 8, 2021
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
13 changes: 13 additions & 0 deletions src/notebook/PathHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ function new_notebooks_directory()
end
end

function without_dotjl(path)
extension = let
parts = split(basename(path), '.')
length(parts) == 1 ? "" : last(parts)
end

if startswith(extension, "jl")
path[1:end-length(extension)-1]
else
path
end
end

"""
Return `base` * `suffix` if the file does not exist yet.

Expand Down
12 changes: 10 additions & 2 deletions src/webserver/SessionActions.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SessionActions

import ..Pluto: ServerSession, Notebook, emptynotebook, tamepath, move_notebook!, update_save_run!, putnotebookupdates!, putplutoupdates!, load_notebook, clientupdate_notebook_list, WorkspaceManager, @asynclog
import ..Pluto: ServerSession, Notebook, emptynotebook, tamepath, new_notebooks_directory, without_dotjl, numbered_until_new, readwrite, move_notebook!, update_save_run!, putnotebookupdates!, putplutoupdates!, load_notebook, clientupdate_notebook_list, WorkspaceManager, @asynclog

struct NotebookIsRunningException <: Exception
notebook::Notebook
Expand All @@ -19,7 +19,15 @@ function open_url(session::ServerSession, url::AbstractString; kwargs...)
open(session, path; kwargs...)
end

function open(session::ServerSession, path::AbstractString; run_async=true, compiler_options=nothing)
function open(session::ServerSession, path::AbstractString; run_async=true, compiler_options=nothing, as_sample=false)
if as_sample
new_filename = "sample " * without_dotjl(basename(path))
new_path = numbered_until_new(joinpath(new_notebooks_directory(), new_filename); suffix=".jl")

readwrite(path, new_path)
path = new_path
end

for nb in values(session.notebooks)
if realpath(nb.path) == realpath(tamepath(path))
throw(NotebookIsRunningException(nb))
Expand Down
18 changes: 8 additions & 10 deletions src/webserver/Static.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ function http_router_for(session::ServerSession)
HTTP.@register(router, "GET", "/ping", r -> HTTP.Response(200, "OK!"))
HTTP.@register(router, "GET", "/possible_binder_token_please", r -> session.binder_token === nothing ? HTTP.Response(404,"") : HTTP.Response(200, session.binder_token))

function try_launch_notebook_response(action::Function, path_or_url::AbstractString; title="", advice="", home_url="./")
function try_launch_notebook_response(action::Function, path_or_url::AbstractString; title="", advice="", home_url="./", action_kwargs...)
try
nb = action(session, path_or_url)
nb = action(session, path_or_url; action_kwargs...)
notebook_redirect_response(nb; home_url=home_url)
catch e
if e isa SessionActions.NotebookIsRunningException
Expand All @@ -155,16 +155,17 @@ function http_router_for(session::ServerSession)
try
uri = HTTP.URI(request.target)
query = HTTP.queryparams(uri)
as_sample = haskey(query, "as_sample")
if haskey(query, "path")
path = tamepath(query["path"])
if isfile(path)
return try_launch_notebook_response(SessionActions.open, path, title="Failed to load notebook", advice="The file <code>$(htmlesc(path))</code> could not be loaded. Please <a href='https://github.com/fonsp/Pluto.jl/issues'>report this error</a>!")
return try_launch_notebook_response(SessionActions.open, path; as_sample=as_sample, title="Failed to load notebook", advice="The file <code>$(htmlesc(path))</code> could not be loaded. Please <a href='https://github.com/fonsp/Pluto.jl/issues'>report this error</a>!")
else
return error_response(404, "Can't find a file here", "Please check whether <code>$(htmlesc(path))</code> exists.")
end
elseif haskey(query, "url")
url = query["url"]
return try_launch_notebook_response(SessionActions.open_url, url, title="Failed to load notebook", advice="The notebook from <code>$(htmlesc(url))</code> could not be loaded. Please <a href='https://github.com/fonsp/Pluto.jl/issues'>report this error</a>!")
return try_launch_notebook_response(SessionActions.open_url, url; as_sample=as_sample, title="Failed to load notebook", advice="The notebook from <code>$(htmlesc(url))</code> could not be loaded. Please <a href='https://github.com/fonsp/Pluto.jl/issues'>report this error</a>!")
else
error("Empty request")
end
Expand All @@ -180,13 +181,10 @@ function http_router_for(session::ServerSession)
security.require_secret_for_open_links
) do request::HTTP.Request
uri = HTTP.URI(request.target)
sample_path = split(HTTP.unescapeuri(uri.path), "sample/")[2]
sample_path_without_dotjl = "sample " * sample_path[1:end - 3]
sample_filename = split(HTTP.unescapeuri(uri.path), "sample/")[2]
sample_path = project_relative_path("sample", sample_filename)

path = numbered_until_new(joinpath(new_notebooks_directory(), sample_path_without_dotjl))
readwrite(project_relative_path("sample", sample_path), path)

try_launch_notebook_response(SessionActions.open, path, home_url="../", title="Failed to load sample", advice="Please <a href='https://github.com/fonsp/Pluto.jl/issues'>report this error</a>!")
try_launch_notebook_response(SessionActions.open, sample_path, home_url="../", title="Failed to load sample", advice="Please <a href='https://github.com/fonsp/Pluto.jl/issues'>report this error</a>!", as_sample=true)
end
HTTP.@register(router, "GET", "/sample/*", serve_sample)

Expand Down