By: Amelia Saiko
Jupyter Sphinx¶
Jupyter Sphinx is a Sphinx extension that executes embedded code in a Jupyter kernel, and embeds outputs of that code in the document.
Using Jupyter Sphinx with MCL Theme¶
In order to use the Jupyter Sphinx extension, you must first install it:
pip install jupyter-sphinx
# or
uv add --group docs jupyter-sphinx
Then, you should activate it:
conf.py¶
extensions = [..., 'jupyter_sphinx']
More information is available on extension’s homepage.
Kitchen Sink¶
Text output¶
name = "Madoka"
surname = "Kaname"
print(f"Hello, {name} {surname}!!")
Hello, Madoka Kaname!!
Graphical output¶
import numpy as np
from matplotlib import pyplot
%matplotlib inline
x = np.linspace(0, 100)
pyplot.plot(x, x)
pyplot.plot(x, 100 - x)
pyplot.grid()
IPython output¶
LaTeX¶
from IPython.display import Latex
Latex(r'\int_{-\infty}^\infty e^{-x²}dx = \sqrt{\pi}')
\[\int_{-\infty}^\infty e^{-x²}dx = \sqrt{\pi}\]
Widgets¶
import ipywidgets as w
from IPython.display import display
a = w.IntSlider()
b = w.IntText()
w.jslink((a, 'value'), (b, 'value'))
display(a, b)
YouTube¶
from IPython.display import YouTubeVideo
YouTubeVideo("8ZP5eqm4JqM")
Code without output¶
name = "Madoka"
surname = "Kaname"
print(f"Hello, {name} {surname}!!")
Code formatting options¶
1name = "Madoka"
2surname = "Kaname"
3
4print(f"Hello, {name} {surname}!!")
Hello, Madoka Kaname!!
name = "Madoka"
surname = "Kaname"
print(f"Hello, {name} {surname}!!")
Hello, Madoka Kaname!!
Code below output¶
Hello, Madoka Kaname!!
name = "Madoka"
surname = "Kaname"
print(f"Hello, {name} {surname}!!")
Exceptions¶
print(1 / 0)
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[11], line 1
----> 1 print(1 / 0)
ZeroDivisionError: division by zero
Stderr¶
import sys
name = "Madoka"
surname = "Kaname"
print(f"Hello, {name} {surname}!!", file=sys.stderr)
Hello, Madoka Kaname!!
Manually constructed cells¶
name = "Madoka"
surname = "Kaname"
print(f"Hello, {name} {surname}!!")
Hello, Homura Akemi!!