Skip to content

Allow multiple namespaces inside one notebook #1796

@cocoa1231

Description

@cocoa1231

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

  1. Read in some data / create some initial data
  2. Do some processingPlot some intermediate results
  3. Do some more processing
  4. Plot final results
  5. Do processing again in a different way (different data, different method, etc.)
  6. Plot intermediate results again
  7. Do some more processing
  8. Plot final results for second version
  9. Compare both versions (often by plotting)

So what’s the problem?

First, I have multiple plots and because plots invariably require variables like an ax = Axis(...) or fig = Figure(...). I either have to give each axis or figure variable a new name like ax_for_intermediate_results_version1 or I have to wrap the whole plotting expression in a let ... 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 and bar to foo2 and bar2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions