Ipython Extension Module

Although acorn works great with compiled and externally authored modules, part of the beauty of the ipython experimentation process is that you can create functions and classes in the notebook for rapid prototyping. In order for acorn to be useful, there needs to be a way for these to be automatically decorated.

To solve this problem, acorn has a special ipython extension module that registers a callback for the post_run_cell event. After a cell is executed, acorn examines all the decoratable objects in the ipython user namespace and decorates any that haven’t already been decorated before. These objects show up with FQDNs like __main__.localfunction in the database log.

API Documentation

Methods for interacting with an ipython notebook or shell so that new functions defined interactively are also logged by acorn.

class acorn.ipython.AcornHistoryManager(existing, decorator)[source]

Sub-class for overriding the input storing during cell execution to allow acorn to detect loops and other problematic code before it executes. Necessary because the ipython hooks that we can register functions to don’t have access to the raw cell code that we need to parse.

Parameters:
  • existing (IPython.core.HistoryManager) – existing instance that possible has some history entries already. This instance’s __dict__ overwrites the new sub-class instance’s one.
  • decorator (InteractiveDecorator) – acorn ipython decorator for intercepting code execution and logging behavior.
store_inputs(line_num, source, source_raw=None)[source]

Store source and raw input in history and create input cache variables _i*.

Parameters:
  • line_num (int) – The prompt number of this input.
  • source (str) – Python input.
  • source_raw (str) – If given, this is the raw input without any IPython transformations applied to it. If not given, source is used.
class acorn.ipython.AcornJPEGFormatter(**kwargs)[source]

Replaces the default display formatter for the ipython notebooks that handles JPEG images. This allows thumbnails of plots to be generated as part of the notebook flow.

class acorn.ipython.AcornPNGFormatter(**kwargs)[source]

Replaces the default display formatter for the ipython notebooks that handles PNG images. This allows thumbnails of plots to be generated as part of the notebook flow.

class acorn.ipython.InteractiveDecorator(ip)[source]

Class for maintaining the state of functions declared interactively in the ipython notebook. Exposes callback functions for pre- and post- cell execution in ipython.

Parameters:ip (IPython.core.interactiveshell.InteractiveShell) – ipython shell instance for interacting with the shell variables.
shell

(IPython.core.interactiveshell.InteractiveShell): ipython shell instance for interacting with the shell variables.

atypes

list – names of types that are tracked for decoration by acorn.

entities

dict – keys are object types specified in atypes; values are dict of variable names and values in the local user namespace.

who

dict – keys are user variable names; values are the id() memory addresses. Used to keep track of variables whose values change between executions of cells in ipython.

pre

dict – pre-execution database entry that will be updated with results after execution has been performed by ipython.

cellids

dict – keys are original cell ids in the ipython notebook cell that was intercepted because it contained loops or other problematic code (from the viewpoint of the acorn database). Values are the latest version of the code. When a loop intercepted cell is run again, we search for a cell with most (at least 50%) overlap in contents and then treat that as a re-execute, after which the contents are overwritten by the most recent version.

cellid

int – id of the most recently loop-intercepted cell in the ipython notebook.

post_run_cell()[source]

Runs after the user-entered code in a cell has been executed. It detects any new, decoratable objects that haven’t been decorated yet and then decorates them.

pre_run_cell(cellno, code)[source]

Executes before the user-entered code in ipython is run. This intercepts loops and other problematic code that would produce lots of database entries and streamlines it to produce only a single entry.

Parameters:
  • cellno (int) – the cell number that is about to be executed.
  • code (str) – python source code that is about to be executed.
acorn.ipython.findloop(m)[source]

Determines if the specified member of _ast contains any for or while loops in its body definition.

acorn.ipython.inspectors = ['isclass', 'isfunction', 'ismethod', 'ismodule']

list – of functions in inspect that will be ignored by the automatic variable tracking after cell execution.

acorn.ipython.load_ipython_extension(ip)[source]

Loads the interacting decorator that ships with acorn into the ipython interactive shell.

Parameters:ip (IPython.core.interactiveshell.InteractiveShell) – ipython shell instance for interacting with the shell variables.
acorn.ipython.record_markdown(text, cellid)[source]

Records the specified markdown text to the acorn database.

Parameters:text (str) – the raw markdown text entered into the cell in the ipython notebook.
acorn.ipython.thumb_uuid = None

str – uuid of the image file that was most recently generated from a plot. The post-execution hook of the cell checks this variable to see if an image should be associated with the cell’s output.