Skip to content

Instantly share code, notes, and snippets.

@ruoyu0088
Created May 27, 2017 22:34
Show Gist options
  • Save ruoyu0088/4e869781ac634081c1d3d438a043cd70 to your computer and use it in GitHub Desktop.
Save ruoyu0088/4e869781ac634081c1d3d438a043cd70 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div class=\"bk-root\">\n",
" <a href=\"http://bokeh.pydata.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
" <span id=\"dbbf71c6-5d4c-4ba8-9d1c-ac45751faca2\">Loading BokehJS ...</span>\n",
" </div>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"\n",
"(function(global) {\n",
" function now() {\n",
" return new Date();\n",
" }\n",
"\n",
" var force = true;\n",
"\n",
" if (typeof (window._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n",
" window._bokeh_onload_callbacks = [];\n",
" window._bokeh_is_loading = undefined;\n",
" }\n",
"\n",
"\n",
" \n",
" if (typeof (window._bokeh_timeout) === \"undefined\" || force === true) {\n",
" window._bokeh_timeout = Date.now() + 5000;\n",
" window._bokeh_failed_load = false;\n",
" }\n",
"\n",
" var NB_LOAD_WARNING = {'data': {'text/html':\n",
" \"<div style='background-color: #fdd'>\\n\"+\n",
" \"<p>\\n\"+\n",
" \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
" \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
" \"</p>\\n\"+\n",
" \"<ul>\\n\"+\n",
" \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
" \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
" \"</ul>\\n\"+\n",
" \"<code>\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"</code>\\n\"+\n",
" \"</div>\"}};\n",
"\n",
" function display_loaded() {\n",
" if (window.Bokeh !== undefined) {\n",
" var el = document.getElementById(\"dbbf71c6-5d4c-4ba8-9d1c-ac45751faca2\");\n",
" el.textContent = \"BokehJS \" + Bokeh.version + \" successfully loaded.\";\n",
" } else if (Date.now() < window._bokeh_timeout) {\n",
" setTimeout(display_loaded, 100)\n",
" }\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",
" };var element = document.getElementById(\"dbbf71c6-5d4c-4ba8-9d1c-ac45751faca2\");\n",
" if (element == null) {\n",
" console.log(\"Bokeh: ERROR: autoload.js configured with elementid 'dbbf71c6-5d4c-4ba8-9d1c-ac45751faca2' but no matching script tag was found. \")\n",
" return false;\n",
" }\n",
"\n",
" var js_urls = [\"./static/js/bokeh.min.js\", \"./static/js/bokeh-widgets.min.js\"];\n",
"\n",
" var inline_js = [\n",
" function(Bokeh) {\n",
" Bokeh.set_log_level(\"info\");\n",
" },\n",
" \n",
" function(Bokeh) {\n",
" \n",
" },\n",
" \n",
" function(Bokeh) {\n",
" \n",
" document.getElementById(\"dbbf71c6-5d4c-4ba8-9d1c-ac45751faca2\").textContent = \"BokehJS is loading...\";\n",
" },\n",
" function(Bokeh) {\n",
" console.log(\"Bokeh: injecting CSS: ./static/css/bokeh.min.css\");\n",
" Bokeh.embed.inject_css(\"./static/css/bokeh.min.css\");\n",
" console.log(\"Bokeh: injecting CSS: ./static/css/bokeh-widgets.min.css\");\n",
" Bokeh.embed.inject_css(\"./static/css/bokeh-widgets.min.css\");\n",
" }\n",
" ];\n",
"\n",
" function run_inline_js() {\n",
" \n",
" if ((window.Bokeh !== undefined) || (force === true)) {\n",
" for (var i = 0; i < inline_js.length; i++) {\n",
" inline_js[i](window.Bokeh);\n",
" }if (force === true) {\n",
" display_loaded();\n",
" }} else if (Date.now() < window._bokeh_timeout) {\n",
" setTimeout(run_inline_js, 100);\n",
" } else if (!window._bokeh_failed_load) {\n",
" console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
" window._bokeh_failed_load = true;\n",
" } else if (force !== true) {\n",
" var cell = $(document.getElementById(\"dbbf71c6-5d4c-4ba8-9d1c-ac45751faca2\")).parents('.cell').data().cell;\n",
" cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
" }\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": [
"from bokehelp import output_notebook\n",
"output_notebook()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"调用`IPython.display.display_javascript()`运行JavaScript代码之后,该段代码保留在单元的输出中,可以调用`clear_output()`清除单元的输出。这样就可以在控件的回调函数中反复运行JavaScript代码,而不会造成Notebook逐渐变得庞大。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def call_javascript(code, delay=None):\n",
" from IPython.display import display_javascript, clear_output \n",
" if delay is not None:\n",
" code = 'window.setTimeout(function(){{{code}}}, {delay:d});'.format(code=code, delay=delay)\n",
" display_javascript(code, raw=True)\n",
" clear_output()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import re\n",
"from bokeh.io import show\n",
"from bokeh.charts import Line, Bar, Area, BoxPlot\n",
"from bokeh.embed import components\n",
"import pandas as pd\n",
"import numpy as np\n",
"import ipywidgets as iw"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df = pd.DataFrame(np.random.randn(100, 3), columns=[\"A\", \"B\", \"C\"])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"fig_settings = dict(\n",
" id=\"myfig\",\n",
" plot_height=300\n",
")\n",
"\n",
"def plot_Line():\n",
" return Line(df.cumsum(axis=0), x=\"index\", **fig_settings)\n",
"\n",
"def plot_BoxPlot():\n",
" return BoxPlot(pd.melt(df), label=\"variable\", values=\"value\", **fig_settings)\n",
"\n",
"def plot_Bar():\n",
" return Bar(pd.melt(df), label=\"variable\", values=\"value\", legend=None, **fig_settings)\n",
" \n",
"def on_click(button):\n",
" name = button.description\n",
" func = globals()[\"plot_{}\".format(name)]\n",
" fig = func()\n",
" fig._id = \"myfig\"\n",
" script, html = components(fig)\n",
" script = re.search(r'<script type=\"text/javascript\">(.+?)</script>', script, flags=re.DOTALL | re.MULTILINE).group(1)\n",
" w_html.value = html\n",
" call_javascript(script)\n",
" \n",
"buttons = [iw.Button(description=label) for label in [\"Line\", \"BoxPlot\", \"Bar\"]]\n",
"for button in buttons:\n",
" button.on_click(on_click)\n",
"w_html = iw.HTML(layout=iw.Layout(height=\"300px\"))\n",
"iw.VBox([iw.HBox(buttons), w_html])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"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.2"
},
"widgets": {
"state": {
"23862b1e1a18447b8b2e0d9bdd661d71": {
"views": [
{
"cell_index": 5
}
]
}
},
"version": "1.2.0"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment