{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ip = get_ipython()\n",
    "\n",
    "from IPython.display import display\n",
    "\n",
    "def display_with_id(obj, display_id, update=False, execute_result=False):\n",
    "    iopub = ip.kernel.iopub_socket\n",
    "    session = get_ipython().kernel.session\n",
    "    data, md = ip.display_formatter.format(obj)\n",
    "    transient = {'display_id': str(display_id)}\n",
    "    content = {'data': data, 'metadata': md, 'transient': transient}\n",
    "    if execute_result:\n",
    "      msg_type = 'execute_result'\n",
    "      content['execution_count'] = ip.execution_count\n",
    "    else:\n",
    "      msg_type = 'update_display_data' if update else 'display_data'\n",
    "    session.send(iopub, msg_type, content, parent=ip.parent_header)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'above'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'below'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display('above')\n",
    "display_with_id(1, 'here')\n",
    "display('below')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_with_id(2, 'here')\n",
    "display_with_id(3, 'there')\n",
    "display_with_id(4, 'here')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_with_id(5, 'there')\n",
    "display_with_id(6, 'there', update=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "display_with_id(7, 'here')\n",
    "display_with_id(8, 'here', update=True)\n",
    "display_with_id(9, 'result', execute_result=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "display_with_id(10, 'result', update=True)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
