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:

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.

4 ms

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.

8 μs

Cells can be visible...

3.4 μs

... 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.

2.3 μs
LaTeX

Cells can contain LATEX math code: 01sin(πξ)dξ. Just surround it by $ symbols as in usual LATEX texts or by double backtics: 01sin(πξ)dξ. The later method is safer as it does not collide with string interpolation (explained below).

4.4 μs

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.

6.7 μs

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.

4.3 μs
x
5
1.2 μs

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.

4.9 μs
6
4.1 μs

One can return several results by stating them separated by , . The returned value then is a tuple.

2.9 μs
8 μs
2.6 μs

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.

6.4 μs
-0.9997551733586199
72.6 ms

An alternative way to have all statements on one line, separated by ;:

2.9 μs
0.022126756261955736
29.5 ms

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.

2.6 μs
24.4 μs
0.022126756261955736
2.7 ms

Interactivity

We can bind interactive HTML elements to variables:

3.3 μs
11.2 ms

v=50 (This uses string interpolation to print the value of v into the Markdown string)

7 μs

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.

3.2 μs

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:

40.5 μs
562 ns

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:

4.1 μs
6.9 s
76.1 ms
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
107 ms

Live docs

The live docs pane in the lower right bottom allows to quickly obtain help information about documented Julia functions etc.

5.3 μs