Last active
August 29, 2015 14:27
-
-
Save tonyfast/3061f9968936ea850072 to your computer and use it in GitHub Desktop.
Building Interactions between the IPython notebook and Bokeh Plots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# PyYAML and Bokeh\n", | |
| "\n", | |
| "Use [YAML Python tags](http://pyyaml.org/wiki/PyYAMLDocumentation) to create bokeh charts" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| " \n", | |
| " <link rel=\"stylesheet\" href=\"http://cdn.pydata.org/bokeh/release/bokeh-0.9.1.min.css\" type=\"text/css\" />\n", | |
| " <script type=\"text/javascript\" src=\"http://cdn.pydata.org/bokeh/release/bokeh-0.9.1.min.js\"></script>\n", | |
| " <script type=\"text/javascript\">\n", | |
| " Bokeh.set_log_level(\"info\");\n", | |
| " </script>\n", | |
| " <div>\n", | |
| " <a href=\"http://bokeh.pydata.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n", | |
| " <span>BokehJS successfully loaded.</span>\n", | |
| " </div>\n", | |
| " <p style=\"background-color: #f2d7dc;\">Warning: BokehJS previously loaded</p>" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "from bokeh.charts import output_notebook, show\n", | |
| "from bokeh.resources import CDN\n", | |
| "import yaml\n", | |
| "output_notebook( resources=CDN )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "> Construct Plot with data. Add an a" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "yaml_string = \"\"\"\n", | |
| "plot: !!python/object/apply:bokeh.charts.Line \n", | |
| " args:\n", | |
| " - y: [6, 7, 2, 4, 5]\n", | |
| " z: [1, 5, 12, 4, 2]\n", | |
| " kwds:\n", | |
| " title: simple line example \n", | |
| " xlabel: x\n", | |
| " ylabel: values\n", | |
| " width: 400\n", | |
| " height: 400\n", | |
| "\"\"\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "> Load YAML using basic types with ``BaseLoader``" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'plot': {'kwds': {'width': '400', 'title': 'simple line example', 'height': '400', 'ylabel': 'values', 'xlabel': 'x'}, 'args': [{'z': ['1', '5', '12', '4', '2'], 'y': ['6', '7', '2', '4', '5']}]}}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "yaml_data = yaml.load( yaml_string,Loader=yaml.BaseLoader )\n", | |
| "print(yaml_data)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "> Allow YAML to execute [pickle-able objects](http://pyyaml.org/wiki/PyYAMLDocumentation#LoadingYAML) to create a Bokeh plot" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<script type=\"text/javascript\">\n", | |
| " Bokeh.$(function() {\n", | |
| " var all_models = [{\"attributes\": {\"ticker\": {\"id\": \"64f1b04c-f4e4-4bc2-bcc2-756c24355135\", \"type\": \"BasicTicker\"}, \"tags\": [], \"id\": \"68baaa4b-2696-4af7-8ed1-91af1e321f94\", \"doc\": null, \"dimension\": 0, \"plot\": {\"subtype\": \"Chart\", \"type\": \"Plot\", \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\"}}, \"id\": \"68baaa4b-2696-4af7-8ed1-91af1e321f94\", \"type\": \"Grid\"}, {\"attributes\": {\"ticker\": {\"id\": \"b8f59ebb-e6fa-42cc-bdf4-c73c4c1dd6eb\", \"type\": \"BasicTicker\"}, \"tags\": [], \"id\": \"6621b7c9-507c-41cd-bb30-6a3fbb33361b\", \"doc\": null, \"dimension\": 1, \"plot\": {\"subtype\": \"Chart\", \"type\": \"Plot\", \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\"}}, \"id\": \"6621b7c9-507c-41cd-bb30-6a3fbb33361b\", \"type\": \"Grid\"}, {\"attributes\": {\"tags\": [], \"y\": {\"field\": \"y_z\"}, \"id\": \"1f89a628-739c-4607-bcc5-f177a48743a0\", \"doc\": null, \"line_color\": {\"value\": \"#f22c40\"}, \"x\": {\"field\": \"x\"}}, \"id\": \"1f89a628-739c-4607-bcc5-f177a48743a0\", \"type\": \"Line\"}, {\"attributes\": {\"data\": {\"y_y\": [6, 7, 2, 4, 5], \"x\": [0, 1, 2, 3, 4], \"y_z\": [1, 5, 12, 4, 2]}, \"doc\": null, \"id\": \"e2455653-9476-4bc9-be1f-972d4dcec2ac\", \"selected\": {\"0d\": {\"flag\": false, \"indices\": []}, \"2d\": {\"indices\": []}, \"1d\": {\"indices\": []}}, \"tags\": [], \"callback\": null, \"column_names\": [\"y_y\", \"x\", \"y_z\"]}, \"id\": \"e2455653-9476-4bc9-be1f-972d4dcec2ac\", \"type\": \"ColumnDataSource\"}, {\"attributes\": {\"tags\": [], \"end\": 13.1, \"id\": \"340b03fd-47e0-4a05-a391-4be11126e2c7\", \"doc\": null, \"callback\": null, \"start\": -0.10000000000000009}, \"id\": \"340b03fd-47e0-4a05-a391-4be11126e2c7\", \"type\": \"Range1d\"}, {\"attributes\": {\"below\": [{\"id\": \"e6c00bdd-be64-4e79-a0d0-0bd338a1ee99\", \"type\": \"LinearAxis\"}], \"tags\": [], \"tool_events\": {\"id\": \"09ef1b71-839e-4d25-a773-4d8edfb578db\", \"type\": \"ToolEvents\"}, \"y_range\": {\"id\": \"340b03fd-47e0-4a05-a391-4be11126e2c7\", \"type\": \"Range1d\"}, \"plot_width\": 400, \"plot_height\": 400, \"left\": [{\"id\": \"67140bf1-0d12-48ee-9e70-e3ce8ef1b65a\", \"type\": \"LinearAxis\"}], \"title\": \"simple line example\", \"tools\": [{\"id\": \"59bfaf14-09a4-46db-8266-7d9e57262e8d\", \"type\": \"PanTool\"}, {\"id\": \"4637a83e-e914-448d-aab4-7b8c55613c86\", \"type\": \"WheelZoomTool\"}, {\"id\": \"3c43929e-2c24-45d1-90ec-fb7a424bfb6b\", \"type\": \"BoxZoomTool\"}, {\"id\": \"d928a1a5-51dc-4c23-8f7d-43701db74aef\", \"type\": \"PreviewSaveTool\"}, {\"id\": \"eb80ab01-e46f-43de-9208-f0a5825cf751\", \"type\": \"ResizeTool\"}, {\"id\": \"c13077b9-cd3b-4ac2-b3e7-9b049958df5e\", \"type\": \"ResetTool\"}, {\"id\": \"b36b5372-f613-487f-8dcb-9ea23b7d2824\", \"type\": \"HelpTool\"}], \"above\": [], \"right\": [], \"doc\": null, \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\", \"extra_y_ranges\": {}, \"renderers\": [{\"id\": \"e6c00bdd-be64-4e79-a0d0-0bd338a1ee99\", \"type\": \"LinearAxis\"}, {\"id\": \"67140bf1-0d12-48ee-9e70-e3ce8ef1b65a\", \"type\": \"LinearAxis\"}, {\"id\": \"68baaa4b-2696-4af7-8ed1-91af1e321f94\", \"type\": \"Grid\"}, {\"id\": \"6621b7c9-507c-41cd-bb30-6a3fbb33361b\", \"type\": \"Grid\"}, {\"id\": \"c888ce3d-820f-48d4-a461-7ba967851fdf\", \"type\": \"GlyphRenderer\"}, {\"id\": \"3809e2b5-8e3a-406b-89eb-620f83a78154\", \"type\": \"GlyphRenderer\"}], \"extra_x_ranges\": {}, \"x_range\": {\"id\": \"d70d3c9b-200d-4bda-9afa-fa5865ba9fc1\", \"type\": \"DataRange1d\"}}, \"subtype\": \"Chart\", \"type\": \"Plot\", \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\"}, {\"attributes\": {\"tags\": [], \"y\": {\"field\": \"y_y\"}, \"id\": \"c90dad26-9ec4-407c-886d-517bb1f94ec7\", \"doc\": null, \"line_color\": {\"value\": \"#5ab738\"}, \"x\": {\"field\": \"x\"}}, \"id\": \"c90dad26-9ec4-407c-886d-517bb1f94ec7\", \"type\": \"Line\"}, {\"attributes\": {\"tags\": [], \"plot\": {\"subtype\": \"Chart\", \"type\": \"Plot\", \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\"}, \"id\": \"c13077b9-cd3b-4ac2-b3e7-9b049958df5e\", \"doc\": null}, \"id\": \"c13077b9-cd3b-4ac2-b3e7-9b049958df5e\", \"type\": \"ResetTool\"}, {\"attributes\": {\"tags\": [], \"id\": \"d70d3c9b-200d-4bda-9afa-fa5865ba9fc1\", \"doc\": null, \"renderers\": [], \"callback\": null, \"names\": []}, \"id\": \"d70d3c9b-200d-4bda-9afa-fa5865ba9fc1\", \"type\": \"DataRange1d\"}, {\"attributes\": {\"tags\": [], \"id\": \"3c43929e-2c24-45d1-90ec-fb7a424bfb6b\", \"dimensions\": [\"width\", \"height\"], \"doc\": null, \"plot\": {\"subtype\": \"Chart\", \"type\": \"Plot\", \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\"}}, \"id\": \"3c43929e-2c24-45d1-90ec-fb7a424bfb6b\", \"type\": \"BoxZoomTool\"}, {\"attributes\": {\"tags\": [], \"geometries\": [], \"id\": \"09ef1b71-839e-4d25-a773-4d8edfb578db\", \"doc\": null}, \"id\": \"09ef1b71-839e-4d25-a773-4d8edfb578db\", \"type\": \"ToolEvents\"}, {\"attributes\": {\"ticker\": {\"id\": \"64f1b04c-f4e4-4bc2-bcc2-756c24355135\", \"type\": \"BasicTicker\"}, \"tags\": [], \"formatter\": {\"id\": \"e0ba0a2d-e46d-46ba-9f41-54d5cf3d0f24\", \"type\": \"BasicTickFormatter\"}, \"doc\": null, \"id\": \"e6c00bdd-be64-4e79-a0d0-0bd338a1ee99\", \"axis_label\": \"x\", \"plot\": {\"subtype\": \"Chart\", \"type\": \"Plot\", \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\"}}, \"id\": \"e6c00bdd-be64-4e79-a0d0-0bd338a1ee99\", \"type\": \"LinearAxis\"}, {\"attributes\": {\"tags\": [], \"selection_glyph\": null, \"data_source\": {\"id\": \"e2455653-9476-4bc9-be1f-972d4dcec2ac\", \"type\": \"ColumnDataSource\"}, \"glyph\": {\"id\": \"1f89a628-739c-4607-bcc5-f177a48743a0\", \"type\": \"Line\"}, \"doc\": null, \"nonselection_glyph\": null, \"id\": \"c888ce3d-820f-48d4-a461-7ba967851fdf\"}, \"id\": \"c888ce3d-820f-48d4-a461-7ba967851fdf\", \"type\": \"GlyphRenderer\"}, {\"attributes\": {\"tags\": [], \"id\": \"e0ba0a2d-e46d-46ba-9f41-54d5cf3d0f24\", \"doc\": null}, \"id\": \"e0ba0a2d-e46d-46ba-9f41-54d5cf3d0f24\", \"type\": \"BasicTickFormatter\"}, {\"attributes\": {\"tags\": [], \"id\": \"bbf8367e-67ac-489c-8911-e82060bbea63\", \"doc\": null}, \"id\": \"bbf8367e-67ac-489c-8911-e82060bbea63\", \"type\": \"BasicTickFormatter\"}, {\"attributes\": {\"tags\": [], \"plot\": {\"subtype\": \"Chart\", \"type\": \"Plot\", \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\"}, \"id\": \"d928a1a5-51dc-4c23-8f7d-43701db74aef\", \"doc\": null}, \"id\": \"d928a1a5-51dc-4c23-8f7d-43701db74aef\", \"type\": \"PreviewSaveTool\"}, {\"attributes\": {\"mantissas\": [2, 5, 10], \"doc\": null, \"id\": \"64f1b04c-f4e4-4bc2-bcc2-756c24355135\", \"tags\": []}, \"id\": \"64f1b04c-f4e4-4bc2-bcc2-756c24355135\", \"type\": \"BasicTicker\"}, {\"attributes\": {\"tags\": [], \"id\": \"4637a83e-e914-448d-aab4-7b8c55613c86\", \"dimensions\": [\"width\", \"height\"], \"doc\": null, \"plot\": {\"subtype\": \"Chart\", \"type\": \"Plot\", \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\"}}, \"id\": \"4637a83e-e914-448d-aab4-7b8c55613c86\", \"type\": \"WheelZoomTool\"}, {\"attributes\": {\"tags\": [], \"plot\": {\"subtype\": \"Chart\", \"type\": \"Plot\", \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\"}, \"id\": \"b36b5372-f613-487f-8dcb-9ea23b7d2824\", \"doc\": null}, \"id\": \"b36b5372-f613-487f-8dcb-9ea23b7d2824\", \"type\": \"HelpTool\"}, {\"attributes\": {\"tags\": [], \"plot\": {\"subtype\": \"Chart\", \"type\": \"Plot\", \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\"}, \"id\": \"eb80ab01-e46f-43de-9208-f0a5825cf751\", \"doc\": null}, \"id\": \"eb80ab01-e46f-43de-9208-f0a5825cf751\", \"type\": \"ResizeTool\"}, {\"attributes\": {\"ticker\": {\"id\": \"b8f59ebb-e6fa-42cc-bdf4-c73c4c1dd6eb\", \"type\": \"BasicTicker\"}, \"tags\": [], \"formatter\": {\"id\": \"bbf8367e-67ac-489c-8911-e82060bbea63\", \"type\": \"BasicTickFormatter\"}, \"doc\": null, \"id\": \"67140bf1-0d12-48ee-9e70-e3ce8ef1b65a\", \"axis_label\": \"values\", \"plot\": {\"subtype\": \"Chart\", \"type\": \"Plot\", \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\"}}, \"id\": \"67140bf1-0d12-48ee-9e70-e3ce8ef1b65a\", \"type\": \"LinearAxis\"}, {\"attributes\": {\"mantissas\": [2, 5, 10], \"doc\": null, \"id\": \"b8f59ebb-e6fa-42cc-bdf4-c73c4c1dd6eb\", \"tags\": []}, \"id\": \"b8f59ebb-e6fa-42cc-bdf4-c73c4c1dd6eb\", \"type\": \"BasicTicker\"}, {\"attributes\": {\"tags\": [], \"id\": \"59bfaf14-09a4-46db-8266-7d9e57262e8d\", \"dimensions\": [\"width\", \"height\"], \"doc\": null, \"plot\": {\"subtype\": \"Chart\", \"type\": \"Plot\", \"id\": \"2f710596-f170-42de-880f-b961d7eab13f\"}}, \"id\": \"59bfaf14-09a4-46db-8266-7d9e57262e8d\", \"type\": \"PanTool\"}, {\"attributes\": {\"tags\": [], \"selection_glyph\": null, \"data_source\": {\"id\": \"e2455653-9476-4bc9-be1f-972d4dcec2ac\", \"type\": \"ColumnDataSource\"}, \"glyph\": {\"id\": \"c90dad26-9ec4-407c-886d-517bb1f94ec7\", \"type\": \"Line\"}, \"doc\": null, \"nonselection_glyph\": null, \"id\": \"3809e2b5-8e3a-406b-89eb-620f83a78154\"}, \"id\": \"3809e2b5-8e3a-406b-89eb-620f83a78154\", \"type\": \"GlyphRenderer\"}];\n", | |
| " Bokeh.load_models(all_models);\n", | |
| " var plots = [{'modeltype': 'Plot', 'elementid': '#54a78e29-7f58-4c00-9a90-3d8a719a6eeb', 'modelid': '2f710596-f170-42de-880f-b961d7eab13f'}];\n", | |
| " for (idx in plots) {\n", | |
| " \tvar plot = plots[idx];\n", | |
| " \tvar model = Bokeh.Collections(plot.modeltype).get(plot.modelid);\n", | |
| " \tBokeh.logger.info('Realizing plot:')\n", | |
| " \tBokeh.logger.info(' - modeltype: ' + plot.modeltype);\n", | |
| " \tBokeh.logger.info(' - modelid: ' + plot.modelid);\n", | |
| " \tBokeh.logger.info(' - elementid: ' + plot.elementid);\n", | |
| " \tvar view = new model.default_view({\n", | |
| " \t\tmodel: model,\n", | |
| " \t\tel: plot.elementid\n", | |
| " \t});\n", | |
| " \tBokeh.index[plot.modelid] = view;\n", | |
| " }\n", | |
| " });\n", | |
| " </script>\n", | |
| "<div class=\"plotdiv\" id=\"54a78e29-7f58-4c00-9a90-3d8a719a6eeb\"></div>\n", | |
| "\n" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "yaml_plot = yaml.load(yaml_string)\n", | |
| "\n", | |
| "show(yaml_plot['plot'])" | |
| ] | |
| } | |
| ], | |
| "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.4.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment