notebook namespace and imports

I wish there were a bit less separation between importing notebook vs. importing libraries. It feels like unnecessary friction / mental overhead both for newcomers and long-time users.

Right now, we can import multiple cells from a notebook into the current notebook, dumping arbitrarily many names into the notebook-global namespace from a special “import” cell. But these must all be top-level flat names, unless the imported notebook itself sticks them into a higher-level object.

By contrast, when importing an external library it is only possible to use a single top-level name, which becomes the content of the cell. To put more than one object from an external library into the notebook-global namespace requires one new cell per object.

People then e.g. making a whole pile of separate cells like:

sin = Math.sin
cos = Math.cos
sqrt = Math.sqrt

instead of what they want to do which is

{sin, cos, sqrt, pow, exp, log, PI, hypot} = Math

Some people do a workaround of repeating something like this above line in every non-trivial cell of their notebook.

Or as an alternative workaround we get pass-through notebooks like and, e.g.

import {abs, acos, arcosh, arsinh, asin, ... } from "@fil/math"

This workaround basically requires creation of an extra notebook every time anyone wants to get multiple names out of any library without needing a big heap of cells. But such notebooks don’t really do anything else except add network hops, indirection in code examination, etc.

In general it would be great if a cell could have the left-hand-side be something like

{a, b, c, d} = { /* some code that returns an object */}


[a, b, ...c] = { /* some code that returns an array */}

and then put all of the names on the left into the notebook-global namespace, track all of their dependencies (maybe internally they can be treated as separate dependent cells or something), and so on.

And it would be great if imports could put names into a namespace in case I want to import 15 or 20 objects from some other notebook but want to give them a common name relevant to where they came from and not pollute my global namespace.

Along similar lines, it would be nice to be able to use arbitrary javascript objects as cells when making an import.

For example,

import {chart} with {400 as height} from "@jrus/not_a_real_notebook"

Instead of needing to make a separate cell containing nothing but

chartheight = 400

so I can then do
For example,

import {chart} with {chartheight as height} from "@jrus/not_a_real_notebook"

It would also be nice to be able to use ES import syntax to import modules by their URL. You can currently use a dynamic import but that isn’t yet supported in all browsers that support ESM and you can’t pull off named or default imports from the module object.

1 Like