{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from bokeh.io import show, output_notebook, vplot\n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from bokeh.models import ColumnDataSource, DataRange1d, Plot, LinearAxis, Grid, Circle, HoverTool, BoxSelectTool\n",
    "from bokeh.models.widgets import 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": [
    "plot = Plot(title=None, x_range= DataRange1d(), y_range=DataRange1d(), plot_width=1000, plot_height=300)\n",
    "\n",
    "# Set up x & y axis\n",
    "plot.add_layout(LinearAxis(), 'below')\n",
    "yaxis = LinearAxis()\n",
    "plot.add_layout(yaxis, 'left')\n",
    "plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker))\n",
    "\n",
    "# Add Glyphs\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 = plot.add_glyph(source, cty_glyph)\n",
    "hwy = plot.add_glyph(source, hwy_glyph)\n",
    "\n",
    "# Add the tools\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(renderers=[cty], tooltips=tooltips + [(\"City MPG\", \"@cty\")])\n",
    "hwy_hover_tool = HoverTool(renderers=[hwy], tooltips=tooltips + [(\"Highway MPG\", \"@hwy\")])\n",
    "select_tool = BoxSelectTool(renderers=[cty, hwy], dimensions=['width'])\n",
    "plot.add_tools(cty_hover_tool, hwy_hover_tool, select_tool)\n",
    "\n",
    "show(vplot(plot, data_table))"
   ]
  }
 ],
 "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
