Passing command-line arguments to a Pluto Notebook

44 Views Asked by At

One of the advantages of Pluto is that a notebook my_notebook.jl is also a Julia script that can be run using julia my_notebook.jl.

In a Julia script, it is easy to set the content of variables using command-line arguments, for instance the following code

# example.jl
println("Hello $ARGS[1]")

will output Hello there when run using julia example.jl there.

It turns out that this works as well in a Pluto notebook, but only if you execute it as a script julia my_notebook.jl there.

I would like to be able to open the notebook using julia -e "using Pluto; Pluto.run(notebook=\"example.jl\")" there and see the result inside the notebook. It turns out this syntax returns an error: I could not find any way to populate the variable ARGS inside the Pluto notebook.

Is there a way this can be achieved?

1

There are 1 best solutions below

1
Przemyslaw Szufel On BEST ANSWER

While it seems that there is no such option around the Pluto.Configuration module, this can be easily circumvented by using system environment variables which are inherited by the child process.

Consider this Julia session:

$ julia -e "" --interactive one two

julia> ARGS
2-element Vector{String}:
 "one"
 "two"

julia> ENV["PLUTO_ARGS"] = join(ARGS,"\n");

julia> using Pluto

julia> Pluto.run()

Now in Pluto you can see those parameters in ENV["PLUTO_ARGS"]:

enter image description here