I would like to know which is the reason for using begin-end scopes in each cell of Pluto.
Another question is to know if there’s a way to avoid having to write begin-end in each cell? I would like this approach so that my notebooks look like my scripts style (which doesn’t make use of begin-end scopes).
Thank you in advance.
Pluto only accepts one instruction per cell.
Why? Because the developers wanted so. It is a design choice, afaik to make the coding more explicit.
The Neptune.jl fork of Pluto doesn’t have this restriction. But I don’t know how well maintained and featured is it relative to Pluto.
That said, it is good practice in Julia to wrap things in functions, thus if you take that habit to the Pluto notebook, on cell will have the function definition, other some setup, other just the execution. It turns out to be organized.
Large cells with begin-end blocks are rarely needed in Pluto.
In general, it is better in notebook systems (also in Jupyter) to use small cells, ideally with only 1 LOC each. This also benefits the reactivity system of Pluto - in case of changes often less code needs to be re-executed when the code is split to multiple cells.
Furthermore, consider using functions or let blocks instead of begin-blocks. These introduce local variable scopes and therefore allow freeing up memory used by block-local variables.
For what it’s worth - I think using 1 LOC per cell in a Jupyter notebook is a really bad idea.
I have taught programming for 7 years using notebooks and the most common source of error is disorganization: students running cells out of order or leaving broken code cells in the notebook. When something doesn’t work, they just copy the bad code cell and make edits to it to try to fix it, never deleting the first one.
It’s clear that Fons and the other developers of Pluto had a similar experience, and almost all of their design choices make this sort of “code graveyard” impossible. It also ensures that 1 LOC per cell actually works, instead of being a potential source of error.
Thank you for your answer. I am new to Julia and with my questions I am not judging any design choice but trying to learn good programming practices.
I would appreciate if you could explain the relation between functions and begin/end blocks.
In your normal scripts (outside of Pluto) do you use begin/end blocks to separate the code as if it was made of “cells” ?
Thanks. I think I do not understand the effect of the begin/end block in a piece of code. What is it in a normal Julia script (outside Pluto)? Does it introduce a local scope ?
No, I don’t use begin-end blocks almost never. To introduce new scopes generally
let blocks are used.
But most of the time just using functions is enough.
A typical “script” I write looks like:
function f1(args) ... end function main() data = ... result = f1(data) end
Which is run with
julia> main(). This is practical because when using Revise you can track the changes without having to reload anything.
In a Pluto notebook
f1 would be identical, and probably the code in
main would be written splitted in cells.
Here is an example, sort of randomly chosen from some recent notebooks I’ve been writing. Of course much better examples exist.
begin/end requirement in Pluto seems to be a way to push users towards one-liners by making multi-line cells ‘ugly’. Is there any other purpose?
It’s quite annoying, especially since I increasingly like Pluto, and enjoy using it for exploration and demonstration.
This has been discussed in
Chances are it will be relaxed at some point.
Thank you very much! In case you must introduce some definitions of initial conditions or parameters, how do you use a function for that?
I have looked at the link, it is an astrophysics notebook. Do you work on that?
In regular script I usually put everything in the
main function. In a notebook I think more about how I’m planning to present and discuss the data, so no single solution there.
No, I don’t, I do molecular stimulations. But that is to exemplify a package that can be useful for particle simulations in general. Lots of people here work on astrophysics, though, there is a slack Channel and regular meetings on the subject.
For Pluto’s reactivity this may be the case, but I also disagree that this is a good idea for Jupyter in general. I do not use
begin end, but I use
let end and every cell of mine is multi-line (often something between 7 and 30 lines). Using functions does not make much sense, because I often want have slightly different blocks for each of 2~3 datasets (and making a single function that deal with all of them is more error prone and more work than its worth), using single lines would be terrible, as I could not isolate intermediary variables from the larger scope.
Also all depends on what one uses the notebooks for. I only use them to produce didactical material. Others use them for actual production coding. The style and requirements of one or other are different.
For didactical purposes the current behavior (and implied style) of Pluto is mostly fine, I think. It makes everything very transparent to the reader.
Ok. I was thinking in using Pluto for prototyping if the coding style is identical to the one I will be using in the normal scripts.
Thanks also to the other people for their answers.
Join us, we have cookies
We’re currently planning our next zoom meeting for the 22nd or the 29th over on the #astronomy slack channel
begin blocks do not really do much in a Julia script, notably they do not introduce a local scope (see Scope of Variables · The Julia Language ).
I confess that I was exaggerating a bit for Jupyter
But for new colleagues, my observation is that they are usually rather making too long code blocks in notebooks (> 20 LOC), which gets rather difficult to understand and maintain.
For having reproducible notebooks in Jupyter the only solution I am aware of is forcing the collaborators (and yourself) that a notebook must give identical results when restarting the kernel and running all cells (in notebook order). Reproducibility is a major advantage of Pluto.