Skip to content

Instantly share code, notes, and snippets.

@maartenbreddels
Created October 10, 2017 13:56
Show Gist options
  • Save maartenbreddels/948f0ca6e7d355cd7249970aeeb2e66c to your computer and use it in GitHub Desktop.
Save maartenbreddels/948f0ca6e7d355cd7249970aeeb2e66c to your computer and use it in GitHub Desktop.
Using _view_count in ipywidget to lazily update an 'expensive' plot
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"import traitlets\n",
"import ipywidgets\n",
"import matplotlib.pylab as plt\n",
"import numpy as np\n",
"import IPython.display"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/plain": [
"<function IPython.core.display.clear_output>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"IPython.display.clear_output"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"class ExpensivePlot(traitlets.HasTraits):\n",
" gamma = traitlets.CFloat(default_value=2, help='Exponent for our expensive computation')\n",
" dirty = traitlets.CBool(default_value=True, help='Keep track if out plot is dirty (needs an update) or not')\n",
" def __init__(self, **kwargs):\n",
" super(ExpensivePlot, self).__init__(**kwargs)\n",
" # _view_count is None by default to avoid keeping track of the number of views\n",
" self.output_plot = ipywidgets.Output(_view_count=0)\n",
" self.output_log = ipywidgets.Output()\n",
" # if the number of views changes, or the plot is 'dirty'\n",
" # we need to check if we need to update the plot\n",
" self.output_plot.observe(self._check_plot_update, '_view_count')\n",
" self.observe(self._check_plot_update, 'dirty')\n",
"\n",
" # track if a parameter has changed, which will mark the plot as dirty\n",
" self.observe(self._parameter_changed, 'gamma')\n",
" \n",
" def _parameter_changed(self, change):\n",
" with self.output_log:\n",
" print('A parameter has changed')\n",
" print(change)\n",
" self.dirty = True\n",
" \n",
" def _check_plot_update(self, change):\n",
" if self.dirty and self.output_plot._view_count > 0:\n",
" with self.output_log:\n",
" print('it seems our plot is dirty, and is visible, we need to update')\n",
" self.update_expensive_plot()\n",
" else:\n",
" with self.output_log:\n",
" if self.dirty:\n",
" print('it seems our plot is dirty, but nothing is visible, so we postpone updating')\n",
" else:\n",
" print('nothing to update')\n",
"\n",
" def _ipython_display_(self):\n",
" # forward to the output for the plot\n",
" return self.output_plot._ipython_display_()\n",
"\n",
" def update_expensive_plot(self):\n",
" with self.output_log:\n",
" print('Updating an expensive plot')\n",
" # clear display, but want till the new output is available to avoid flickering\n",
" with self.output_plot:\n",
" IPython.display.clear_output(wait=True)\n",
" x = np.arange(10)\n",
" y = x**self.gamma\n",
" plt.plot(x, y)\n",
" plt.show()\n",
" self.dirty = False\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "209385d667704d2e9bb25a65d0d9a7cf",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>Output</code>.</p>\n",
"<p>\n",
" If you're reading this message in Jupyter Notebook or JupyterLab, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=https://ipywidgets.readthedocs.io/en/latest/user_install.html>Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another notebook frontend (e.g. a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plot = ExpensivePlot(gamma=1)\n",
"# print out the log here\n",
"plot.output_log"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"# change a parameter, plot will not be updated\n",
"plot.gamma = 0.2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"scrolled": true
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "959d08c51aaa48609db37849ec95f4f2",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>Output</code>.</p>\n",
"<p>\n",
" If you're reading this message in Jupyter Notebook or JupyterLab, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=https://ipywidgets.readthedocs.io/en/latest/user_install.html>Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another notebook frontend (e.g. a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# display the widget, now it's view count will change to 1, and the plot should update itself\n",
"plot"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"# We change the parameter, and the plot should update\n",
"plot.gamma = 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"# you could remove the widget above (for instance by commenting out the 'plot' statement and running the cell again)\n",
"# and then change the parameter again\n",
"# this will *not* update the plot\n",
"plot.gamma = 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"# if we display the widget again, it will update itself, since it is marked dirty\n",
"plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment