-
-
Notifications
You must be signed in to change notification settings - Fork 293
β‘ Writing and running code
How do I make my variables reactive?
All global variables are automatically reactive!
How do I write multiple expressions in one cell?
You can use a
let ... end
block or abegin ... end
block to wrap multiple expressions into one. But you probably want to create multiple smaller cells instead! It makes reactivity much more useful.
I used print
and display
but nothing is happening! Help!
These functions will still write to the console where you started Pluto. To display things in Pluto.jl, you simply output the value you want to show.
For example, instead of
begin
x = 25.0
display(sqrt(x))
end
begin
y = 0.1
display(cos(y))
display(sin(y))
end
you can write
begin
x = 25.0
sqrt(x)
end
begin
y = 0.1
cos(y), sin(y)
end
In Julia, the last subexpression in a block (
begin
,let
,if
, etc.) is the output value.(If you need the plaintext result of
PlutoUI.Print
.)
How can I modify a variable in a different cell?
Variables can only be assigned and modified in a single cell. This is what makes reactivity possible:
For example, if you have four cells:
"I have $(n) pets"
,n += 3
,n *= 2
andn = 0
, what is supposed to happen?Try to write all assignments in one cell, using a
begin ... end
or alet ... end
block, or try renaming your variables. The example above would become:
"I have $(n_today) pets"
begin
n_today = n_yesterday
n_today += 3
n_today *= 2
end
n_yesterday = 0
That doesn't work for me!
Sometimes a mutable state makes the most sense for your problem.
In Pluto, assignments to values (e.g.
a = 1
) will 'trigger' reactivity, but assignments to properties (e.g.a.start = 2
ora[5] = 3
) will not. This means that you can use aRef
(docs) to create 'non-reactive' variables.
How can I write different methods/specialisations for a function?
You need to put all definitions in one cell, using a
begin ... end
block.
Can I use @async
to update values reactively on demand?
Unfortunately not. Pluto.jl uses static code analysis to determine which cells to run next β it does not use wrappers or a polling mechanism to watch variables in real-time. However, the
@bind
macro might be able to do what you want! Have a look at the Interactivity sample inside Pluto. Feel free to open a GitHub issue if this doesn't suit your needs.
How can I load startup.jl
?
We want your notebooks to be reproducible, so our suggested method is that you copy and paste your personal setup script into a
begin ... end
block inside your notebook. This avoids implicit dependencies of your notebook. Alternatively, you could writeinclude("path/to/startup.jl")
, but this will only work on your computer.
How can I load a notebook upon launching Pluto?
You can pass the
notebook
keyword argument toPluto.run
, e.g.Pluto.run(notebook="/path/to/notebook.jl")
.
How can I use a sysimage inside Pluto?
Pluto.run()
takes a keyword argumentsysimage
which can be used to add sysimage used by each notebook. To load the currently loaded sysimage, start pluto using:
Pluto.run(sysimage=unsafe_string(Base.JLOptions().image_file))