'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:[1]
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 2:[2]
from IPython.display import display, Markdown
display(Markdown("# Hello World!"))
Solution 3:[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
Solution 4:[4]
If you accept Colab as 'jupyter notebook' , then the answer is YES.
Here some examples.
- Upper part, code cell
- Lower part, markdown cell
Code in the code cell:
#@markdown # Title
#@markdown **black**
#@markdown `code stuff`
#@markdown 
Very useful for when you want to insert an image within a code cell.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MSeifert |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | Rub |

