{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import division\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "from bokeh.models import ColumnDataSource, CustomJS, Rect\n",
    "from bokeh.plotting import output_notebook, figure, show\n",
    "from bokeh.layouts import row\n",
    "\n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "N = 20\n",
    "img = np.empty((N,N), dtype=np.uint32)\n",
    "view = img.view(dtype=np.uint8).reshape((N, N, 4))\n",
    "for i in range(N):\n",
    "    for j in range(N):\n",
    "        view[i, j, 0] = int(i/N*255)\n",
    "        view[i, j, 1] = 158\n",
    "        view[i, j, 2] = int(j/N*255)\n",
    "        view[i, j, 3] = 255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "source = ColumnDataSource({'x':[], 'y':[], 'width':[], 'height':[]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "xrange_callback = CustomJS(args=dict(source=source), code=\"\"\"\n",
    "    var data = source.data;\n",
    "    var start = cb_obj.start;\n",
    "    var end = cb_obj.end;\n",
    "    data['x'] = [start + (end - start) / 2];\n",
    "    data['width'] = [end - start];\n",
    "    source.change.emit();\n",
    "\"\"\")\n",
    "\n",
    "yrange_callback = CustomJS(args=dict(source=source), code=\"\"\"\n",
    "    var data = source.data;\n",
    "    var start = cb_obj.start;\n",
    "    var end = cb_obj.end;\n",
    "    data['y'] = [start + (end - start) / 2];\n",
    "    data['height'] = [end - start];\n",
    "    source.change.emit();\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p1 = figure(title='Box Zoom Here', plot_width=400, plot_height=400,\n",
    "            x_range=(0,10), y_range=(0,10), tools ='box_zoom,wheel_zoom,pan,reset')\n",
    "p1.image_rgba(image=[img], x=[0], y=[0], dw=[10], dh=[10])\n",
    "p1.x_range.callback = xrange_callback\n",
    "p1.y_range.callback = yrange_callback\n",
    "\n",
    "p2 = figure(title='See Zoom Window Here', plot_width=400, plot_height=400, \n",
    "            x_range=(0,10), y_range=(0,10), tools=\"\")\n",
    "p2.image_rgba(image=[img], x=[0], y=[0], dw=[10], dh=[10])\n",
    "rect = Rect(x='x', y='y', width='width', height='height', fill_alpha=0, line_color='black')\n",
    "p2.add_glyph(source, rect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "show(row(p1, p2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
