{ "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": 1, "metadata": { "collapsed": false }, "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": 2, "metadata": { "collapsed": false }, "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": 3, "metadata": { "collapsed": false }, "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": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " Loading BokehJS ...\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function(global) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " var force = \"1\";\n", "\n", " if (typeof (window._bokeh_onload_callbacks) === \"undefined\" || force !== \"\") {\n", " window._bokeh_onload_callbacks = [];\n", " window._bokeh_is_loading = undefined;\n", " }\n", "\n", " function run_callbacks() {\n", " window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n", " delete window._bokeh_onload_callbacks\n", " console.info(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(js_urls, callback) {\n", " window._bokeh_onload_callbacks.push(callback);\n", " if (window._bokeh_is_loading > 0) {\n", " console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", " return null;\n", " }\n", " if (js_urls == null || js_urls.length === 0) {\n", " run_callbacks();\n", " return null;\n", " }\n", " console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " window._bokeh_is_loading = js_urls.length;\n", " for (var i = 0; i < js_urls.length; i++) {\n", " var url = js_urls[i];\n", " var s = document.createElement('script');\n", " s.src = url;\n", " s.async = false;\n", " s.onreadystatechange = s.onload = function() {\n", " window._bokeh_is_loading--;\n", " if (window._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: all BokehJS libraries loaded\");\n", " run_callbacks()\n", " }\n", " };\n", " s.onerror = function() {\n", " console.warn(\"failed to load library \" + url);\n", " };\n", " console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.getElementsByTagName(\"head\")[0].appendChild(s);\n", " }\n", " };\n", "\n", " var js_urls = ['https://cdn.pydata.org/bokeh/release/bokeh-0.12.1.min.js', 'https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.1.min.js', 'https://cdn.pydata.org/bokeh/release/bokeh-compiler-0.12.1.min.js'];\n", "\n", " var inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " \n", " function(Bokeh) {\n", " \n", " Bokeh.$(\"#a5332a96-33f5-40e7-9563-d16203f2a8ab\").text(\"BokehJS successfully loaded\");\n", " var kernel = Jupyter.notebook.kernel\n", " if (kernel.execute !== undefined) {\n", " kernel.execute(\"import bokeh.io; bokeh.io._nb_loaded = True\");\n", " }\n", " },\n", " function(Bokeh) {\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.1.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.1.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.1.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.1.min.css\");\n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " for (var i = 0; i < inline_js.length; i++) {\n", " inline_js[i](window.Bokeh);\n", " }\n", " }\n", "\n", " if (window._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", " run_inline_js();\n", " } else {\n", " load_libs(js_urls, function() {\n", " console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n", " run_inline_js();\n", " });\n", " }\n", "}(this));" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "output_notebook()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "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": 6, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "

<Bokeh Notebook handle for In[6]>

" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "show(p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "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.5.0rc4" }, "thumbnail_figure": "bokeh_example.png" }, "nbformat": 4, "nbformat_minor": 0 }