Custom IPython magic command for multiple cells

How to build a custom IPython magic command that:

  1. run a block of code before running a cell;
  2. only needs to be called once to be applied on every cell of notebook.

Similar cases
%matplotlib,
%pdb,
%doctest_mode

Example

In [1]: %myMagic 1
Out[1]: myMagic is: ON

In [2]: x = 1
Out[2]: 'Hello World'

In [3]: x
Out[3]: 'Hello World'
   ...:  1

In [4]: %myMagic 0
Out[4]: myMagic is: OFF

In [5]: y=x+1
   ...: y
Out[5]: 2

Source: Python Questions

LEAVE A COMMENT