Allow multiple namespaces inside one notebook #1799
Replies: 1 comment 3 replies
-
Generally, I quite like that Pluto doesn't allow this. Since code can become very complex very quickly, some things are generally advised against. Re-using variable names is one such a thing. The reason is that once variables can contain different things at different points, it becomes very hard for a reader of the code to track down what is inside the variable at a given point in time. The most capable solution, in my opinion, is to write proper functions.
It is a fair point that code inside functions is sort of locked away and that you cannot easily access the insides. However, a better way to think about functions is as building blocks. When writing code, when a proper function is created which hits the right abstraction, the function is created once and only when bugs occur the inside needs to be inspected. Then, functions are not about "locking away" things, but functions are about "hiding unnecessary details". |
Beta Was this translation helpful? Give feedback.
-
This issue was raised on Discourse, here's the original link -
https://discourse.julialang.org/t/notebooks-need-modules-i-e-multiple-separate-global-namespaces/68541
My main issue is that I may have multiple variables with the same name that I want to redefine as I go along using the notebook. For instance, I'm reading a CSV with a column of current values, and I have two or three such CSVs all analysed in different ways in the same project (notebook), so I would like something like a chapter/module option so that I can import data from the CSV into a variable named current multiple times. A more detailed case is given in the link by Discourse user Luapulu, here's it copy pasted for convenience -
For context, a typical workflow for me, using a Pluto or Jupyter notebook, looks like this
So what’s the problem?
First, I have multiple plots and because plots invariably require variables like an
ax = Axis(...)
orfig = Figure(...)
. I either have to give each axis or figure variable a new name likeax_for_intermediate_results_version1
or I have to wrap the whole plotting expression in alet ... end
block, which is my current solution. This solution, however, doesn’t allow for any kind of interactive plotting. The whole plot has to be defined in one block. Adding elements piece by piece is not possible. That’s sad because, for me, the whole point of a notebook is to have a more interactive coding experience, which includes the plotting.Second, since notebooks are great for experimentation, I often implement things in a few different ways and compare the results. Or I process different data using similar methods. In any case, I often have repeated code. And again, I run into issues with namespaces. When I write the second implementation I have to make sure I change every
foo
andbar
tofoo2
andbar2
. Pluto helps quite a bit here because it won’t let you assign to the same variable twice. But still, this process of appending some kind of number or letter sequence to every variable is the cause of a great number of bugs in my notebooks and is quite tedious. You might argue that I should just write more reusable code. Put everything in functions and you’ve solve the problem, right? Yes, you have, but if you can do that, why even use a notebook? The thing that makes notebooks work, is that you have a lot of stuff in global scope that you can freely play with and inspect. Reusable code is necessarily less interactive.What’s my proposed solution?
Implement a chapter/module feature using modules. Allow me to put multiple cells into one module. This way, both versions of my implementation get their own global scope and everything remains interactive because everything is still global and I can easily use code from other modules (or chapters) via the import/using syntax.
Every plot can live in its own little module where I can freely create the plot across multiple cells. I avoid naming conflicts and I have even better control of what I do and don’t import from the surrounding scope, than I would with a
let ... end
block.Beta Was this translation helpful? Give feedback.
All reactions