🏠 back to Observable

Importing entire notebooks


#1

Hello good people!

Is it possible to import all the cells in a notebook without specifying them individually? I’m going to be doing a lot of similar stuff - so I’m trying to build a library of commonly used methods.

For example, I’d like replace the last cell in this this notebook with something like “import all…”.

Thanks!


#2

Not sure how to get all the way there, but if you’re using Chrome or another browser that supports dynamic imports, you can use this function:

async function importAll(id){
  const module = await import(`https://api.observablehq.com/${id}.js`);
  return `import {${
    module.default.modules[0].variables.filter(v => !v.from).filter(v => v.name).map(d => d.name).join(', ')
  }} from "${id}"`;
}

to automatically generate an import statement that you can then copy and put elsewhere.
For example, put the above function into its own cell and then

importAll('@ronzor/common')

in a different cell and you’ll get

`import {d3, make_svg_path_from_data, mod, MakeStandardAxes} from "@ronzor/common"` 

as output, which you can copy and paste into another cell.

(It’s in principle possible to create a version of importAll that works in Firefox by e.g. parsing the output of getSource in @mootari’s Version Pinning notebook, but I won’t try to do that now.)


#3

Thanks! It’s good to know that the solution might be browser dependent.