{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from bokeh.util.notebook import load_notebook\n",
    "load_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from bokeh.sampledata.autompg2 import autompg2 as mpg\n",
    "source = ColumnDataSource(mpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "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())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "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)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "xdr = DataRange1d()\n",
    "ydr = DataRange1d()\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])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "VBox(children=[plot, data_table]).html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
