Julia: First Contact - Basic Pluto
What is Pluto ?
Pluto is a browser based notebook interface for the Julia language. It allows to present Julia code and computational results in a tightly linked fashion.
For those familiar with spreadsheets: Pluto is like Google Sheets or Excel but with Julia code in its cells. Pluto cells are arranged in one broad column. Communication of data between cells works via variables defined in the cells instead of cell references like
A5
etc. With Excel and other spreadsheets, Pluto shares the idea of reactivity: If a variable value is changed in the cell where it is defined, the code in all dependent cells aka cells using this variable is executed.For those familiar with Jupyter notebooks: Pluto is like Jupyter for Julia, but without the hidden state created by the unlimited possibility to execute cells in arbitrary sequence. Instead, it enhances the notebook concept by reactivity.
Pluto is implemented in a combination of Julia and javascript, and can be installed like any other Julia package.
During this course, Pluto notebooks will be used to present numerical methods implemented in Julia.
Pluto resources:
How to Install Pluto (straight from the main author Fons van der Plas)
Sample notebooks are available via the index page after starting Pluto.
Pluto structure
Pluto notebooks consist of a sequence of cells which contain valid Julia code. The result of execution of the code in a cell is its return value which is displayed on top of the cell.
Text cells and cell visibility
Cells can consist of a string with text in Markdown format. This single text string is valid Julia code and thus returned, formatted and shown as text.
Cells can be visible...
xxxxxxxxxx
md"""
Cells can be visible...
"""
... or hidden, but their return value is visible nevertheless.Visibility can be toggled via the eye symbol on the top left of the cell. We will use markdown cells for displaying text and explanatory information, and keep them hidden.
LaTeX
Cells can contain
xxxxxxxxxx
md"""
##### LaTeX
Cells can contain $\LaTeX$ math code: $\int_0^1 sin(π ξ) dξ$. Just surround
it by \$ symbols as in usual $\LaTeX$ texts or by double backtics: ``\int_0^1 sin(π ξ) dξ``.
The later method is safer as it does not collide with string interpolation (explained below).
"""
Code cells
Code cells are cells which just contain "normal" Julia code. Running the code in the cell is triggered by the Shift-Enter
keyboard combination or clicking on the triangle symbol on the right below the cell.
Variables and reactivity
We can define a variable in a cell.The assignment has a return value like any other Julia statement which is shown on top of the cell.
5
xxxxxxxxxx
x=5
A variable defined in one cell can be used in another cell. Moreover, if the value is changed, the other cell reacts and the code contained in that cell is executed with the new value of the variable. This reactive behaviour typical for a spreadsheet.
6
xxxxxxxxxx
x+1
One can return several results by stating them separated by ,
. The returned value then is a tuple.
6
7
8
xxxxxxxxxx
x+1,x+2,x+3
xxxxxxxxxx
md"""
Display of the return value can be suppressed by ending the last statement with `;`
""";
Only one statement per cell
Each cell can contain only exactly one Julia statement. If multiple expressions are desired, they can made into one by surrounding them by begin
and end
. The return value will be the return value of the last expression in the statement.
-0.9997551733586199
xxxxxxxxxx
begin
z=x+v
sin(z)
end
An alternative way to have all statements on one line, separated by ;
:
0.022126756261955736
xxxxxxxxxx
z1=x+v; cos(z1)
However, in this situation the better structural decision would be to combine the statements into a function defined in one cell and to call it in another cell.
xxxxxxxxxx
function f(x,v)
z=x+v
cos(z)
end;
0.022126756261955736
xxxxxxxxxx
f(x,v)
Interactivity
We can bind interactive HTML elements to variables:
v=50 (This uses string interpolation to print the value of v into the Markdown string)
xxxxxxxxxx
md"""v=$(v) (This uses _string interpolation_ to print the value of v into the Markdown string)"""
This example also shows that the dependency of one cell from another is defined via the involved variables and not by the sequence in the notebook: the value v
in the cells above is defined by the slider.
In order to achieve this, Pluto makes extensive use of the possibility to inspect the variables defined in a running Julia instance using Julia itself.
Deactivating code
We occasionally will use the possibility to deactivate cells before running their code. This can be useful for preventing long runnig code to start immediately after loading the notebook or for pedagogical reasons.
The preferred pattern for this uses a checkbox bound to a logical variable.
Run next cell:
xxxxxxxxxx
if allow_run
using LinearAlgebra
a=rand(2000,2000)
eigvals(inv(a))
end
Accessing text output
Normally text output from statements in a cell is shown in the console window where Pluto was started, and not in the notebook, as Pluto focuses on the presentation of the results. Sometimes it is however desirable to inspect this output instead of the result returned. For this purpose, we use the Julia package PlutoUI.jl
which defines the function with_terminal
:
xxxxxxxxxx
begin
import Pkg
Pkg.activate(mktempdir())
Pkg.add("PlutoUI")
end
xxxxxxxxxx
using PlutoUI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
xxxxxxxxxx
with_terminal() do
for i=1:30
println(i)
end
end
Live docs
The live docs pane in the lower right bottom allows to quickly obtain help information about documented Julia functions etc.