Show objects with display¶
IPython can display objects such as HTML, JSON, PNG, JPEG, SVG and Latex
Images¶
To display images (JPEG, PNG) in IPython and notebooks, you can use the Image class:
[1]:
from IPython.display import Image
Image("https://www.python.org/images/python-logo.gif")
[1]:
<IPython.core.display.Image object>
[2]:
from IPython.display import SVG
SVG(
"https://www.python.org/static/community_logos/python-logo-generic.svg",
)
[2]:
Non-embedded images¶
By default, image data is embedded:
Image ('img_url')However, if the
urlis given askwarg, this is interpreted as a soft link:Image (url='img_url')
embedcan also be specified explicitly:Image (url='img_url', embed = True)
HTML¶
Python objects can declare HTML representations to be displayed in a notebook:
[3]:
from IPython.display import HTML
[4]:
%%html
<ul>
<li>foo</li>
<li>bar</li>
</ul>
- foo
- bar
Javascript¶
With notebooks, objects can also declare a JavaScript representation. This enables for example data visualisations with Javascript libraries like d3.js.
[5]:
from IPython.display import Javascript
welcome = Javascript(
'alert("An example of a JavaScript warning displayed by IPython.")',
)
display(welcome)
For more extensive Javascript you can also use the %%javascript syntax.
LaTeX¶
IPython.display also has built-in support for displaying mathematical expressions set in LaTeX and rendered in the browser with MathJax:
[6]:
from IPython.display import Math
Math(r"F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx")
[6]:
With the Latex class you have to specify the limits yourself. In this way, however, you can also use other LaTeX modes, such as eqnarray:
[7]:
from IPython.display import 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}} \\ \end{eqnarray}""",
)
[7]:
Audio¶
IPython also enables interactive work with sounds. With the display.Audio class you can create an audio control that is embedded in the notebook. The interface is analogous to that of the Image class. All audio formats supported by the browser can be used.
[8]:
from IPython.display import Audio
In the following we will output the sine function of a NumPy array as an audio signal. The Audio class normalises and codes the data and embeds the resulting audio in the notebook.
[9]:
import numpy as np
f = 500.0
rate = 8000
L = 3
times = np.linspace(0, L, rate * L)
signal = np.sin(f * times)
Audio(data=signal, rate=rate)
[9]:
Links to local files¶
IPython has built-in classes for generating links to local files. To do this, create a link to a single file with the FileLink object:
[10]:
from IPython.display import FileLink, FileLinks
FileLink("magics.ipynb")
[10]:
Alternatively, you can generate a list with links to all files in a directory, e.g .:
[11]:
FileLinks("./mypackage/")
[11]:
Display notebooks¶
[12]:
from pathlib import Path
import nbformat
from IPython.display import display as ipydisplay
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import PythonLexer
formatter = HtmlFormatter()
lexer = PythonLexer()
# publish the CSS for pygments highlighting
ipydisplay(
HTML(
f"""
<style type='text/css'>
{formatter.get_style_defs()}
</style>
""",
),
)
[13]:
def show_notebook(fname):
"""Display a short summary of the cells of a notebook"""
nb = nbformat.read(fname, as_version=4)
html = []
for cell in nb.cells:
html.append(f"<h4>{cell.cell_type} cell</h4>")
if cell.cell_type == "code":
html.append(highlight(cell.source, lexer, formatter))
else:
html.append(f"<pre>{cell.source}</pre>")
ipydisplay(HTML("\n".join(html)))
show_notebook(Path("mypackage/foo.ipynb"))
markdown cell
# `foo.ipynb`
code cell
def bar():
return "bar"
code cell
def dirlist():
listing = !ls
return listing
code cell
def whatsmyname():
return __name__