[IPython-User] Python code in a markdown cell

Fernando Perez fperez.net@gmail....
Fri Apr 13 02:27:59 CDT 2012


On Thu, Apr 12, 2012 at 9:11 PM, Fernando Perez <fperez.net@gmail.com> wrote:
> You actually can execute Python code in the kernel, try the following
> in a markdown cell:
>
> <script type="text/javascript">
>    IPython.notebook.kernel.execute("x=99");
> </script>
>
> then print x in your next python cell, and you'll see 99 (also note
> how your prompt counter will have gone up by one).

BTW, I forgot to note that you should *NOT* put the above tricks into
any notebook that you actually want to save, because it actually
breaks reloading of that same notebook later on.  As Brian mentioned,
there are subtle issues with the JS trying to access the IPython
objects in random places, and in this case having a markdown cell that
tries to use the IPython object will prevent the file from opening
cleanly later on (since at web load time, the object isn't fully
constructed yet).

f


More information about the IPython-User mailing list