Skip to content

Instantly share code, notes, and snippets.

@tonyfast
Created August 12, 2015 17:33
Show Gist options
  • Save tonyfast/db6830cd9e68aa14dd8e to your computer and use it in GitHub Desktop.
Save tonyfast/db6830cd9e68aa14dd8e to your computer and use it in GitHub Desktop.
Use yaml to parse and also to create a bokeh chart
Display the source blob
Display the rendered blob
Raw
{
"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": 1,
"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>"
]
},
"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 addition Global YAML tag that can execute Python classes to construct a plot form the data later."
]
},
{
"cell_type": "code",
"execution_count": 2,
"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": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'plot': {'args': [{'y': ['6', '7', '2', '4', '5'], 'z': ['1', '5', '12', '4', '2']}], 'kwds': {'height': '400', 'ylabel': 'values', 'xlabel': 'x', 'width': '400', 'title': 'simple line example'}}}\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": 4,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"<script type=\"text/javascript\">\n",
" Bokeh.$(function() {\n",
" var all_models = [{\"id\": \"bf5bcd4c-f351-4729-80b7-c073431d1565\", \"attributes\": {\"doc\": null, \"formatter\": {\"id\": \"8458f772-ae57-4202-b6ca-68a03a6cf63b\", \"type\": \"BasicTickFormatter\"}, \"plot\": {\"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\", \"subtype\": \"Chart\", \"type\": \"Plot\"}, \"axis_label\": \"values\", \"ticker\": {\"id\": \"fed8769e-1c1b-423b-818c-a14e4b230137\", \"type\": \"BasicTicker\"}, \"tags\": [], \"id\": \"bf5bcd4c-f351-4729-80b7-c073431d1565\"}, \"type\": \"LinearAxis\"}, {\"id\": \"fec6e9cb-20d8-417d-a7e8-ceb6f47479a2\", \"attributes\": {\"data_source\": {\"id\": \"2761d9cd-c618-4ba7-83e9-3a4571790f1e\", \"type\": \"ColumnDataSource\"}, \"doc\": null, \"glyph\": {\"id\": \"a383a706-b5ce-4558-94af-2ed8f8203073\", \"type\": \"Line\"}, \"selection_glyph\": null, \"tags\": [], \"nonselection_glyph\": null, \"id\": \"fec6e9cb-20d8-417d-a7e8-ceb6f47479a2\"}, \"type\": \"GlyphRenderer\"}, {\"id\": \"fed8769e-1c1b-423b-818c-a14e4b230137\", \"attributes\": {\"doc\": null, \"mantissas\": [2, 5, 10], \"tags\": [], \"id\": \"fed8769e-1c1b-423b-818c-a14e4b230137\"}, \"type\": \"BasicTicker\"}, {\"id\": \"a383a706-b5ce-4558-94af-2ed8f8203073\", \"attributes\": {\"doc\": null, \"line_color\": {\"value\": \"#5ab738\"}, \"y\": {\"field\": \"y_z\"}, \"x\": {\"field\": \"x\"}, \"tags\": [], \"id\": \"a383a706-b5ce-4558-94af-2ed8f8203073\"}, \"type\": \"Line\"}, {\"id\": \"bddc75f2-8567-4e26-b7e1-fff8e12d253b\", \"attributes\": {\"doc\": null, \"ticker\": {\"id\": \"4fc78ca6-b987-47e8-a511-3980c48cf4d5\", \"type\": \"BasicTicker\"}, \"plot\": {\"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\", \"subtype\": \"Chart\", \"type\": \"Plot\"}, \"dimension\": 0, \"tags\": [], \"id\": \"bddc75f2-8567-4e26-b7e1-fff8e12d253b\"}, \"type\": \"Grid\"}, {\"id\": \"897445c2-bc98-44fc-ac8b-b3e08c3c0d7e\", \"attributes\": {\"doc\": null, \"plot\": {\"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\", \"subtype\": \"Chart\", \"type\": \"Plot\"}, \"tags\": [], \"id\": \"897445c2-bc98-44fc-ac8b-b3e08c3c0d7e\"}, \"type\": \"PreviewSaveTool\"}, {\"id\": \"cd32d434-8fb4-4362-8945-58dde0a0b930\", \"attributes\": {\"doc\": null, \"plot\": {\"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\", \"subtype\": \"Chart\", \"type\": \"Plot\"}, \"tags\": [], \"dimensions\": [\"width\", \"height\"], \"id\": \"cd32d434-8fb4-4362-8945-58dde0a0b930\"}, \"type\": \"BoxZoomTool\"}, {\"id\": \"5b847362-12b3-405b-a180-d7ddd4600d93\", \"attributes\": {\"data_source\": {\"id\": \"2761d9cd-c618-4ba7-83e9-3a4571790f1e\", \"type\": \"ColumnDataSource\"}, \"doc\": null, \"glyph\": {\"id\": \"3701e849-f0b4-4be4-a3d5-031e26412e40\", \"type\": \"Line\"}, \"selection_glyph\": null, \"tags\": [], \"nonselection_glyph\": null, \"id\": \"5b847362-12b3-405b-a180-d7ddd4600d93\"}, \"type\": \"GlyphRenderer\"}, {\"id\": \"87f72347-c2f8-4033-8bd5-3dc61071789d\", \"attributes\": {\"doc\": null, \"end\": 13.1, \"start\": -0.10000000000000009, \"tags\": [], \"callback\": null, \"id\": \"87f72347-c2f8-4033-8bd5-3dc61071789d\"}, \"type\": \"Range1d\"}, {\"id\": \"3701e849-f0b4-4be4-a3d5-031e26412e40\", \"attributes\": {\"doc\": null, \"line_color\": {\"value\": \"#f22c40\"}, \"y\": {\"field\": \"y_y\"}, \"x\": {\"field\": \"x\"}, \"tags\": [], \"id\": \"3701e849-f0b4-4be4-a3d5-031e26412e40\"}, \"type\": \"Line\"}, {\"id\": \"15f366c0-6b10-4350-b05f-6583e1310f9d\", \"attributes\": {\"doc\": null, \"plot\": {\"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\", \"subtype\": \"Chart\", \"type\": \"Plot\"}, \"tags\": [], \"id\": \"15f366c0-6b10-4350-b05f-6583e1310f9d\"}, \"type\": \"ResetTool\"}, {\"id\": \"2761d9cd-c618-4ba7-83e9-3a4571790f1e\", \"attributes\": {\"data\": {\"y_z\": [1, 5, 12, 4, 2], \"x\": [0, 1, 2, 3, 4], \"y_y\": [6, 7, 2, 4, 5]}, \"doc\": null, \"selected\": {\"0d\": {\"indices\": [], \"flag\": false}, \"1d\": {\"indices\": []}, \"2d\": {\"indices\": []}}, \"column_names\": [\"y_z\", \"x\", \"y_y\"], \"tags\": [], \"callback\": null, \"id\": \"2761d9cd-c618-4ba7-83e9-3a4571790f1e\"}, \"type\": \"ColumnDataSource\"}, {\"id\": \"48110f05-4766-460e-b49a-547fc6a71055\", \"attributes\": {\"doc\": null, \"plot\": {\"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\", \"subtype\": \"Chart\", \"type\": \"Plot\"}, \"tags\": [], \"id\": \"48110f05-4766-460e-b49a-547fc6a71055\"}, \"type\": \"HelpTool\"}, {\"id\": \"138e4be0-c219-4696-a26f-77052efddc2d\", \"attributes\": {\"doc\": null, \"geometries\": [], \"tags\": [], \"id\": \"138e4be0-c219-4696-a26f-77052efddc2d\"}, \"type\": \"ToolEvents\"}, {\"id\": \"eef19e84-f390-414f-8a4f-079e2c7f5bb7\", \"attributes\": {\"doc\": null, \"renderers\": [], \"names\": [], \"tags\": [], \"callback\": null, \"id\": \"eef19e84-f390-414f-8a4f-079e2c7f5bb7\"}, \"type\": \"DataRange1d\"}, {\"id\": \"ff29d363-6186-48fc-837f-664f2b98120b\", \"attributes\": {\"doc\": null, \"plot\": {\"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\", \"subtype\": \"Chart\", \"type\": \"Plot\"}, \"tags\": [], \"id\": \"ff29d363-6186-48fc-837f-664f2b98120b\"}, \"type\": \"ResizeTool\"}, {\"id\": \"58c43310-3ea5-4f16-864e-1d01b9ebdc87\", \"attributes\": {\"doc\": null, \"tags\": [], \"id\": \"58c43310-3ea5-4f16-864e-1d01b9ebdc87\"}, \"type\": \"BasicTickFormatter\"}, {\"id\": \"8458f772-ae57-4202-b6ca-68a03a6cf63b\", \"attributes\": {\"doc\": null, \"tags\": [], \"id\": \"8458f772-ae57-4202-b6ca-68a03a6cf63b\"}, \"type\": \"BasicTickFormatter\"}, {\"id\": \"f0822a89-a6eb-4bda-a296-6485ac032d6f\", \"attributes\": {\"doc\": null, \"plot\": {\"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\", \"subtype\": \"Chart\", \"type\": \"Plot\"}, \"tags\": [], \"dimensions\": [\"width\", \"height\"], \"id\": \"f0822a89-a6eb-4bda-a296-6485ac032d6f\"}, \"type\": \"PanTool\"}, {\"id\": \"804f2e00-33aa-41d3-a058-ec73ae426ac2\", \"attributes\": {\"doc\": null, \"plot\": {\"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\", \"subtype\": \"Chart\", \"type\": \"Plot\"}, \"tags\": [], \"dimensions\": [\"width\", \"height\"], \"id\": \"804f2e00-33aa-41d3-a058-ec73ae426ac2\"}, \"type\": \"WheelZoomTool\"}, {\"id\": \"4735e5f1-fd5e-4e83-a0a5-6d5710c947ff\", \"attributes\": {\"doc\": null, \"formatter\": {\"id\": \"58c43310-3ea5-4f16-864e-1d01b9ebdc87\", \"type\": \"BasicTickFormatter\"}, \"plot\": {\"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\", \"subtype\": \"Chart\", \"type\": \"Plot\"}, \"axis_label\": \"x\", \"ticker\": {\"id\": \"4fc78ca6-b987-47e8-a511-3980c48cf4d5\", \"type\": \"BasicTicker\"}, \"tags\": [], \"id\": \"4735e5f1-fd5e-4e83-a0a5-6d5710c947ff\"}, \"type\": \"LinearAxis\"}, {\"id\": \"4fc78ca6-b987-47e8-a511-3980c48cf4d5\", \"attributes\": {\"doc\": null, \"mantissas\": [2, 5, 10], \"tags\": [], \"id\": \"4fc78ca6-b987-47e8-a511-3980c48cf4d5\"}, \"type\": \"BasicTicker\"}, {\"attributes\": {\"y_range\": {\"id\": \"87f72347-c2f8-4033-8bd5-3dc61071789d\", \"type\": \"Range1d\"}, \"extra_y_ranges\": {}, \"plot_width\": 400, \"left\": [{\"id\": \"bf5bcd4c-f351-4729-80b7-c073431d1565\", \"type\": \"LinearAxis\"}], \"renderers\": [{\"id\": \"4735e5f1-fd5e-4e83-a0a5-6d5710c947ff\", \"type\": \"LinearAxis\"}, {\"id\": \"bf5bcd4c-f351-4729-80b7-c073431d1565\", \"type\": \"LinearAxis\"}, {\"id\": \"bddc75f2-8567-4e26-b7e1-fff8e12d253b\", \"type\": \"Grid\"}, {\"id\": \"0b712b7c-5ee9-4e3e-a3b8-254022ff93b6\", \"type\": \"Grid\"}, {\"id\": \"5b847362-12b3-405b-a180-d7ddd4600d93\", \"type\": \"GlyphRenderer\"}, {\"id\": \"fec6e9cb-20d8-417d-a7e8-ceb6f47479a2\", \"type\": \"GlyphRenderer\"}], \"above\": [], \"tags\": [], \"doc\": null, \"plot_height\": 400, \"title\": \"simple line example\", \"tools\": [{\"id\": \"f0822a89-a6eb-4bda-a296-6485ac032d6f\", \"type\": \"PanTool\"}, {\"id\": \"804f2e00-33aa-41d3-a058-ec73ae426ac2\", \"type\": \"WheelZoomTool\"}, {\"id\": \"cd32d434-8fb4-4362-8945-58dde0a0b930\", \"type\": \"BoxZoomTool\"}, {\"id\": \"897445c2-bc98-44fc-ac8b-b3e08c3c0d7e\", \"type\": \"PreviewSaveTool\"}, {\"id\": \"ff29d363-6186-48fc-837f-664f2b98120b\", \"type\": \"ResizeTool\"}, {\"id\": \"15f366c0-6b10-4350-b05f-6583e1310f9d\", \"type\": \"ResetTool\"}, {\"id\": \"48110f05-4766-460e-b49a-547fc6a71055\", \"type\": \"HelpTool\"}], \"below\": [{\"id\": \"4735e5f1-fd5e-4e83-a0a5-6d5710c947ff\", \"type\": \"LinearAxis\"}], \"right\": [], \"tool_events\": {\"id\": \"138e4be0-c219-4696-a26f-77052efddc2d\", \"type\": \"ToolEvents\"}, \"x_range\": {\"id\": \"eef19e84-f390-414f-8a4f-079e2c7f5bb7\", \"type\": \"DataRange1d\"}, \"extra_x_ranges\": {}, \"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\"}, \"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\", \"subtype\": \"Chart\", \"type\": \"Plot\"}, {\"id\": \"0b712b7c-5ee9-4e3e-a3b8-254022ff93b6\", \"attributes\": {\"doc\": null, \"ticker\": {\"id\": \"fed8769e-1c1b-423b-818c-a14e4b230137\", \"type\": \"BasicTicker\"}, \"plot\": {\"id\": \"5d04b1c5-49f7-484f-866d-22b945472d5c\", \"subtype\": \"Chart\", \"type\": \"Plot\"}, \"dimension\": 1, \"tags\": [], \"id\": \"0b712b7c-5ee9-4e3e-a3b8-254022ff93b6\"}, \"type\": \"Grid\"}];\n",
" Bokeh.load_models(all_models);\n",
" var plots = [{'modeltype': 'Plot', 'elementid': '#41b5d0d8-c371-4121-9dd1-f31c836fc65c', 'modelid': '5d04b1c5-49f7-484f-866d-22b945472d5c'}];\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=\"41b5d0d8-c371-4121-9dd1-f31c836fc65c\"></div>\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"yaml_plot = yaml.load(yaml_string)\n",
"\n",
"show(yaml_plot['plot'])"
]
},
{
"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.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