iPython display examples¶

Some JupyterLite inspired examples (see python.ipynb) of using iPython display.

In [ ]:
from IPython.display import display, HTML, JSON, Markdown, Math

Single elements¶

Individual elements can be rendered without display()

In [ ]:
HTML("<h3>HTML Title</h3>")
Out[ ]:

HTML Title

In [ ]:
Markdown("### Markdown title")
Out[ ]:

Markdown title¶

In [ ]:
Math("a^2 + b^2 = c^2")
Out[ ]:
$\displaystyle a^2 + b^2 = c^2$
In [ ]:
JSON(["foo", {"bar": ("baz", None, 1.0, 2)}], expanded=True)
Out[ ]:
<IPython.core.display.JSON object>

Multiple elements¶

Use display to show multipe elements from a single cell.

In [ ]:
display(HTML("<em>HTML element</em>"))
display(Markdown("__Markdown title__"))
display(Math("a^2 + b^2 = c^2"))
HTML element

Markdown title

$\displaystyle a^2 + b^2 = c^2$
In [ ]:
print("'first element'")
display("second element")
print("'third element'")
'first element'
'second element'
'third element'

This does not work without display()

In [ ]:
HTML("first HTML element <em>not shown</em>")
Markdown("only last Markdown element _shown_")
Out[ ]:

only last Markdown element shown

JupyterLite examples¶

The JupyterLite example python.ipynb made me aware of iPython display.

Here are some of these examples. They are modified to get a nice table of content in Jupyter.

In [ ]:
from IPython.display import Markdown, HTML, JSON, Latex

HTML¶

In [ ]:
print("Before display")

s = "<h3>HTML Title</h3>"
display(HTML(s))

print("After display")
Before display

HTML Title

After display
In [ ]:
HTML(
    """
        <div style="background: aliceblue; width: 200px; height: 100px; border-radius: 10px;">
        </div>"""
)
Out[ ]:

Markdown¶

In [ ]:
Markdown(
    """
### Title

**in bold**

~~Strikethrough~~
"""
)
Out[ ]:

Title¶

in bold

Strikethrough

Pandas DataFrame¶

In [ ]:
import pandas as pd
import numpy as np
from string import ascii_uppercase as letters
from IPython.display import display

df = pd.DataFrame(
    np.random.randint(0, 100, size=(100, len(letters))), columns=list(letters)
)
df
Out[ ]:
A B C D E F G H I J ... Q R S T U V W X Y Z
0 52 27 29 4 82 95 38 45 4 90 ... 77 69 3 37 57 66 73 92 77 56
1 79 5 88 64 48 21 54 62 4 26 ... 4 33 77 73 4 39 87 36 93 97
2 66 27 67 34 70 81 44 9 81 65 ... 54 87 82 94 11 11 89 42 98 6
3 24 33 89 10 81 15 73 39 97 67 ... 53 93 11 73 65 50 82 67 80 95
4 68 30 16 48 58 72 89 36 5 50 ... 75 92 64 3 46 33 84 47 60 11
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
95 79 69 28 24 56 66 5 62 11 91 ... 74 39 93 28 29 39 96 27 99 82
96 36 93 41 48 60 29 82 10 11 27 ... 84 67 58 93 32 77 57 98 92 77
97 59 22 78 0 88 59 33 98 29 73 ... 43 18 78 37 77 34 74 12 18 15
98 70 9 96 31 16 15 41 65 69 40 ... 63 39 27 78 6 36 85 47 66 81
99 32 87 3 93 39 84 17 26 12 61 ... 19 11 69 85 34 32 21 68 75 56

100 rows × 26 columns

Math¶

In [ ]:
from IPython.display import Math

Math(r"F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx")
Out[ ]:
$\displaystyle F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$

Latex¶

In [ ]:
Latex(
    r"""\begin{eqnarray}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{eqnarray}"""
)
Out[ ]:
\begin{eqnarray} \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{eqnarray}

JSON¶

In [ ]:
JSON(["foo", {"bar": ("baz", None, 1.0, 2)}], metadata={}, expanded=True, root="test")
Out[ ]:
<IPython.core.display.JSON object>

Network requests and JSON¶

Here, I use requests instead of the JupyterLite version with js.

In [ ]:
%pip install -q requests
Note: you may need to restart the kernel to use updated packages.
In [ ]:
import requests

r = requests.get("https://httpbin.org/get")
r.text
Out[ ]:
'{\n  "args": {}, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate", \n    "Host": "httpbin.org", \n    "User-Agent": "python-requests/2.31.0", \n    "X-Amzn-Trace-Id": "Root=1-65626017-34b8b75d061503f21747f6c4"\n  }, \n  "origin": "79.241.77.212", \n  "url": "https://httpbin.org/get"\n}\n'
In [ ]:
JSON(r.json())
Out[ ]:
<IPython.core.display.JSON object>