Skip to content

Commit

Permalink
formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Nov 26, 2021
1 parent 29475c1 commit 5068875
Show file tree
Hide file tree
Showing 17 changed files with 572 additions and 416 deletions.
3 changes: 3 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

whitespace_in_kwargs = false
trailing_comma = true
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"julia.format.ops": false,
"julia.format.iterOps": false,
"julia.format.tuples": false,
"editor.formatOnSave": false
"editor.formatOnSave": true
}
145 changes: 72 additions & 73 deletions src/Actions.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

import Pluto: Pluto, without_pluto_file_extension, generate_html, @asynclog
using Base64
using SHA
using FromFile

@from "./MoreAnalysis.jl" import bound_variable_connections_graph
@from "./MoreAnalysis.jl" import bound_variable_connections_graph
@from "./Export.jl" import try_get_exact_pluto_version, try_fromcache, try_tocache
@from "./Types.jl" import NotebookSession, RunningNotebook, FinishedNotebook
@from "./Configuration.jl" import PlutoDeploySettings
Expand All @@ -14,52 +13,47 @@ function path_hash(path)
myhash(read(path))
end

showall(xs) = Text(join(string.(xs),"\n"))
showall(xs) = Text(join(string.(xs), "\n"))


###
# Shutdown
function process(s::NotebookSession{String,Nothing,<:Any};
server_session::Pluto.ServerSession,
settings::PlutoDeploySettings,
output_dir::AbstractString,
start_dir::AbstractString,
progress,
)::NotebookSession


function process(
s::NotebookSession{String,Nothing,<:Any};
server_session::Pluto.ServerSession,
settings::PlutoDeploySettings,
output_dir::AbstractString,
start_dir::AbstractString,
progress,
)::NotebookSession


if s.run isa RunningNotebook
Pluto.SessionActions.shutdown(server_session, s.run.notebook)
end

try
remove_static_export(s.path;
settings,
output_dir,
)
remove_static_export(s.path; settings, output_dir)
catch e
@warn "Failed to remove static export files" s.path exception=(e,catch_backtrace())
@warn "Failed to remove static export files" s.path exception =
(e, catch_backtrace())
end

@info "### ✓ $(progress) Shutdown complete" s.path

NotebookSession(;
path=s.path,
current_hash=nothing,
desired_hash=nothing,
run=nothing,
)
NotebookSession(; path=s.path, current_hash=nothing, desired_hash=nothing, run=nothing)
end

###
# Launch
function process(s::NotebookSession{Nothing,String,<:Any};
server_session::Pluto.ServerSession,
settings::PlutoDeploySettings,
output_dir::AbstractString,
start_dir::AbstractString,
progress,
)::NotebookSession
function process(
s::NotebookSession{Nothing,String,<:Any};
server_session::Pluto.ServerSession,
settings::PlutoDeploySettings,
output_dir::AbstractString,
start_dir::AbstractString,
progress,
)::NotebookSession

