[IPython-User] Python code in a markdown cell

Fernando Perez fperez.net@gmail....
Thu Apr 12 23:11:15 CDT 2012


On Mon, Apr 9, 2012 at 3:36 PM, Michael Hadmack <hadmack@hawaii.edu> wrote:
> Is there a way that you can execute python code from within a markdown cell?
>  For example I want to refer to the value a result from an earlier python
> cell from within my markdown text.  Presently I just copy and paste but I
> would like to do this dynamically.  Really what I suppose is needed is a new
> markdown tag that will pass the contents to IPython.display() via
> Javascript??.  Thanks.

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).

Now, the JS api doesn't expose direct retrieval of individual
variables, so pulling back variables from python to be rendered into
your markdown isn't currently possible.  We've thought a bit about
that, because the need is fairly obvious to write paragraphs with nice
references to previous computations.  But we don't have a clean
solution to the problem yet.

Cheers,

f


More information about the IPython-User mailing list