{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Bokeh example\n",
    "=============\n",
    "This example demonstrates the use of bokeh in the sphinx-nbgallery.\n",
    "\n",
    "The example is taken from the bokeh-notebook gallery to demonstrate the use\n",
    "bokeh. For the sphinx-nbgallery, you have to do 3 additional modifications\n",
    "in the `example_gallery_config` of your `conf.py`:\n",
    "\n",
    "1. You have to set the `insert_bokeh` configuration value because we need\n",
    "   additional style sheets and javascript files\n",
    "2. Note, that, since the  `output_notebook` function needs some time, we \n",
    "  recommend to use the `dont_preprocess` configuration value for this \n",
    "  notebook. \n",
    "3. We cannot estimate a thumbnail figure for a notebook not using matplotlib.\n",
    "   So you should provide it using the `thumbnail_figure` metadata key (as it\n",
    "   has been done for this notebook)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bokeh.models import HoverTool, ColumnDataSource\n",
    "from bokeh.palettes import Viridis6\n",
    "from bokeh.plotting import figure, show, output_notebook\n",
    "from bokeh.sampledata.us_counties import data as counties\n",
    "from bokeh.sampledata.unemployment import data as unemployment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "counties = {\n",
    "    code: county for code, county in counties.items() if county[\"state\"] == \"tx\"\n",
    "}\n",
    "\n",
    "county_xs = [county[\"lons\"] for county in counties.values()]\n",
    "county_ys = [county[\"lats\"] for county in counties.values()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "county_names = [county['name'] for county in counties.values()]\n",
    "county_rates = [unemployment[county_id] for county_id in counties]\n",
    "county_colors = [Viridis6[int(rate/3)] for rate in county_rates]\n",
    "\n",
    "source = ColumnDataSource(data=dict(\n",
    "    x=county_xs,\n",
    "    y=county_ys,\n",
    "    color=county_colors,\n",
    "    name=county_names,\n",
    "    rate=county_rates,\n",
    "))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "TOOLS=\"pan,wheel_zoom,box_zoom,reset,hover,save\"\n",
    "\n",
    "p = figure(title=\"Texas Unemployment 2009\", tools=TOOLS,\n",
    "           x_axis_location=None, y_axis_location=None)\n",
    "p.grid.grid_line_color = None\n",
    "\n",
    "p.patches('x', 'y', source=source,\n",
    "          fill_color='color', fill_alpha=0.7,\n",
    "          line_color=\"white\", line_width=0.5)\n",
    "\n",
    "hover = p.select_one(HoverTool)\n",
    "hover.point_policy = \"follow_mouse\"\n",
    "hover.tooltips = [\n",
    "    (\"Name\", \"@name\"),\n",
    "    (\"Unemployment rate)\", \"@rate%\"),\n",
    "    (\"(Long, Lat)\", \"($x, $y)\"),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "show(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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.5.0rc4"
  },
  "thumbnail_figure": "bokeh_example.png"
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
