-
-
Save bollwyvl/8388c7a9425e237ff48d to your computer and use it in GitHub Desktop.
Use yaml to parse and also to create a bokeh chart
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": [ | |
| "> Based on [this](http://nbviewer.ipython.org/gist/tonyfast/db6830cd9e68aa14dd8e/bokeh_yaml.ipynb)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from bokeh.charts import output_notebook, show\n", | |
| "from bokeh import charts\n", | |
| "from bokeh.resources import CDN\n", | |
| "\n", | |
| "import yaml\n", | |
| "from yaml import SafeLoader, Loader, BaseLoader" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| " \n", | |
| " <link rel=\"stylesheet\" href=\"http://cdn.pydata.org/bokeh/release/bokeh-0.9.2.min.css\" type=\"text/css\" />\n", | |
| " <script type=\"text/javascript\" src=\"http://cdn.pydata.org/bokeh/release/bokeh-0.9.2.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": [ | |
| "output_notebook(resources=CDN)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "%reload_ext yamlmagic" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "class BokehLoader(SafeLoader):\n", | |
| " pass" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def line_constructor(loader, node):\n", | |
| " bits = loader.construct_mapping(node, deep=True)\n", | |
| " return charts.Line(**bits)\n", | |
| "\n", | |
| "BokehLoader.add_constructor(\"!line\", line_constructor)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "application/javascript": [ | |
| "\n", | |
| " require(\n", | |
| " [\n", | |
| " \"notebook/js/codecell\",\n", | |
| " \"codemirror/mode/yaml/yaml\"\n", | |
| " ],\n", | |
| " function(cc){\n", | |
| " cc.CodeCell.options_default.highlight_modes.magic_yaml = {\n", | |
| " reg: [\"^%%yaml\"]\n", | |
| " }\n", | |
| " }\n", | |
| " );\n", | |
| " " | |
| ], | |
| "text/plain": [ | |
| "<IPython.core.display.Javascript object>" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "%%yaml plot -lBokehLoader\n", | |
| "!line\n", | |
| " values:\n", | |
| " y: [6, 7, 2, 4, 5]\n", | |
| " z: [1, 5, 12, 4, 2]\n", | |
| " title: simple line example \n", | |
| " xlabel: x\n", | |
| " ylabel: values\n", | |
| " width: 400\n", | |
| " height: 400" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 14, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<script type=\"text/javascript\">\n", | |
| " Bokeh.$(function() {\n", | |
| " var all_models = [{\"type\": \"HelpTool\", \"attributes\": {\"plot\": {\"type\": \"Plot\", \"subtype\": \"Chart\", \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\"}, \"doc\": null, \"tags\": [], \"id\": \"f29519c5-aadc-488f-b27e-12f24fa0a544\"}, \"id\": \"f29519c5-aadc-488f-b27e-12f24fa0a544\"}, {\"type\": \"Line\", \"attributes\": {\"y\": {\"field\": \"y_y\"}, \"line_color\": {\"value\": \"#f22c40\"}, \"id\": \"e1e4a776-e944-4f5b-8073-e4d924ab90a8\", \"doc\": null, \"x\": {\"field\": \"x\"}, \"tags\": []}, \"id\": \"e1e4a776-e944-4f5b-8073-e4d924ab90a8\"}, {\"type\": \"LinearAxis\", \"attributes\": {\"axis_label\": \"values\", \"formatter\": {\"type\": \"BasicTickFormatter\", \"id\": \"97dbe69e-d261-4670-b2fa-6ef51581d286\"}, \"id\": \"c9dde829-b217-476a-b46e-aa5b9adca4df\", \"ticker\": {\"type\": \"BasicTicker\", \"id\": \"b4a6973b-725e-4ab0-a414-43a9d132702c\"}, \"plot\": {\"type\": \"Plot\", \"subtype\": \"Chart\", \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\"}, \"doc\": null, \"tags\": []}, \"id\": \"c9dde829-b217-476a-b46e-aa5b9adca4df\"}, {\"type\": \"LinearAxis\", \"attributes\": {\"axis_label\": \"x\", \"formatter\": {\"type\": \"BasicTickFormatter\", \"id\": \"288bdf15-0f54-410c-9477-b93435ba84e7\"}, \"id\": \"2423367e-e9d3-42ba-85a7-9b3f4d50dc4a\", \"ticker\": {\"type\": \"BasicTicker\", \"id\": \"1fdfccaf-829e-4d73-b142-adb6c542d9b8\"}, \"plot\": {\"type\": \"Plot\", \"subtype\": \"Chart\", \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\"}, \"doc\": null, \"tags\": []}, \"id\": \"2423367e-e9d3-42ba-85a7-9b3f4d50dc4a\"}, {\"type\": \"PreviewSaveTool\", \"attributes\": {\"plot\": {\"type\": \"Plot\", \"subtype\": \"Chart\", \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\"}, \"doc\": null, \"tags\": [], \"id\": \"3ae20ad9-a799-42fc-900a-f81422be4b05\"}, \"id\": \"3ae20ad9-a799-42fc-900a-f81422be4b05\"}, {\"type\": \"ColumnDataSource\", \"attributes\": {\"callback\": null, \"selected\": {\"1d\": {\"indices\": []}, \"0d\": {\"indices\": [], \"flag\": false}, \"2d\": {\"indices\": []}}, \"id\": \"869279c3-e6d5-40a2-86ab-3d13394f2254\", \"doc\": null, \"data\": {\"y_z\": [1, 5, 12, 4, 2], \"x\": [0, 1, 2, 3, 4], \"y_y\": [6, 7, 2, 4, 5]}, \"tags\": [], \"column_names\": [\"y_z\", \"x\", \"y_y\"]}, \"id\": \"869279c3-e6d5-40a2-86ab-3d13394f2254\"}, {\"type\": \"WheelZoomTool\", \"attributes\": {\"id\": \"622811c0-bdaf-46e6-b85f-c26c4499e1ec\", \"dimensions\": [\"width\", \"height\"], \"plot\": {\"type\": \"Plot\", \"subtype\": \"Chart\", \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\"}, \"doc\": null, \"tags\": []}, \"id\": \"622811c0-bdaf-46e6-b85f-c26c4499e1ec\"}, {\"type\": \"GlyphRenderer\", \"attributes\": {\"glyph\": {\"type\": \"Line\", \"id\": \"116355ff-55ed-4b5a-b6c1-8cffbdcc2868\"}, \"id\": \"fb157fcb-307d-47b4-8ce6-242216515330\", \"selection_glyph\": null, \"doc\": null, \"nonselection_glyph\": null, \"tags\": [], \"data_source\": {\"type\": \"ColumnDataSource\", \"id\": \"869279c3-e6d5-40a2-86ab-3d13394f2254\"}}, \"id\": \"fb157fcb-307d-47b4-8ce6-242216515330\"}, {\"type\": \"ResetTool\", \"attributes\": {\"plot\": {\"type\": \"Plot\", \"subtype\": \"Chart\", \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\"}, \"doc\": null, \"tags\": [], \"id\": \"25e89f21-0343-4ad1-901f-2de2598e6b93\"}, \"id\": \"25e89f21-0343-4ad1-901f-2de2598e6b93\"}, {\"type\": \"BasicTicker\", \"attributes\": {\"mantissas\": [2, 5, 10], \"doc\": null, \"tags\": [], \"id\": \"1fdfccaf-829e-4d73-b142-adb6c542d9b8\"}, \"id\": \"1fdfccaf-829e-4d73-b142-adb6c542d9b8\"}, {\"type\": \"Plot\", \"attributes\": {\"right\": [], \"title\": \"simple line example\", \"tools\": [{\"type\": \"PanTool\", \"id\": \"69189d33-c5b8-4b89-9804-0079bc294e52\"}, {\"type\": \"WheelZoomTool\", \"id\": \"622811c0-bdaf-46e6-b85f-c26c4499e1ec\"}, {\"type\": \"BoxZoomTool\", \"id\": \"53e1c539-0996-44eb-a35a-4ef44705efd4\"}, {\"type\": \"PreviewSaveTool\", \"id\": \"3ae20ad9-a799-42fc-900a-f81422be4b05\"}, {\"type\": \"ResizeTool\", \"id\": \"6943fdf2-1713-4fe1-b6fd-afd1ba054ce6\"}, {\"type\": \"ResetTool\", \"id\": \"25e89f21-0343-4ad1-901f-2de2598e6b93\"}, {\"type\": \"HelpTool\", \"id\": \"f29519c5-aadc-488f-b27e-12f24fa0a544\"}], \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\", \"left\": [{\"type\": \"LinearAxis\", \"id\": \"c9dde829-b217-476a-b46e-aa5b9adca4df\"}], \"x_range\": {\"type\": \"DataRange1d\", \"id\": \"7fb0837c-15db-410e-95f1-ea0bcbcb2e1e\"}, \"renderers\": [{\"type\": \"LinearAxis\", \"id\": \"2423367e-e9d3-42ba-85a7-9b3f4d50dc4a\"}, {\"type\": \"LinearAxis\", \"id\": \"c9dde829-b217-476a-b46e-aa5b9adca4df\"}, {\"type\": \"Grid\", \"id\": \"08d28835-5291-42b2-8970-08a69ce9d907\"}, {\"type\": \"Grid\", \"id\": \"80b3befb-ded0-4be1-bb9c-6457762cb455\"}, {\"type\": \"GlyphRenderer\", \"id\": \"c3ac9b58-d538-4193-9d97-54287a5775b9\"}, {\"type\": \"GlyphRenderer\", \"id\": \"fb157fcb-307d-47b4-8ce6-242216515330\"}], \"doc\": null, \"plot_width\": 400, \"plot_height\": 400, \"above\": [], \"extra_y_ranges\": {}, \"tool_events\": {\"type\": \"ToolEvents\", \"id\": \"c2e88901-0ab8-4cb9-8ad7-03f182001625\"}, \"below\": [{\"type\": \"LinearAxis\", \"id\": \"2423367e-e9d3-42ba-85a7-9b3f4d50dc4a\"}], \"extra_x_ranges\": {}, \"tags\": [], \"y_range\": {\"type\": \"Range1d\", \"id\": \"34aba272-6763-49a2-b717-6e5fb7ab04c2\"}}, \"subtype\": \"Chart\", \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\"}, {\"type\": \"BasicTickFormatter\", \"attributes\": {\"doc\": null, \"tags\": [], \"id\": \"288bdf15-0f54-410c-9477-b93435ba84e7\"}, \"id\": \"288bdf15-0f54-410c-9477-b93435ba84e7\"}, {\"type\": \"ToolEvents\", \"attributes\": {\"tags\": [], \"doc\": null, \"geometries\": [], \"id\": \"c2e88901-0ab8-4cb9-8ad7-03f182001625\"}, \"id\": \"c2e88901-0ab8-4cb9-8ad7-03f182001625\"}, {\"type\": \"BasicTicker\", \"attributes\": {\"mantissas\": [2, 5, 10], \"doc\": null, \"tags\": [], \"id\": \"b4a6973b-725e-4ab0-a414-43a9d132702c\"}, \"id\": \"b4a6973b-725e-4ab0-a414-43a9d132702c\"}, {\"type\": \"Grid\", \"attributes\": {\"dimension\": 0, \"id\": \"08d28835-5291-42b2-8970-08a69ce9d907\", \"ticker\": {\"type\": \"BasicTicker\", \"id\": \"1fdfccaf-829e-4d73-b142-adb6c542d9b8\"}, \"plot\": {\"type\": \"Plot\", \"subtype\": \"Chart\", \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\"}, \"doc\": null, \"tags\": []}, \"id\": \"08d28835-5291-42b2-8970-08a69ce9d907\"}, {\"type\": \"BasicTickFormatter\", \"attributes\": {\"doc\": null, \"tags\": [], \"id\": \"97dbe69e-d261-4670-b2fa-6ef51581d286\"}, \"id\": \"97dbe69e-d261-4670-b2fa-6ef51581d286\"}, {\"type\": \"PanTool\", \"attributes\": {\"id\": \"69189d33-c5b8-4b89-9804-0079bc294e52\", \"dimensions\": [\"width\", \"height\"], \"plot\": {\"type\": \"Plot\", \"subtype\": \"Chart\", \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\"}, \"doc\": null, \"tags\": []}, \"id\": \"69189d33-c5b8-4b89-9804-0079bc294e52\"}, {\"type\": \"Grid\", \"attributes\": {\"dimension\": 1, \"id\": \"80b3befb-ded0-4be1-bb9c-6457762cb455\", \"ticker\": {\"type\": \"BasicTicker\", \"id\": \"b4a6973b-725e-4ab0-a414-43a9d132702c\"}, \"plot\": {\"type\": \"Plot\", \"subtype\": \"Chart\", \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\"}, \"doc\": null, \"tags\": []}, \"id\": \"80b3befb-ded0-4be1-bb9c-6457762cb455\"}, {\"type\": \"Line\", \"attributes\": {\"y\": {\"field\": \"y_z\"}, \"line_color\": {\"value\": \"#5ab738\"}, \"id\": \"116355ff-55ed-4b5a-b6c1-8cffbdcc2868\", \"doc\": null, \"x\": {\"field\": \"x\"}, \"tags\": []}, \"id\": \"116355ff-55ed-4b5a-b6c1-8cffbdcc2868\"}, {\"type\": \"ResizeTool\", \"attributes\": {\"plot\": {\"type\": \"Plot\", \"subtype\": \"Chart\", \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\"}, \"doc\": null, \"tags\": [], \"id\": \"6943fdf2-1713-4fe1-b6fd-afd1ba054ce6\"}, \"id\": \"6943fdf2-1713-4fe1-b6fd-afd1ba054ce6\"}, {\"type\": \"GlyphRenderer\", \"attributes\": {\"glyph\": {\"type\": \"Line\", \"id\": \"e1e4a776-e944-4f5b-8073-e4d924ab90a8\"}, \"id\": \"c3ac9b58-d538-4193-9d97-54287a5775b9\", \"selection_glyph\": null, \"doc\": null, \"nonselection_glyph\": null, \"tags\": [], \"data_source\": {\"type\": \"ColumnDataSource\", \"id\": \"869279c3-e6d5-40a2-86ab-3d13394f2254\"}}, \"id\": \"c3ac9b58-d538-4193-9d97-54287a5775b9\"}, {\"type\": \"Range1d\", \"attributes\": {\"callback\": null, \"id\": \"34aba272-6763-49a2-b717-6e5fb7ab04c2\", \"start\": -0.10000000000000009, \"end\": 13.1, \"doc\": null, \"tags\": []}, \"id\": \"34aba272-6763-49a2-b717-6e5fb7ab04c2\"}, {\"type\": \"BoxZoomTool\", \"attributes\": {\"id\": \"53e1c539-0996-44eb-a35a-4ef44705efd4\", \"dimensions\": [\"width\", \"height\"], \"plot\": {\"type\": \"Plot\", \"subtype\": \"Chart\", \"id\": \"3ebddb4a-d0bf-4d36-91b5-ae7b006404ec\"}, \"doc\": null, \"tags\": []}, \"id\": \"53e1c539-0996-44eb-a35a-4ef44705efd4\"}, {\"type\": \"DataRange1d\", \"attributes\": {\"callback\": null, \"id\": \"7fb0837c-15db-410e-95f1-ea0bcbcb2e1e\", \"renderers\": [], \"doc\": null, \"names\": [], \"tags\": []}, \"id\": \"7fb0837c-15db-410e-95f1-ea0bcbcb2e1e\"}];\n", | |
| " Bokeh.load_models(all_models);\n", | |
| " var plots = [{'modelid': '3ebddb4a-d0bf-4d36-91b5-ae7b006404ec', 'elementid': '#114f82cd-f9ff-4be8-939c-83bd29ca432d', 'modeltype': 'Plot'}];\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=\"114f82cd-f9ff-4be8-939c-83bd29ca432d\"></div>\n", | |
| "\n" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "show(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.2" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment