Skip to content

Instantly share code, notes, and snippets.

@parente parente/README.md
Last active Nov 4, 2018

Embed
What would you like to do?
Jupyter Tidbit: Display handles

Summary

IPython's display() function can return a DisplayHandle object. You can use a DisplayHandle to update the output of one cell from any other cell in a Jupyter Notebook.

Example

Binder

The display_handle.ipynb notebook below calls display(display_id=True) to get a display handle instance. It then uses the DisplayHandle.display method to show some initial, static Markdown. Later, in a different cell, it calls DisplayHandle.update in a loop to show a range of emoji characters.

Why is this useful?

You can use display handles to redraw matplotlib plots, re-render DataFrame tables, print log file updates, etc. from code executed anywhere in the notebook.

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import itertools\n",
"import time\n",
"\n",
"from IPython.display import Markdown"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make a display handle."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dh = display(display_id=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Show some Markdown (or plain text, or a plot, or ...)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(Markdown('# Display Update Fun'))\n",
"dh.display(Markdown(''))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Update the display above. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in itertools.cycle(range(0x1F600, 0x1F650)):\n",
" dh.update(Markdown(f'## &#{i}; {hex(i)}'))\n",
" time.sleep(0.5)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.