{
 "metadata": {
  "name": "",
  "signature": "sha256:d726c39daab37ef55a3a42c4fd3ac5ae9a581c3160c1164337ffbbca78472a25"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from bokeh import load_notebook\n",
      "load_notebook(force=True)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from bokeh.models import ColumnDataSource, DataRange1d, Plot, LinearAxis, Grid, GlyphRenderer, Circle, HoverTool, BoxSelectTool\n",
      "from bokeh.models.widgets import VBox, DataTable, TableColumn, StringFormatter, NumberFormatter, StringEditor, IntEditor, NumberEditor, SelectEditor"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from bokeh.sampledata.autompg2 import autompg2 as mpg\n",
      "source = ColumnDataSource(mpg)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "manufacturers = sorted(mpg[\"manufacturer\"].unique())\n",
      "models = sorted(mpg[\"model\"].unique())\n",
      "transmissions = sorted(mpg[\"trans\"].unique())\n",
      "drives = sorted(mpg[\"drv\"].unique())\n",
      "classes = sorted(mpg[\"class\"].unique())"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "columns = [\n",
      "    TableColumn(field=\"manufacturer\", title=\"Manufacturer\", editor=SelectEditor(options=manufacturers), formatter=StringFormatter(font_style=\"bold\")),\n",
      "    TableColumn(field=\"model\",        title=\"Model\",        editor=StringEditor(completions=models)),\n",
      "    TableColumn(field=\"displ\",        title=\"Displacement\", editor=NumberEditor(step=0.1),              formatter=NumberFormatter(format=\"0.0\")),\n",
      "    TableColumn(field=\"year\",         title=\"Year\",         editor=IntEditor()),\n",
      "    TableColumn(field=\"cyl\",          title=\"Cylinders\",    editor=IntEditor()),\n",
      "    TableColumn(field=\"trans\",        title=\"Transmission\", editor=SelectEditor(options=transmissions)),\n",
      "    TableColumn(field=\"drv\",          title=\"Drive\",        editor=SelectEditor(options=drives)),\n",
      "    TableColumn(field=\"class\",        title=\"Class\",        editor=SelectEditor(options=classes)),\n",
      "    TableColumn(field=\"cty\",          title=\"City MPG\",     editor=IntEditor()),\n",
      "    TableColumn(field=\"hwy\",          title=\"Highway MPG\",  editor=IntEditor()),\n",
      "]\n",
      "data_table = DataTable(source=source, columns=columns, editable=True)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "xdr = DataRange1d(sources=[source.columns(\"index\")])\n",
      "ydr = DataRange1d(sources=[source.columns(\"cty\"), source.columns(\"hwy\")])\n",
      "plot = Plot(title=None, x_range=xdr, y_range=ydr, plot_width=1000, plot_height=300)\n",
      "xaxis = LinearAxis(plot=plot)\n",
      "plot.below.append(xaxis)\n",
      "yaxis = LinearAxis(plot=plot)\n",
      "ygrid = Grid(plot=plot, dimension=1, ticker=yaxis.ticker)\n",
      "plot.left.append(yaxis)\n",
      "cty_glyph = Circle(x=\"index\", y=\"cty\", fill_color=\"#396285\", size=8, fill_alpha=0.5, line_alpha=0.5)\n",
      "hwy_glyph = Circle(x=\"index\", y=\"hwy\", fill_color=\"#CE603D\", size=8, fill_alpha=0.5, line_alpha=0.5)\n",
      "cty = GlyphRenderer(data_source=source, glyph=cty_glyph)\n",
      "hwy = GlyphRenderer(data_source=source, glyph=hwy_glyph)\n",
      "tooltips = [\n",
      "    (\"Manufacturer\", \"@manufacturer\"),\n",
      "    (\"Model\", \"@model\"),\n",
      "    (\"Displacement\", \"@displ\"),\n",
      "    (\"Year\", \"@year\"),\n",
      "    (\"Cylinders\", \"@cyl\"),\n",
      "    (\"Transmission\", \"@trans\"),\n",
      "    (\"Drive\", \"@drv\"),\n",
      "    (\"Class\", \"@class\"),\n",
      "]\n",
      "cty_hover_tool = HoverTool(plot=plot, renderers=[cty], tooltips=tooltips + [(\"City MPG\", \"@cty\")])\n",
      "hwy_hover_tool = HoverTool(plot=plot, renderers=[hwy], tooltips=tooltips + [(\"Highway MPG\", \"@hwy\")])\n",
      "select_tool = BoxSelectTool(plot=plot, renderers=[cty, hwy], dimensions=['width'])\n",
      "plot.tools.extend([cty_hover_tool, hwy_hover_tool, select_tool])\n",
      "plot.renderers.extend([cty, hwy, ygrid])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "VBox(children=[plot, data_table]).html"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}