path = s.path
abs_path = joinpath(start_dir, path)
Expand All @@ -71,7 +65,7 @@ function process(s::NotebookSession{Nothing,String,<:Any};
if new_hash != s.desired_hash
@warn "Notebook file does not have desired hash. This probably means that the file changed too quickly. Continuing and hoping for the best!" s.path new_hash s.desired_hash
end

keep_running = settings.SliderServer.enabled
skip_cache = keep_running || path settings.Export.ignore_cache

Expand All @@ -80,10 +74,7 @@ function process(s::NotebookSession{Nothing,String,<:Any};
run = if cached_state !== nothing
@info "Loaded from cache, skipping notebook run" s.path new_hash
original_state = cached_state
FinishedNotebook(;
path,
original_state,
)
FinishedNotebook(; path, original_state)
else
try
# open and run the notebook
Expand All @@ -100,27 +91,23 @@ function process(s::NotebookSession{Nothing,String,<:Any};
bond_connections = bound_variable_connections_graph(notebook)
@info "Bond connections" s.path showall(collect(bond_connections))

RunningNotebook(;
path,
notebook,
original_state,
bond_connections,
)
RunningNotebook(; path, notebook, original_state, bond_connections)
else
FinishedNotebook(;
path,
original_state,
)
FinishedNotebook(; path, original_state)
end
catch e
(e isa InterruptException) || rethrow(e)
@error "$(progress) Failed to run notebook!" path exception=(e,catch_backtrace())
@error "$(progress) Failed to run notebook!" path exception =
(e, catch_backtrace())
# continue
nothing
end
end

generate_static_export(path, run.original_state, jl_contents;
generate_static_export(
path,
run.original_state,
jl_contents;
settings,
start_dir,
output_dir,
Expand All @@ -138,30 +125,32 @@ end

###
# Update if needed
function process(s::NotebookSession{String,String,<:Any};
server_session::Pluto.ServerSession,
settings::PlutoDeploySettings,
output_dir::AbstractString,
start_dir::AbstractString,
progress,
)::NotebookSession
function process(
s::NotebookSession{String,String,<:Any};
server_session::Pluto.ServerSession,
settings::PlutoDeploySettings,
output_dir::AbstractString,
start_dir::AbstractString,
progress,
)::NotebookSession

if s.current_hash != s.desired_hash
@info "Updating notebook... will shut down and relaunch" s.path

# Simple way to update: shut down notebook and start new one
if s.run isa RunningNotebook
Pluto.SessionActions.shutdown(server_session, s.run.notebook)
end

@info "Shutdown complete" s.path

result = process(NotebookSession(;
path=s.path,
current_hash=nothing,
desired_hash=s.desired_hash,
run=nothing,
);
result = process(
NotebookSession(;
path=s.path,
current_hash=nothing,
desired_hash=s.desired_hash,
run=nothing,
);
server_session,
settings,
output_dir,
Expand Down Expand Up @@ -200,7 +189,14 @@ Core Action: Generate static export for a Pluto Notebook
4. baked_state: Whether to export pluto state within the html or in a separate file.
5. pluto_cdn_root: URL where pluto will go to find the static frontend assets
"""
function generate_static_export(path, original_state, jl_contents; settings, output_dir, start_dir)
function generate_static_export(
path,
original_state,
jl_contents;
settings,
output_dir,
start_dir,
)
pluto_version = try_get_exact_pluto_version()
export_jl_path = let
relative_to_notebooks_dir = path
Expand All @@ -221,7 +217,9 @@ function generate_static_export(path, original_state, jl_contents; settings, out
mkpath(dirname(export_statefile_path))


slider_server_running_somewhere = settings.Export.slider_server_url !== nothing || (settings.SliderServer.serve_static_export_folder && settings.SliderServer.enabled)
slider_server_running_somewhere =
settings.Export.slider_server_url !== nothing ||
(settings.SliderServer.serve_static_export_folder && settings.SliderServer.enabled)

notebookfile_js = if settings.Export.offer_binder || slider_server_running_somewhere
if settings.Export.baked_notebookfile
Expand All @@ -235,10 +233,7 @@ function generate_static_export(path, original_state, jl_contents; settings, out
slider_server_url_js = if slider_server_running_somewhere
abs_path = joinpath(start_dir, path)
url_of_root = relpath(start_dir, dirname(abs_path)) # e.g. "." or "../../.."
repr(something(
settings.Export.slider_server_url,
url_of_root
))
repr(something(settings.Export.slider_server_url, url_of_root))
else
"undefined"
end
Expand All @@ -263,13 +258,16 @@ function generate_static_export(path, original_state, jl_contents; settings, out
html_contents = generate_html(;
pluto_cdn_root=settings.Export.pluto_cdn_root,
version=pluto_version,
notebookfile_js, statefile_js,
slider_server_url_js, binder_url_js,
disable_ui=settings.Export.disable_ui
notebookfile_js,
statefile_js,
slider_server_url_js,
binder_url_js,
disable_ui=settings.Export.disable_ui,
)
write(export_html_path, html_contents)

if (settings.Export.offer_binder || settings.Export.slider_server_url !== nothing) && !settings.Export.baked_notebookfile
if (settings.Export.offer_binder || settings.Export.slider_server_url !== nothing) &&
!settings.Export.baked_notebookfile
write(export_jl_path, jl_contents)
end

Expand Down Expand Up @@ -297,7 +295,8 @@ function remove_static_export(path; settings, output_dir)
tryrm(export_statefile_path)
end
tryrm(export_html_path)
if (settings.Export.offer_binder || settings.Export.slider_server_url !== nothing) && !settings.Export.baked_notebookfile
if (settings.Export.offer_binder || settings.Export.slider_server_url !== nothing) &&
!settings.Export.baked_notebookfile
tryrm(export_jl_path)
end
end
64 changes: 38 additions & 26 deletions src/Configuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,71 @@ using FromFile


@extract_docs @option struct SliderServerSettings
enabled::Bool=true
enabled::Bool = true
"List of notebook files to skip. Provide paths relative to `start_dir`. *If `Export.enabled` is `true` (default), then only paths in `SliderServer_exclude ∩ Export_exclude` will be skipped, paths in `setdiff(SliderServer_exclude, Export_exclude)` will be shut down after exporting.*"
exclude::Vector=String[]
exclude::Vector = String[]
"Port to run the HTTP server on."
port::Integer=2345
port::Integer = 2345
"""Often set to `"0.0.0.0"` on a server."""
host::Any="127.0.0.1"
simulated_lag::Real=0
host::Any = "127.0.0.1"
simulated_lag::Real = 0
"Besides handling slider server request, should we also run a static file server of the export output folder? Set to `false` if you are serving the HTML files in another way, e.g. using GitHub Pages, and, for some reason, you do not want to *also* serve the HTML files using this serve."
serve_static_export_folder::Bool=true
serve_static_export_folder::Bool = true
"Watch the input directory for file changes, and update the slider server sessions automatically. Only takes effect when running the slider server. More info in the README."
watch_dir::Bool=true
watch_dir::Bool = true
end

@extract_docs @option struct ExportSettings
"Generate static HTML files? This setting can only be `false` if you are also running a slider server."
enabled::Bool=true
enabled::Bool = true
"Folder to write generated HTML files to (will create directories to preserve the input folder structure). The behaviour of the default value depends on whether you are running the slider server, or just exporting. If running the slider server, we use a temporary directory; otherwise, we use `start_dir` (i.e. we generate each HTML file in the same folder as the notebook file)."
output_dir::Union{Nothing,String}=nothing
output_dir::Union{Nothing,String} = nothing
"List of notebook files to skip. Provide paths relative to `start_dir`."
exclude::Vector{String}=String[]
exclude::Vector{String} = String[]
"List of notebook files that should always re-run, skipping the `cache_dir` system. Provide paths relative to `start_dir`."
ignore_cache::Vector=String[]
pluto_cdn_root::Union{Nothing,String}=nothing
ignore_cache::Vector = String[]
pluto_cdn_root::Union{Nothing,String} = nothing
"base64-encode the state object and write it inside the .html file. If `false`, a separate `.plutostate` file is generated. A separate statefile allows us to show a loading bar in pluto while the statefile is loading, but it can complicate setup in some environments."
baked_state::Bool=true
baked_notebookfile::Bool=true
baked_state::Bool = true
baked_notebookfile::Bool = true
"Hide all buttons and toolbars in Pluto to make it look like an article."
disable_ui::Bool=true
disable_ui::Bool = true
"""Show a "Run on Binder" button on the notebooks."""
offer_binder::Bool=true
offer_binder::Bool = true
"e.g. `https://mybinder.org/v2/gh/mitmath/18S191/e2dec90`. Defaults to a binder repo that runs the correct version of Pluto -- https://github.com/fonsp/pluto-on-binder. TODO docs"
binder_url::Union{Nothing,String}=nothing
binder_url::Union{Nothing,String} = nothing
"""If 1) you are using this setup to export HTML files for notebooks, AND 2) you are running the slider server **on another setup/computer**, then this setting should be the URL pointing to the slider server, e.g. `"https://sliderserver.mycoolproject.org/"`. For example, you need this if you use GitHub Actions and GitHub Pages to generate HTML files, with a slider server on DigitalOcean. === If you only have *one* server for both the static exports and the slider server, and people will read notebooks directly on your server, then the default value `nothing` will work: it will automatically make the HTML files use their current domain for the slider server."""
slider_server_url::Union{Nothing,String}=nothing
slider_server_url::Union{Nothing,String} = nothing
"If provided, use this directory to read and write cached notebook states. Caches will be indexed by the hash of the notebook file, but you need to take care to invalidate the cache when Pluto or this export script updates. Useful in combination with https://github.com/actions/cache, see https://github.com/JuliaPluto/static-export-template for an example."
cache_dir::Union{Nothing,String}=nothing
cache_dir::Union{Nothing,String} = nothing
"Automatically generate an `index.html` file, listing all the exported notebooks (only if no `index.jl` or `index.html` file exists already)."
create_index::Bool=true
create_index::Bool = true
end

@option struct PlutoDeploySettings
SliderServer::SliderServerSettings=SliderServerSettings()
Export::ExportSettings=ExportSettings()
Pluto::Pluto.Configuration.Options=Pluto.Configuration.Options()
SliderServer::SliderServerSettings = SliderServerSettings()
Export::ExportSettings = ExportSettings()
Pluto::Pluto.Configuration.Options = Pluto.Configuration.Options()
end

function get_configuration(toml_path::Union{Nothing,String}=nothing; kwargs...)::PlutoDeploySettings
function get_configuration(
toml_path::Union{Nothing,String}=nothing;
kwargs...,
)::PlutoDeploySettings
# we set `Pluto_server_notebook_path_suggestion=joinpath(homedir(),"")` to because the default value for this setting changes when the pwd changes. This causes our run_git_directory to exit... Not necessary for Pluto 0.17.3 and up.
if !isnothing(toml_path) && isfile(toml_path)
Configurations.from_toml(PlutoDeploySettings, toml_path; Pluto_server_notebook_path_suggestion=joinpath(homedir(),""), kwargs...)
Configurations.from_toml(
PlutoDeploySettings,
toml_path;
Pluto_server_notebook_path_suggestion=joinpath(homedir(), ""),
kwargs...,
)
else
Configurations.from_kwargs(PlutoDeploySettings; Pluto_server_notebook_path_suggestion=joinpath(homedir(),""), kwargs...)
Configurations.from_kwargs(
PlutoDeploySettings;
Pluto_server_notebook_path_suggestion=joinpath(homedir(), ""),
kwargs...,
)
end
end

Expand Down
Loading

0 comments on commit 5068875

Please sign in to comment.