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

Feat: serialize Pluto.activate_notebook_environment to allow running Pluto notebooks as scripts #2573

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
10 changes: 8 additions & 2 deletions src/notebook/saving and loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ In the produced file, cells are not saved in the notebook order. If `notebook.to
Have a look at our [JuliaCon 2020 presentation](https://youtu.be/IAF8DjrQSSk?t=1085) to learn more!
"""
function save_notebook(io::IO, notebook::Notebook)
using_plutopkg = notebook.nbpkg_ctx !== nothing

println(io, _notebook_header)
println(io, "# ", PLUTO_VERSION_STR)

Expand All @@ -45,6 +47,12 @@ function save_notebook(io::IO, notebook::Notebook)

# Anything between the version string and the first UUID delimiter will be ignored by the notebook loader.
println(io, "")

if using_plutopkg
println(io, PlutoRunner.script_pkg(PLUTO_VERSION_STR))
println(io, "")
end

println(io, "using Markdown")
println(io, "using InteractiveUtils")
# Super Advanced Code Analysis™ to add the @bind macro to the saved file if it's used somewhere.
Expand Down Expand Up @@ -81,8 +89,6 @@ function save_notebook(io::IO, notebook::Notebook)
end


using_plutopkg = notebook.nbpkg_ctx !== nothing

write_package = if using_plutopkg
ptoml_contents = PkgCompat.read_project_file(notebook)
mtoml_contents = PkgCompat.read_manifest_file(notebook)
Expand Down
12 changes: 12 additions & 0 deletions src/runner/PlutoRunner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,18 @@ const fake_bind = """macro bind(def, element)
end
end"""

# At some point it would be nice to move
# activate_notebook_environment either to Pkg or PlutoPkg.jl
script_pkg(version) = """
if !isdefined(Main, :PlutoRunner)
import Pkg
Pkg.activate(;temp=true)
Pkg.add(Pkg.Types.PackageSpec(name="Pluto", version="$version"))
import Pluto
Pluto.activate_notebook_environment(@__FILE__)
Pkg.instantiate()
end
"""



Expand Down
Loading