How To Have Python Code And Markdown In One Cell
Can jupyter notebook support inline python code (arthritic calculations, or plot a figure) in markdown cell, or verse visa. Have both python code and markdown in one cell.
Solution 1:
from IPython.display import display, Markdown
display(Markdown("# Hello World!"))
Solution 2:
I don't think that's possible but there is an extension that you could use: https://github.com/ipython-contrib/IPython-notebook-extensions/wiki/python-markdown
That way you can display the result (and only the result) of a python statement inside a markdown cell.
Solution 3:
You can use the magic function below
%%md_with_code
"""
# Title
More markdown
"""
my_code.execute()
Here is the code for the magic function
from IPython.display import display, Markdown
from IPython.core.magic import register_cell_magic
from IPython import get_ipython
import ast
@register_cell_magic
def md_with_code(line, cell):
parsed = ast.parse(cell)
if not parsed:
return
if isinstance(parsed.body[0], ast.Expr) and isinstance(parsed.body[0].value, ast.Str):
display(Markdown(parsed.body[0].value.s.strip()))
get_ipython().run_cell(cell)
del md_with_code
del md_with_code
Post a Comment for "How To Have Python Code And Markdown In One Cell"