from IPython.display import HTML, JSON, Markdown, Math, display
iPython display examples
Some JupyterLite inspired examples (see python.ipynb
) of using iPython display.
Single elements
Individual elements can be rendered without display()
"<h3>HTML Title</h3>") HTML(
HTML Title
"### Markdown title") Markdown(
Markdown title
"a^2 + b^2 = c^2") Math(
\(\displaystyle a^2 + b^2 = c^2\)
"foo", {"bar": ("baz", None, 1.0, 2)}], expanded=True) JSON([
<IPython.core.display.JSON object>
Multiple elements
Use display to show multipe elements from a single cell.
"<em>HTML element</em>"))
display(HTML("__Markdown title__"))
display(Markdown("a^2 + b^2 = c^2")) display(Math(
HTML element
Markdown title
\(\displaystyle a^2 + b^2 = c^2\)
print("'first element'")
"second element")
display(print("'third element'")
'first element'
'second element'
'third element'
This does not work without display()
"first HTML element <em>not shown</em>")
HTML("only last Markdown element _shown_") Markdown(
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.
from IPython.display import HTML, JSON, Latex, Markdown
HTML
print("Before display")
= "<h3>HTML Title</h3>"
s
display(HTML(s))
print("After display")
Before display
HTML Title
After display
HTML("""
<div style="background: aliceblue; width: 200px; height: 100px; border-radius: 10px;">
</div>
""" # noqa: E501
)
Markdown
Markdown("""
### Title
**in bold**
~~Strikethrough~~
"""
)
Title
in bold
Strikethrough
Pandas DataFrame
from string import ascii_uppercase as letters
import numpy as np
import pandas as pd
from IPython.display import display
= pd.DataFrame(
df 0, 100, size=(100, len(letters))), columns=list(letters)
np.random.randint(
) df
A | B | C | D | E | F | G | H | I | J | ... | Q | R | S | T | U | V | W | X | Y | Z | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 43 | 27 | 22 | 30 | 11 | 64 | 63 | 50 | 90 | 53 | ... | 2 | 38 | 30 | 53 | 50 | 93 | 43 | 44 | 20 | 28 |
1 | 79 | 21 | 73 | 33 | 14 | 84 | 79 | 21 | 77 | 37 | ... | 93 | 61 | 32 | 23 | 38 | 17 | 10 | 2 | 89 | 23 |
2 | 60 | 89 | 4 | 35 | 13 | 53 | 64 | 12 | 13 | 66 | ... | 25 | 6 | 53 | 6 | 60 | 19 | 98 | 65 | 73 | 89 |
3 | 41 | 64 | 96 | 28 | 25 | 8 | 26 | 65 | 32 | 30 | ... | 4 | 27 | 53 | 53 | 11 | 95 | 10 | 97 | 88 | 19 |
4 | 15 | 93 | 72 | 37 | 17 | 94 | 16 | 22 | 34 | 77 | ... | 22 | 45 | 22 | 0 | 35 | 47 | 15 | 26 | 53 | 38 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
95 | 96 | 84 | 86 | 54 | 51 | 75 | 39 | 31 | 78 | 66 | ... | 92 | 97 | 72 | 58 | 82 | 51 | 22 | 51 | 55 | 13 |
96 | 56 | 54 | 86 | 21 | 86 | 16 | 65 | 56 | 96 | 62 | ... | 19 | 8 | 29 | 58 | 11 | 28 | 58 | 55 | 81 | 0 |
97 | 62 | 7 | 94 | 1 | 1 | 73 | 83 | 82 | 42 | 63 | ... | 54 | 59 | 14 | 92 | 42 | 54 | 71 | 33 | 15 | 7 |
98 | 86 | 88 | 62 | 58 | 60 | 83 | 79 | 26 | 32 | 68 | ... | 44 | 49 | 8 | 8 | 80 | 26 | 34 | 59 | 86 | 14 |
99 | 91 | 65 | 43 | 30 | 51 | 40 | 97 | 81 | 35 | 76 | ... | 37 | 2 | 15 | 24 | 76 | 99 | 42 | 84 | 87 | 5 |
100 rows × 26 columns
Math
from IPython.display import Math
r"F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx") Math(
\(\displaystyle F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx\)
Latex
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}
""" # noqa: E501
)
<IPython.core.display.Latex object>
JSON
"foo", {"bar": ("baz", None, 1.0, 2)}], metadata={}, expanded=True, root="test") JSON([
<IPython.core.display.JSON object>
Network requests and JSON
Here, I use requests instead of the JupyterLite version with js.
%pip install -q requests
/Users/hd/hd/learning/learning-diary/.venv/bin/python: No module named pip
Note: you may need to restart the kernel to use updated packages.
import requests
= requests.get("https://httpbin.org/get")
r r.text
'{\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-65fab6bc-1087d67f265788bf1f4c3a6d"\n }, \n "origin": "79.212.250.177", \n "url": "https://httpbin.org/get"\n}\n'
JSON(r.json())
<IPython.core.display.JSON object>