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

Add CLI API #3

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 24 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
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM julia:1.6.0

ENV USER pluto
ENV USER_HOME_DIR /home/${USER}
ENV JULIA_DEPOT_PATH ${USER_HOME_DIR}/.julia
ENV NOTEBOOK_DIR ${USER_HOME_DIR}/notebooks
ENV JULIA_NUM_THREADS 100

RUN useradd -m -d ${USER_HOME_DIR} ${USER} \
&& mkdir -p ${NOTEBOOK_DIR}

COPY . ${USER_HOME_DIR}/
WORKDIR ${USER_HOME_DIR}

RUN mkdir -p /home/pluto/.julia/environments/v1.6/ &&\
cp ./PlutoSliderServer/*.toml /home/pluto/.julia/environments/v1.6/ &&\
julia -e "import Pkg; Pkg.activate(); Pkg.instantiate(); Pkg.precompile();" &&\
chown -R ${USER} ${USER_HOME_DIR}

USER ${USER}

EXPOSE 80
VOLUME ${NOTEBOOK_DIR}
WORKDIR ${NOTEBOOK_DIR}

CMD [ "julia", "-e", "using PlutoSliderServer; cli()", "--", "--port", "80", "--host", "0.0.0.0", "--run-test-server-shortcut"]
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ authors = ["Fons van der Plas <[email protected]>"]
version = "0.2.0"

[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Configurations = "5218b696-f38b-4ac9-8b61-a12ec717816d"
GitHubActions = "6b79fd1a-b13a-48ab-b6b0-aaee1fee41df"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Pluto = "c3e4b0f8-55cb-11ea-2926-15256bba5781"
Expand Down
9 changes: 4 additions & 5 deletions src/Export.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function try_fromcache(cache_dir::String, hash::String)
try
open(Pluto.unpack, p, "r")
catch e
@warn "Failed to load statefile from cache" hash exception=(e,catch_backtrace())
@warn "Failed to load statefile from cache" hash exception = (e, catch_backtrace())
end
end
end
Expand All @@ -36,7 +36,7 @@ function try_tocache(cache_dir::String, hash::String, state)
Pluto.pack(io, state)
end
catch e
@warn "Failed to write to cache file" hash exception=(e,catch_backtrace())
@warn "Failed to write to cache file" hash exception = (e, catch_backtrace())
end
end
try_tocache(cache_dir::Nothing, hash, state) = nothing
Expand Down Expand Up @@ -67,7 +67,7 @@ function try_get_exact_pluto_version()
p.git_revision
end
catch e
@error "Failed to get exact Pluto version from dependency. Your website is not guaranteed to work forever." exception=(e, catch_backtrace())
@error "Failed to get exact Pluto version from dependency. Your website is not guaranteed to work forever." exception = (e, catch_backtrace())
Pluto.PLUTO_VERSION
end
end
Expand All @@ -92,15 +92,14 @@ function default_index(paths)
</head>
<body>
<h1>Notebooks</h1>

<ul>
$(join(
if link === nothing
"""<li>$(name) <em style="opacity: .5;">(Loading...)</em></li>"""
else
"""<li><a href="$(link)">$(name)</a></li>"""
end
for (name,link) in paths
for (name, link) in paths
))
</ul>
</body>
Expand Down
4 changes: 2 additions & 2 deletions src/FileHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ endswith_pluto_file_extension(s) = any(endswith(s, e) for e in pluto_file_extens
function without_pluto_file_extension(s)
for e in pluto_file_extensions
if endswith(s, e)
return s[1:end-length(e)]
return s[1:end - length(e)]
end
end
s
Expand Down Expand Up @@ -41,4 +41,4 @@ function find_notebook_files_recursive(start_dir)

# reverse alphabetical order so that week5 becomes available before week4 :)
reverse(plutofiles)
end
end
13 changes: 7 additions & 6 deletions src/HTTPRouter.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

###
# HTTP ROUTER
include("./WebAPI.jl")

function make_router(settings::PlutoDeploySettings, server_session::ServerSession, notebook_sessions::AbstractVector{<:NotebookSession}; static_dir::Union{String,Nothing}=nothing)
router = HTTP.Router()
Expand All @@ -22,7 +23,7 @@ function make_router(settings::PlutoDeploySettings, server_session::ServerSessio

This means that the notebook file used by the web client does not precisely match any of the notebook files running in this server.

If this is an automated setup, then this could happen inotebooketween deployments.
If this is an automated setup, then this could happen inbetween deployments.

If this is a manual setup, then running the .jl notebook file might have caused a small change (e.g. the version number or a whitespace change). Copy notebooks to a temporary directory before running them using the bind server. =#
@info "Request hash not found. See errror hint in my source code." notebook_hash
Expand Down Expand Up @@ -61,7 +62,7 @@ function make_router(settings::PlutoDeploySettings, server_session::ServerSessio
bonds = try
get_bonds(request)
catch e
@error "Failed to deserialize bond values" exception=(e, catch_backtrace())
@error "Failed to deserialize bond values" exception = (e, catch_backtrace())
return HTTP.Response(500, "Failed to deserialize bond values") |> with_cors! |> with_not_cachable!
end

Expand Down Expand Up @@ -90,7 +91,7 @@ function make_router(settings::PlutoDeploySettings, server_session::ServerSessio

topological_order, new_state
catch e
@error "Failed to set bond values" exception=(e, catch_backtrace())
@error "Failed to set bond values" exception = (e, catch_backtrace())
nothing, nothing
end
end
Expand Down Expand Up @@ -179,7 +180,7 @@ function make_router(settings::PlutoDeploySettings, server_session::ServerSessio
end
HTTP.@register(router, "GET", "/*", serve_asset)
end

WebAPI.extend_router!(router, server_session, notebook_sessions, get_sesh)
router
end

Expand Down Expand Up @@ -222,5 +223,5 @@ function temp_index(s::QueuedNotebookSession)
without_pluto_file_extension(s.path) => nothing
end
function temp_index(s::Union{FinishedNotebookSession,RunningNotebookSession})
without_pluto_file_extension(s.path) => without_pluto_file_extension(s.path)*".html"
end
without_pluto_file_extension(s.path) => without_pluto_file_extension(s.path) * ".html"
end
68 changes: 35 additions & 33 deletions src/PlutoSliderServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ using Sockets
using Configurations
using TOML

using Logging: global_logger
using GitHubActions: GitHubActionsLogger
using Logging:global_logger
using GitHubActions:GitHubActionsLogger
function __init__()
get(ENV, "GITHUB_ACTIONS", "false") == "true" && global_logger(GitHubActionsLogger())
end
Expand All @@ -34,7 +34,7 @@ Base.@kwdef struct RunningNotebookSession <: NotebookSession
hash::String
notebook::Pluto.Notebook
original_state
token::Token=Token()
token::Token = Token()
bond_connections::Dict{Symbol,Vector{Symbol}}
end

Expand All @@ -57,38 +57,38 @@ end
UnionNothingString = Any

@option struct SliderServerSettings
exclude::Vector=String[]
port::Integer=2345
host="127.0.0.1"
simulated_lag::Real=0
serve_static_export_folder::Bool=true
exclude::Vector = String[]
port::Integer = 2345
host = "127.0.0.1"
simulated_lag::Real = 0
serve_static_export_folder::Bool = true
end

@option struct ExportSettings
output_dir::UnionNothingString=nothing
exclude::Vector=String[]
ignore_cache::Vector=String[]
pluto_cdn_root::UnionNothingString=nothing
baked_state::Bool=true
offer_binder::Bool=true
disable_ui::Bool=true
cache_dir::UnionNothingString=nothing
slider_server_url::UnionNothingString=nothing
binder_url::UnionNothingString=nothing
create_index::Bool=true
output_dir::UnionNothingString = nothing
exclude::Vector = String[]
ignore_cache::Vector = String[]
pluto_cdn_root::UnionNothingString = nothing
baked_state::Bool = true
offer_binder::Bool = true
disable_ui::Bool = true
cache_dir::UnionNothingString = nothing
slider_server_url::UnionNothingString = nothing
binder_url::UnionNothingString = nothing
create_index::Bool = true
end

@option struct PlutoDeploySettings
SliderServer::SliderServerSettings=SliderServerSettings()
Export::ExportSettings=ExportSettings()
SliderServer::SliderServerSettings = SliderServerSettings()
Export::ExportSettings = ExportSettings()
end


function get_configuration(toml_path::Union{Nothing,String}=nothing; kwargs...)
if !isnothing(toml_path) && isfile(toml_path)
toml_d = TOML.parsefile(toml_path)

relevant_for_me = filter(toml_d) do (k,v)
relevant_for_me = filter(toml_d) do (k, v)
k ∈ ["SliderServer", "Export"]
end
relevant_for_pluto = get(toml_d, "Pluto", Dict())
Expand All @@ -101,7 +101,7 @@ function get_configuration(toml_path::Union{Nothing,String}=nothing; kwargs...)
kwargs_dict = Configurations.to_dict(Configurations.from_kwargs(PlutoDeploySettings; kwargs...))
(
Configurations.from_dict(PlutoDeploySettings, merge_recursive(relevant_for_me, kwargs_dict)),
Pluto.Configuration.from_flat_kwargs(;(Symbol(k) => v for (k,v) in relevant_for_pluto)...),
Pluto.Configuration.from_flat_kwargs(;(Symbol(k) => v for (k, v) in relevant_for_pluto)...),
)
else
(
Expand All @@ -117,8 +117,8 @@ merge_recursive(a, b) = b
include("./HTTPRouter.jl")



export export_directory, run_directory, github_action
include("./cli.jl")
export export_directory, run_directory, github_action, cli, FinishedNotebookSession, RunningNotebookSession, QueuedNotebookSession, myhash, MoreAnalysis


"""
Expand All @@ -132,7 +132,7 @@ Search recursively for all Pluto notebooks in the current folder, and for each n
- The state object embedded
- Extra functionality enabled, such as hidden UI, binder button, and a live bind server

# Keyword rguments
# Keyword arguments
- `Export_exclude::Vector{String}=[]`: list of notebook files to skip. Provide paths relative to `start_dir`.
- `Export_disable_ui::Bool=true`: hide all buttons and toolbars to make it look like an article.
- `Export_baked_state::Bool=true`: base64-encode the state object and write it inside the .html file. If `false`, a separate `.plutostate` file is generated.
Expand Down Expand Up @@ -170,8 +170,9 @@ If `static_export` is `true`, then additional `Export_` keywords can be given, s
function run_directory(
start_dir::String=".";
notebook_paths::Vector{String}=find_notebook_files_recursive(start_dir),
static_export::Bool=false, run_server::Bool=true,
on_ready::Function=((args...)->()),
static_export::Bool=false,
run_server::Bool=true,
on_ready::Function=((args...) -> ()),
config_toml_path::Union{String,Nothing}=joinpath(Base.active_project() |> dirname, "PlutoDeployment.toml"),
kwargs...
)
Expand Down Expand Up @@ -208,8 +209,8 @@ function run_directory(
if run_server
static_dir = (
static_export && settings.SliderServer.serve_static_export_folder
) ? output_dir : nothing
router = make_router(settings, server_session, notebook_sessions; static_dir )
) ? output_dir : nothing
router = make_router(settings, server_session, notebook_sessions; static_dir)
# This is boilerplate HTTP code, don't read it
host = settings.SliderServer.host
port = settings.SliderServer.port
Expand Down Expand Up @@ -256,12 +257,13 @@ function run_directory(
end
end
else
http_server_task = @async 1+1
http_server_task = @async 1 + 1
serversocket = nothing
end

if static_export && settings.Export.create_index
exists = any(["index.html", "index.md", ("index"*e for e in pluto_file_extensions)...]) do f
ENV["current_root"] = output_dir
exists = any(["index.html", "index.md", ("index" * e for e in pluto_file_extensions)...]) do f
joinpath(output_dir, f) |> isfile
end
if !exists
Expand Down Expand Up @@ -305,7 +307,7 @@ function run_directory(
try_tocache(settings.Export.cache_dir, hash, original_state)
catch e
(e isa InterruptException) || rethrow(e)
@error "Failed to run notebook!" path exception=(e,catch_backtrace())
@error "Failed to run notebook!" path exception = (e, catch_backtrace())
continue
end
end
Expand Down
Loading