[IPython-dev] running cells in notebook programmatically

Nathan Goldbaum nathan12343 at gmail.com
Tue Dec 9 14:22:47 EST 2014


You need to run the script under IPython, not the standard python
interpreter.  Under CPython, get_ipython() will return None.  Under
IPython, it returns an instance of TerminalInteractiveShell.

You might also be interested in the runipy project:
https://github.com/paulgb/runipy

On Tue Dec 09 2014 at 11:19:58 AM Robert Alexander <roalexan at microsoft.com>
wrote:

>  Hi. Running a script from the command line (using IPython 2.3.0)
> containing:
>
>
>
> from IPython.nbformat import current as nbf
>
> *from IPython import get_ipython*
>
>
>
> nb = nbf.new_notebook()
>
> firstCell = "print hello"
>
> cells = [ nbf.new_code_cell(firstCell) ]
>
> nb['worksheets'].append(nbf.new_worksheet(cells=cells))
>
>
>
> *ip = get_ipython()*
>
> print "ip"
>
> print ip
>
> for cell in nb.worksheets[0].cells:
>
>     if cell.cell_type != 'code':
>
>         continue
>
>     print "cell.input"
>
>     print cell.input
>
>     *ip.run_cell(cell.input)*
>
>
>
> returns the error:
>
>
>
> ip
>
> None
>
> cell.input
>
> print hello
>
> Traceback (most recent call last):
>
>   File "//.ipython/createnotebook.py", line 17, in <module>
>
>     ip.run_cell(cell.input)
>
> *AttributeError: 'NoneType' object has no attribute 'run_cell'*
>
>
>
> Is there a correct way to do this?
>  _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20141209/ef5d63b0/attachment.html>


More information about the IPython-dev mailing list