1

I would like to instantiate the project.toml that's build in in a Pluto notebook with the native package manager. How do I read it from the notebook?

Say, I have a notebook, e.g.,

nb_source = "https://raw.githubusercontent.com/fonsp/Pluto.jl/main/sample/Interactivity.jl"

How can I create a temporary environment, and get the packages for the project of this notebook? In particular, how do I complete the following code?

cd(mktempdir()) 
import Pkg; Pkg.activate(".") 
import Pluto, Pkg 

nb = download(nb_source, ".") 

### Some code using Pluto's build in package manager 
### to read the Project.toml from nb --> nb_project_toml 

cp(nb_project_toml, "./Project.toml", force=true) 
Pkg.instantiate(".")

2 Answers2

1

So, first of all, the notebook you are looking at is a Pluto 0.17.0 notebook, which does not have the internal package manager. I think it was added in Pluto 0.19.0.

This is what the very last few cells look like in a notebook using the internal pluto packages:

# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
Plots = "~1.32.0"
PlutoUI = "~0.7.40"
PyCall = "~1.94.1"
"""

# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised

julia_version = "1.8.0"
...

so you could add something like:

import(nb)
write("./Project.toml", PLUTO_PROJECT_TOML_CONTENTS)

This has the drawback of running all the code in your notebook, which might take a while.

Alternatively, you could read the notebook file until you find the # ╔═╡ 00000000-0000-0000-0000-000000000001 line and then either parse the following string yourself or eval everything after that (something like eval(Meta.parse(string_stuff_after_comment)) should do it...)

I hope that helps a little bit.

HenrikWolf
  • 101
  • 1
  • 6
0

The Pluto.load_notebook_nobackup() reads the information of a notebook. This gives a dictionary of deps in the field .nbpkg_ctx.env.project.deps

import Pluto, Pkg 
Pkg.activate(;temp=true)  
nb_source = "https://raw.githubusercontent.com/fonsp/Pluto.jl/main/sample/PlutoUI.jl.jl" 
nb = download(nb_source)
nb_info = Pluto.load_notebook_nobackup(nb)
deps = nb_info.nbpkg_ctx.env.project.deps 
Pkg.add([Pkg.PackageSpec(name=p, uuid=u) for (p, u) in deps])