Skip to content

Instantly share code, notes, and snippets.

@minrk
Last active November 10, 2016 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minrk/1a1e56a611f1ff1e2645f733bdd0e381 to your computer and use it in GitHub Desktop.
Save minrk/1a1e56a611f1ff1e2645f733bdd0e381 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"collapsed": true,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "ip = get_ipython()\nfrom IPython.display import display\niopub = ip.kernel.iopub_socket\nsession = get_ipython().kernel.session\n\n\ndef display_with_id(obj, display_id, update=False):\n \"\"\"Create a new display with an id\"\"\"\n data, md = ip.display_formatter.format(obj)\n transient = {'display_id': display_id}\n content = {\n 'data': data,\n 'metadata': md,\n 'transient': transient,\n }\n msg_type = 'update_display_data' if update else 'display_data'\n session.send(iopub, msg_type, content, parent=ip.parent_header)\n\ndef update_display(obj, display_id):\n \"\"\"Update existing displays with id\"\"\"\n return display_with_id(obj, display_id, update=True)\n\n",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "display_with_id('x', display_id='here')",
"execution_count": 8,
"outputs": [
{
"data": {
"text/plain": "'z'"
},
"output_type": "display_data",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "display_with_id('y', display_id='here')",
"execution_count": 9,
"outputs": [
{
"data": {
"text/plain": "'z'"
},
"output_type": "display_data",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "update_display('z', display_id='here')",
"execution_count": 11,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "An example ProgressBar using these messages:"
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "import os\nfrom binascii import hexlify\n\nclass ProgressBar(object):\n def __init__(self, capacity):\n self.progress = 0\n self.capacity = capacity\n self.width = 48\n self._display_id = hexlify(os.urandom(8)).decode('ascii')\n \n def __repr__(self):\n fraction = self.progress / self.capacity\n filled = '=' * int(fraction * self.width)\n rest = ' ' * (self.width - len(filled))\n return '[{}{}] {}/{}'.format(\n filled, rest,\n self.progress, self.capacity,\n )\n \n def display(self):\n display_with_id(self, display_id=self._display_id)\n \n def update(self):\n update_display(self, display_id=self._display_id)\n\nbar = ProgressBar(10)\nbar.display()",
"execution_count": 16,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "[================================================] 10/10"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Update the progress bar:"
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "import time\n\nfor i in range(11):\n bar.progress = i\n bar.update()\n time.sleep(0.25)",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"version": "3.5.2",
"nbconvert_exporter": "python",
"file_extension": ".py",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"name": "python",
"mimetype": "text/x-python",
"pygments_lexer": "ipython3"
},
"gist_id": "1a1e56a611f1ff1e2645f733bdd0e381"
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment