Is it possible to import a notebook from a file?

Is there any way to create/update a notebook by importing from an external file?

The situation is this. I have a course textbook written in markdown that includes code blocks in R and Python. I can currently compile that into a Jupyter notebook or an RStudio document; I’d like to also be able to publish chapters to observable with js code blocks turned into cells, and markdown or HTML rendered as is.

In my ideal world I’d be depositing some kind of JSON manifest of cells, much like an .ipynb file, into a git repo that would instantly be the live version of the observable notebook. I anticipate the editing flow would be one-way, with observablehq downstream from the editing.

Is anything like this possible? Searching for ‘observablehq import’ is hard because swamped by other results.

3 Likes

Like

or

Thanks, hadn’t seen those! That’s great, but allows embedding HTML inside a single cell;
I’d like to automatically populate a whole bunch of cells, some of which are HTML/Markdown and some of which may be javascript.

E.g., I’d be starting with a markdown file like this:

# Sliders

To make a slider, first define an output cell

'''js
2 + x
'''

and then use inputs

'''
viewof x = Inputs.range([0, 100], {step: 10})
'''

That would somehow fill in all four cells in a notebook like this one.

For Jupyter, I can do that by writing the markdown to JSON. I’m wondering if Observable notebooks have any similar backing representation that I can write to.

Even though a notebook can be downloaded as a module I don’t know of a way for observable to import the module again.

It could be done by coding a puppeteer script, but that would be a lot of work.

Not sure if prerender would help.

other notebook that might help, Accessing a Notebook's Runtime / Fabian Iwand / Observable

It’s not currently possible, but there are open feature requests, e.g.:

That said, if you log into Observable via Github and have 2FA disabled, you might be able to make it work with GitHub - mootari/observable-client and some custom code that connects to the Websocket and sends the necessary update messages. I don’t recommend it though, and the client is probably broken by now. In any case, see this topic for a lengthy discussion.

You can’t modify editor contents this way. The runtime lives in the sandboxed iframe and its only task is to run your notebook code. :slight_smile:

Thanks so much @hellonearthis and @mootari. Prerender looks like it’s close to having an API I’d imagine be happy to use for cell by cell editing, but without authentication seems as though the publishing stage would be both difficult and fragile. (Especially because I don’t want to disable 2FA.)

Would love to see this, though, because about 99% of an observable notebooks content’s could be represented in a form that looks either like ipython json or R Markdown.

I’ll +1 some of those feature requests on github.