4

Within Jupyter, there's two options to export notebooks to other formats, using nbconvert either on the command-line or as a library. Both require you to pass the original notebook as input (either as a file name or JSON data).

What I would like to do is essentially automatic, self-contained report generation, i.e. use the nbconvert library within a notebook, to export that same notebook to HTML/PDF on execution. I envisage the export code in the final cell of the document, with instructions to ignore that last cell on export.

My problem is in getting the data. Within the notebook I can access the code cells through In[1], Out[2] etc, but AFAIK there is no such functionality for markdown cells. There's also no (official) method to grab the name of the notebook (no __file__ attribute) to pass information that way.

There's plenty of examples to grab all the cell data from other notebooks - but is there a way to access all the cell data from the current notebook?

blackgore
  • 127
  • 8

1 Answers1

1

You can do this with the notebook filename and nbconvert, as you surmise. Doing some stuff client side might require extensions, one I can envision is a server extension to do the convert, and a bit of JS you write in the last code cell which renders a button, for example, and posts the notebook back to the convert server extension. Hiding certain cells (say, the Js cell) you can do in the convert extension, or with cell metadata and nbconvert templates. Given the flexibility of the architecture, there are quite a few ways to do this

Tim P
  • 367
  • 2
  • 9
  • Thanks Tim. You're right, of course, I could use nbconvert with a passed filename. I guess I was trying it my way (wanting access to the cell data) so that I would have fuller control over, e.g. the cells that are written to the report, and the name and location of the report. AFAIK the nbconvert route would require the report document to have the same name and path as the notebook (and for the notebook to know its own name, something that might easily change as the notebook gets passed around). – blackgore Sep 05 '17 at 15:56
  • So my solution in that case would be to write some javascript to add metadata to the notebook json on the client (i.e. write a form for file location, which cells to save, etc), then send the notebook json to a custom server extension and parse the metadata, then hand that off to nbconvert using custom templates/preprocessing of the notebook json as necessary. – Tim P Sep 06 '17 at 03:16