Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically add cells to an ipython notebook for report generation

Tags:

I have seen a few of the talks by iPython developers about how to convert an ipython notebook to a blog post, a pdf, or even to an entire book(~min 43). The PDF-to-X converter interprets the iPython cells which are written in markdown or code and spits out a newly formatted document in one step.

My problem is that I would like to generate a large document where many of the figures and sections are programmatically generated - something like this. For this to work in iPython using the methods above, I would need to be able to write a function that would write other iPython-Code-Blocks. Does this capability exist?

#some pseudocode to give an idea for variable in list:     image = make_image(variable)     write_iPython_Markdown_Cell(variable)     write_iPython_Image_cell(image) 

I think this might be useful so I am wondering if:

  1. generating iPython Cells through iPython is possible
  2. if there is a reason that this is a bad idea and I should stick to a 'classic' solution like a templating library (Jinja).

thanks, zach cp

EDIT: As per Thomas' suggestion I posted on the ipython mailing list and got some feedback on the feasibility of this idea. In short - there are some technical difficulties that make this idea less than ideal for the original idea. For a repetitive report where you would like to generate markdown -cells and corresponding images/tables it is ore complicated to work through the ipython kernel/browser than to generate a report directly with a templating system like Jinja.

like image 759
zach Avatar asked Nov 28 '12 21:11

zach


People also ask

How do I add text to IPython notebook?

Simply Enter Esc and type m it will convert to text cell.

What does %% do in Jupyter Notebook?

Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.


2 Answers

There's a Notebook gist by Fernando Perez here that demonstrates how to programmatically create new cells. Note that you can also pass metadata in, so if you're generating a report and want to turn the notebook into a slideshow, you can easily indicate whether the cell should be a slide, sub-slide, fragment, etc.

You can add any kind of cell, so what you want is straightforward now (though it probably wasn't when the question was asked!). E.g., something like this (untested code) should work:

from IPython.nbformat import current as nbf  nb = nbf.new_notebook()  cells = []  for var in my_list:     # Assume make_image() saves an image to file and returns the filename     image_file = make_image(var)     text = "Variable: %s\n![image](%s)" % (var, image_file)     cell = nbf.new_text_cell('markdown', text)     cells.append(cell)  nb['worksheets'].append(nbf.new_worksheet(cells=cells))  with open('my_notebook.ipynb', 'w') as f:         nbf.write(nb, f, 'ipynb') 
like image 68
Tal Yarkoni Avatar answered Sep 20 '22 14:09

Tal Yarkoni


I won't judge whether it's a good idea, but if you call get_ipython().set_next_input(s) in the notebook, it will create a new cell with the string s. This is what IPython uses internally for its %load and %recall commands.

like image 30
Thomas K Avatar answered Sep 21 '22 14:09

Thomas K