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 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": "code", | |
"execution_count": 5, | |
"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": [ | |
"import IPython\n", | |
"from bokeh.models import ColumnDataSource, OpenURL, TapTool, Callback\n", | |
"from bokeh.plotting import figure, output_notebook, show\n", | |
"from bokeh.resources import CDN\n", | |
"output_notebook(resources=CDN)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<script type=\"text/javascript\">\n", | |
" Bokeh.$(function() {\n", | |
" var all_models = [{\"attributes\": {\"plot\": {\"subtype\": \"Figure\", \"type\": \"Plot\", \"id\": \"8036209d-ef12-49e3-b845-5cd9543cdba9\"}, \"tags\": [], \"ticker\": {\"type\": \"BasicTicker\", \"id\": \"01403758-637d-4ed9-84ba-fb56320df6c8\"}, \"dimension\": 0, \"doc\": null, \"id\": \"9307da96-9866-4d71-a3fe-6ca23c35e6c0\"}, \"type\": \"Grid\", \"id\": \"9307da96-9866-4d71-a3fe-6ca23c35e6c0\"}, {\"attributes\": {\"doc\": null, \"tags\": [], \"id\": \"47bffc8d-5641-49da-a8d1-6ae76b14be72\"}, \"type\": \"BasicTickFormatter\", \"id\": \"47bffc8d-5641-49da-a8d1-6ae76b14be72\"}, {\"attributes\": {\"url\": \"http://www.colors.commutercreative.com/@color/\", \"tags\": [], \"doc\": null, \"id\": \"b62f3762-668e-4589-b51c-51540d8623ff\"}, \"type\": \"OpenURL\", \"id\": \"b62f3762-668e-4589-b51c-51540d8623ff\"}, {\"attributes\": {\"left\": [{\"type\": \"LinearAxis\", \"id\": \"add2c7a4-579d-4d7b-8a9c-262c4214b255\"}], \"tools\": [{\"type\": \"TapTool\", \"id\": \"de8aed10-84f1-4a5d-9853-bc1b380eae1b\"}], \"tool_events\": {\"type\": \"ToolEvents\", \"id\": \"2c02d164-0a2f-4d6c-82c5-a8df68c70044\"}, \"tags\": [], \"renderers\": [{\"type\": \"LinearAxis\", \"id\": \"f5426fbb-024e-46d0-8040-a66527b7eed9\"}, {\"type\": \"Grid\", \"id\": \"9307da96-9866-4d71-a3fe-6ca23c35e6c0\"}, {\"type\": \"LinearAxis\", \"id\": \"add2c7a4-579d-4d7b-8a9c-262c4214b255\"}, {\"type\": \"Grid\", \"id\": \"b280dfc0-114a-42e0-ac62-8d27c2aa1331\"}, {\"type\": \"GlyphRenderer\", \"id\": \"23be5ad0-15dc-4e41-9125-1df7ce68cc1b\"}], \"below\": [{\"type\": \"LinearAxis\", \"id\": \"f5426fbb-024e-46d0-8040-a66527b7eed9\"}], \"y_range\": {\"type\": \"DataRange1d\", \"id\": \"5e156ef1-9bbe-4c4d-9e70-ab4f1dfa44e6\"}, \"doc\": null, \"above\": [], \"right\": [], \"plot_height\": 400, \"plot_width\": 400, \"extra_y_ranges\": {}, \"title\": \"Click the Dots\", \"x_range\": {\"type\": \"DataRange1d\", \"id\": \"26775507-f547-42cf-b6a9-bc15c8449515\"}, \"extra_x_ranges\": {}, \"id\": \"8036209d-ef12-49e3-b845-5cd9543cdba9\"}, \"subtype\": \"Figure\", \"type\": \"Plot\", \"id\": \"8036209d-ef12-49e3-b845-5cd9543cdba9\"}, {\"attributes\": {\"tags\": [], \"x\": {\"field\": \"x\"}, \"fill_color\": {\"field\": \"color\"}, \"line_color\": {\"field\": \"color\"}, \"doc\": null, \"size\": {\"value\": 20, \"units\": \"screen\"}, \"line_alpha\": {\"value\": 1.0}, \"y\": {\"field\": \"y\"}, \"fill_alpha\": {\"value\": 1.0}, \"id\": \"91679e10-d2aa-431b-8669-5a8802c2fa43\"}, \"type\": \"Circle\", \"id\": \"91679e10-d2aa-431b-8669-5a8802c2fa43\"}, {\"attributes\": {\"data\": {\"y\": [2, 5, 8, 2, 7], \"color\": [\"navy\", \"orange\", \"olive\", \"firebrick\", \"gold\"], \"x\": [1, 2, 3, 4, 5]}, \"callback\": null, \"tags\": [], \"column_names\": [\"y\", \"color\", \"x\"], \"doc\": null, \"selected\": {\"2d\": {\"indices\": []}, \"0d\": {\"flag\": false, \"indices\": []}, \"1d\": {\"indices\": []}}, \"id\": \"c8c2b0d1-edbe-4fca-bebc-779df6e1ad62\"}, \"type\": \"ColumnDataSource\", \"id\": \"c8c2b0d1-edbe-4fca-bebc-779df6e1ad62\"}, {\"attributes\": {\"names\": [], \"tags\": [], \"callback\": null, \"renderers\": [], \"doc\": null, \"id\": \"26775507-f547-42cf-b6a9-bc15c8449515\"}, \"type\": \"DataRange1d\", \"id\": \"26775507-f547-42cf-b6a9-bc15c8449515\"}, {\"attributes\": {\"names\": [], \"tags\": [], \"callback\": null, \"renderers\": [], \"doc\": null, \"id\": \"5e156ef1-9bbe-4c4d-9e70-ab4f1dfa44e6\"}, \"type\": \"DataRange1d\", \"id\": \"5e156ef1-9bbe-4c4d-9e70-ab4f1dfa44e6\"}, {\"attributes\": {\"doc\": null, \"tags\": [], \"id\": \"0b92eda2-af68-4502-b0ec-bf997221c591\"}, \"type\": \"BasicTickFormatter\", \"id\": \"0b92eda2-af68-4502-b0ec-bf997221c591\"}, {\"attributes\": {\"plot\": {\"subtype\": \"Figure\", \"type\": \"Plot\", \"id\": \"8036209d-ef12-49e3-b845-5cd9543cdba9\"}, \"formatter\": {\"type\": \"BasicTickFormatter\", \"id\": \"47bffc8d-5641-49da-a8d1-6ae76b14be72\"}, \"tags\": [], \"ticker\": {\"type\": \"BasicTicker\", \"id\": \"442ea605-bb09-48be-a535-08d2a198b108\"}, \"doc\": null, \"id\": \"add2c7a4-579d-4d7b-8a9c-262c4214b255\"}, \"type\": \"LinearAxis\", \"id\": \"add2c7a4-579d-4d7b-8a9c-262c4214b255\"}, {\"attributes\": {\"tags\": [], \"doc\": null, \"geometries\": [], \"id\": \"2c02d164-0a2f-4d6c-82c5-a8df68c70044\"}, \"type\": \"ToolEvents\", \"id\": \"2c02d164-0a2f-4d6c-82c5-a8df68c70044\"}, {\"attributes\": {\"num_minor_ticks\": 5, \"tags\": [], \"doc\": null, \"mantissas\": [2, 5, 10], \"id\": \"442ea605-bb09-48be-a535-08d2a198b108\"}, \"type\": \"BasicTicker\", \"id\": \"442ea605-bb09-48be-a535-08d2a198b108\"}, {\"attributes\": {\"plot\": {\"subtype\": \"Figure\", \"type\": \"Plot\", \"id\": \"8036209d-ef12-49e3-b845-5cd9543cdba9\"}, \"formatter\": {\"type\": \"BasicTickFormatter\", \"id\": \"0b92eda2-af68-4502-b0ec-bf997221c591\"}, \"tags\": [], \"ticker\": {\"type\": \"BasicTicker\", \"id\": \"01403758-637d-4ed9-84ba-fb56320df6c8\"}, \"doc\": null, \"id\": \"f5426fbb-024e-46d0-8040-a66527b7eed9\"}, \"type\": \"LinearAxis\", \"id\": \"f5426fbb-024e-46d0-8040-a66527b7eed9\"}, {\"attributes\": {\"nonselection_glyph\": {\"type\": \"Circle\", \"id\": \"1bed064c-ac0d-4edc-aecb-d096eb71af4b\"}, \"data_source\": {\"type\": \"ColumnDataSource\", \"id\": \"c8c2b0d1-edbe-4fca-bebc-779df6e1ad62\"}, \"tags\": [], \"selection_glyph\": null, \"id\": \"23be5ad0-15dc-4e41-9125-1df7ce68cc1b\", \"doc\": null, \"glyph\": {\"type\": \"Circle\", \"id\": \"91679e10-d2aa-431b-8669-5a8802c2fa43\"}}, \"type\": \"GlyphRenderer\", \"id\": \"23be5ad0-15dc-4e41-9125-1df7ce68cc1b\"}, {\"attributes\": {\"num_minor_ticks\": 5, \"tags\": [], \"doc\": null, \"mantissas\": [2, 5, 10], \"id\": \"01403758-637d-4ed9-84ba-fb56320df6c8\"}, \"type\": \"BasicTicker\", \"id\": \"01403758-637d-4ed9-84ba-fb56320df6c8\"}, {\"attributes\": {\"names\": [], \"always_active\": true, \"plot\": {\"subtype\": \"Figure\", \"type\": \"Plot\", \"id\": \"8036209d-ef12-49e3-b845-5cd9543cdba9\"}, \"tags\": [], \"action\": {\"type\": \"OpenURL\", \"id\": \"b62f3762-668e-4589-b51c-51540d8623ff\"}, \"renderers\": [], \"doc\": null, \"id\": \"de8aed10-84f1-4a5d-9853-bc1b380eae1b\"}, \"type\": \"TapTool\", \"id\": \"de8aed10-84f1-4a5d-9853-bc1b380eae1b\"}, {\"attributes\": {\"plot\": {\"subtype\": \"Figure\", \"type\": \"Plot\", \"id\": \"8036209d-ef12-49e3-b845-5cd9543cdba9\"}, \"tags\": [], \"ticker\": {\"type\": \"BasicTicker\", \"id\": \"442ea605-bb09-48be-a535-08d2a198b108\"}, \"dimension\": 1, \"doc\": null, \"id\": \"b280dfc0-114a-42e0-ac62-8d27c2aa1331\"}, \"type\": \"Grid\", \"id\": \"b280dfc0-114a-42e0-ac62-8d27c2aa1331\"}, {\"attributes\": {\"tags\": [], \"x\": {\"field\": \"x\"}, \"fill_color\": {\"value\": \"#1f77b4\"}, \"line_color\": {\"value\": \"#1f77b4\"}, \"doc\": null, \"size\": {\"value\": 20, \"units\": \"screen\"}, \"line_alpha\": {\"value\": 0.1}, \"y\": {\"field\": \"y\"}, \"fill_alpha\": {\"value\": 0.1}, \"id\": \"1bed064c-ac0d-4edc-aecb-d096eb71af4b\"}, \"type\": \"Circle\", \"id\": \"1bed064c-ac0d-4edc-aecb-d096eb71af4b\"}];\n", | |
" Bokeh.load_models(all_models);\n", | |
" var plots = [{'modelid': '8036209d-ef12-49e3-b845-5cd9543cdba9', 'modeltype': 'Plot', 'elementid': '#498534c7-5df3-4acc-a507-80490f6b6f31'}];\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=\"498534c7-5df3-4acc-a507-80490f6b6f31\"></div>\n", | |
"\n" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"p = figure(plot_width=400, plot_height=400,\n", | |
" tools=\"tap\", title=\"Click the Dots\")\n", | |
"\n", | |
"source = ColumnDataSource(data=dict(\n", | |
" x=[1, 2, 3, 4, 5],\n", | |
" y=[2, 5, 8, 2, 7],\n", | |
" color=[\"navy\", \"orange\", \"olive\", \"firebrick\", \"gold\"]\n", | |
" ))\n", | |
"\n", | |
"p.circle('x', 'y', color='color', size=20, source=source)\n", | |
"\n", | |
"url = \"http://www.colors.commutercreative.com/@color/\"\n", | |
"taptool = p.select(type=TapTool)\n", | |
"taptool.action = OpenURL(url=url)\n", | |
"\n", | |
"show(p)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<script type=\"text/javascript\">\n", | |
" Bokeh.$(function() {\n", | |
" var all_models = [{\"attributes\": {\"data\": {\"y\": [2, 5, 8, 2, 7], \"color\": [\"navy\", \"orange\", \"olive\", \"firebrick\", \"gold\"], \"x\": [1, 2, 3, 4, 5]}, \"callback\": null, \"tags\": [], \"column_names\": [\"y\", \"color\", \"x\"], \"doc\": null, \"selected\": {\"2d\": {\"indices\": []}, \"0d\": {\"flag\": false, \"indices\": []}, \"1d\": {\"indices\": []}}, \"id\": \"a99a1211-c79c-41f1-9063-c3ba55aef671\"}, \"type\": \"ColumnDataSource\", \"id\": \"a99a1211-c79c-41f1-9063-c3ba55aef671\"}, {\"attributes\": {\"num_minor_ticks\": 5, \"tags\": [], \"doc\": null, \"mantissas\": [2, 5, 10], \"id\": \"ba3e376a-d4eb-497a-b339-aa94aae86df0\"}, \"type\": \"BasicTicker\", \"id\": \"ba3e376a-d4eb-497a-b339-aa94aae86df0\"}, {\"attributes\": {\"tags\": [], \"x\": {\"field\": \"x\"}, \"fill_color\": {\"value\": \"#1f77b4\"}, \"line_color\": {\"value\": \"#1f77b4\"}, \"doc\": null, \"size\": {\"value\": 20, \"units\": \"screen\"}, \"line_alpha\": {\"value\": 0.1}, \"y\": {\"field\": \"y\"}, \"fill_alpha\": {\"value\": 0.1}, \"id\": \"5c189f58-506e-418d-b2f7-afa4a91a34d0\"}, \"type\": \"Circle\", \"id\": \"5c189f58-506e-418d-b2f7-afa4a91a34d0\"}, {\"attributes\": {\"args\": {\"source\": {\"type\": \"ColumnDataSource\", \"id\": \"a99a1211-c79c-41f1-9063-c3ba55aef671\"}}, \"tags\": [], \"code\": \"eval( \\\"a = cb_obj.get('selected')\\\" );\\n id = cb_obj.get('selected')['1d']['indices'][0];\\n $('#block').text( cb_obj.get('data')['color'][id]);\\n console.log( cb_obj.get('data') );\\n IPython.notebook.kernel.execute( 'current_color = \\\"' + cb_obj.get('data')['color'][id] + '\\\"')\\n debugger;\\n \", \"doc\": null, \"id\": \"0d490da8-ca12-4e29-95cb-24d518fdbf1a\"}, \"type\": \"Callback\", \"id\": \"0d490da8-ca12-4e29-95cb-24d518fdbf1a\"}, {\"attributes\": {\"plot\": {\"subtype\": \"Figure\", \"type\": \"Plot\", \"id\": \"6f105649-cab1-4ac4-965b-fb64853d9352\"}, \"formatter\": {\"type\": \"BasicTickFormatter\", \"id\": \"67ec3d13-f858-40b7-93e7-38ec1a67428f\"}, \"tags\": [], \"ticker\": {\"type\": \"BasicTicker\", \"id\": \"ba3e376a-d4eb-497a-b339-aa94aae86df0\"}, \"doc\": null, \"id\": \"d51ee07a-879e-4b21-a917-c90d68142d67\"}, \"type\": \"LinearAxis\", \"id\": \"d51ee07a-879e-4b21-a917-c90d68142d67\"}, {\"attributes\": {\"doc\": null, \"tags\": [], \"id\": \"429b28f1-d3f2-4c1e-aa3b-74acad8028ec\"}, \"type\": \"BasicTickFormatter\", \"id\": \"429b28f1-d3f2-4c1e-aa3b-74acad8028ec\"}, {\"attributes\": {\"names\": [], \"always_active\": true, \"plot\": {\"subtype\": \"Figure\", \"type\": \"Plot\", \"id\": \"6f105649-cab1-4ac4-965b-fb64853d9352\"}, \"tags\": [], \"action\": {\"type\": \"Callback\", \"id\": \"0d490da8-ca12-4e29-95cb-24d518fdbf1a\"}, \"renderers\": [], \"doc\": null, \"id\": \"cd1a2fee-68f1-40b2-b624-a5481893237d\"}, \"type\": \"TapTool\", \"id\": \"cd1a2fee-68f1-40b2-b624-a5481893237d\"}, {\"attributes\": {\"names\": [], \"tags\": [], \"callback\": null, \"renderers\": [], \"doc\": null, \"id\": \"333357f8-cfbd-4e88-8181-2959ea2f11af\"}, \"type\": \"DataRange1d\", \"id\": \"333357f8-cfbd-4e88-8181-2959ea2f11af\"}, {\"attributes\": {\"tags\": [], \"x\": {\"field\": \"x\"}, \"fill_color\": {\"field\": \"color\"}, \"line_color\": {\"field\": \"color\"}, \"doc\": null, \"size\": {\"value\": 20, \"units\": \"screen\"}, \"line_alpha\": {\"value\": 1.0}, \"y\": {\"field\": \"y\"}, \"fill_alpha\": {\"value\": 1.0}, \"id\": \"662c777b-6cbb-4ec0-8849-9c9a1cbf28a1\"}, \"type\": \"Circle\", \"id\": \"662c777b-6cbb-4ec0-8849-9c9a1cbf28a1\"}, {\"attributes\": {\"plot\": {\"subtype\": \"Figure\", \"type\": \"Plot\", \"id\": \"6f105649-cab1-4ac4-965b-fb64853d9352\"}, \"tags\": [], \"ticker\": {\"type\": \"BasicTicker\", \"id\": \"c69b4980-f97c-41b0-becf-241028fecb76\"}, \"dimension\": 1, \"doc\": null, \"id\": \"ab433495-8b2e-425d-8981-e9749e008883\"}, \"type\": \"Grid\", \"id\": \"ab433495-8b2e-425d-8981-e9749e008883\"}, {\"attributes\": {\"names\": [], \"tags\": [], \"callback\": null, \"renderers\": [], \"doc\": null, \"id\": \"4064f5a5-ec2c-49e3-9fcd-beec0c49bca7\"}, \"type\": \"DataRange1d\", \"id\": \"4064f5a5-ec2c-49e3-9fcd-beec0c49bca7\"}, {\"attributes\": {\"plot\": {\"subtype\": \"Figure\", \"type\": \"Plot\", \"id\": \"6f105649-cab1-4ac4-965b-fb64853d9352\"}, \"tags\": [], \"ticker\": {\"type\": \"BasicTicker\", \"id\": \"ba3e376a-d4eb-497a-b339-aa94aae86df0\"}, \"dimension\": 0, \"doc\": null, \"id\": \"14a4e3bb-7966-4639-b16d-7fbd6699f371\"}, \"type\": \"Grid\", \"id\": \"14a4e3bb-7966-4639-b16d-7fbd6699f371\"}, {\"attributes\": {\"nonselection_glyph\": {\"type\": \"Circle\", \"id\": \"5c189f58-506e-418d-b2f7-afa4a91a34d0\"}, \"data_source\": {\"type\": \"ColumnDataSource\", \"id\": \"a99a1211-c79c-41f1-9063-c3ba55aef671\"}, \"tags\": [], \"selection_glyph\": null, \"id\": \"4573fa44-1fe9-4403-a1d4-df9e77292780\", \"doc\": null, \"glyph\": {\"type\": \"Circle\", \"id\": \"662c777b-6cbb-4ec0-8849-9c9a1cbf28a1\"}}, \"type\": \"GlyphRenderer\", \"id\": \"4573fa44-1fe9-4403-a1d4-df9e77292780\"}, {\"attributes\": {\"left\": [{\"type\": \"LinearAxis\", \"id\": \"6d23be59-3a64-424e-b63d-648ff9b58a20\"}], \"tools\": [{\"type\": \"TapTool\", \"id\": \"cd1a2fee-68f1-40b2-b624-a5481893237d\"}], \"tool_events\": {\"type\": \"ToolEvents\", \"id\": \"731c49d1-4d5f-4b91-8b95-fc9ac781b512\"}, \"tags\": [], \"renderers\": [{\"type\": \"LinearAxis\", \"id\": \"d51ee07a-879e-4b21-a917-c90d68142d67\"}, {\"type\": \"Grid\", \"id\": \"14a4e3bb-7966-4639-b16d-7fbd6699f371\"}, {\"type\": \"LinearAxis\", \"id\": \"6d23be59-3a64-424e-b63d-648ff9b58a20\"}, {\"type\": \"Grid\", \"id\": \"ab433495-8b2e-425d-8981-e9749e008883\"}, {\"type\": \"GlyphRenderer\", \"id\": \"4573fa44-1fe9-4403-a1d4-df9e77292780\"}], \"below\": [{\"type\": \"LinearAxis\", \"id\": \"d51ee07a-879e-4b21-a917-c90d68142d67\"}], \"y_range\": {\"type\": \"DataRange1d\", \"id\": \"333357f8-cfbd-4e88-8181-2959ea2f11af\"}, \"doc\": null, \"above\": [], \"right\": [], \"plot_height\": 400, \"plot_width\": 400, \"extra_y_ranges\": {}, \"title\": \"Click the Dots\", \"x_range\": {\"type\": \"DataRange1d\", \"id\": \"4064f5a5-ec2c-49e3-9fcd-beec0c49bca7\"}, \"extra_x_ranges\": {}, \"id\": \"6f105649-cab1-4ac4-965b-fb64853d9352\"}, \"subtype\": \"Figure\", \"type\": \"Plot\", \"id\": \"6f105649-cab1-4ac4-965b-fb64853d9352\"}, {\"attributes\": {\"doc\": null, \"tags\": [], \"id\": \"67ec3d13-f858-40b7-93e7-38ec1a67428f\"}, \"type\": \"BasicTickFormatter\", \"id\": \"67ec3d13-f858-40b7-93e7-38ec1a67428f\"}, {\"attributes\": {\"num_minor_ticks\": 5, \"tags\": [], \"doc\": null, \"mantissas\": [2, 5, 10], \"id\": \"c69b4980-f97c-41b0-becf-241028fecb76\"}, \"type\": \"BasicTicker\", \"id\": \"c69b4980-f97c-41b0-becf-241028fecb76\"}, {\"attributes\": {\"tags\": [], \"doc\": null, \"geometries\": [], \"id\": \"731c49d1-4d5f-4b91-8b95-fc9ac781b512\"}, \"type\": \"ToolEvents\", \"id\": \"731c49d1-4d5f-4b91-8b95-fc9ac781b512\"}, {\"attributes\": {\"plot\": {\"subtype\": \"Figure\", \"type\": \"Plot\", \"id\": \"6f105649-cab1-4ac4-965b-fb64853d9352\"}, \"formatter\": {\"type\": \"BasicTickFormatter\", \"id\": \"429b28f1-d3f2-4c1e-aa3b-74acad8028ec\"}, \"tags\": [], \"ticker\": {\"type\": \"BasicTicker\", \"id\": \"c69b4980-f97c-41b0-becf-241028fecb76\"}, \"doc\": null, \"id\": \"6d23be59-3a64-424e-b63d-648ff9b58a20\"}, \"type\": \"LinearAxis\", \"id\": \"6d23be59-3a64-424e-b63d-648ff9b58a20\"}];\n", | |
" Bokeh.load_models(all_models);\n", | |
" var plots = [{'modelid': '6f105649-cab1-4ac4-965b-fb64853d9352', 'modeltype': 'Plot', 'elementid': '#4fc8531d-1014-49da-be26-4069e7c96f1d'}];\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=\"4fc8531d-1014-49da-be26-4069e7c96f1d\"></div>\n", | |
"\n" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"p = figure(plot_width=400, plot_height=400,\n", | |
" tools=\"tap\", title=\"Click the Dots\")\n", | |
"\n", | |
"source = ColumnDataSource(data=dict(\n", | |
" x=[1, 2, 3, 4, 5],\n", | |
" y=[2, 5, 8, 2, 7],\n", | |
" color=[\"navy\", \"orange\", \"olive\", \"firebrick\", \"gold\"]\n", | |
" ))\n", | |
"\n", | |
"p.circle('x', 'y', color='color', size=20, source=source)\n", | |
"\n", | |
"url = \"http://www.colors.commutercreative.com/@color/\"\n", | |
"taptool = p.select(type=TapTool)\n", | |
"taptool.action = Callback(\n", | |
" args = {\n", | |
" 'source': source\n", | |
" },\n", | |
" code = \"\"\"eval( \"a = cb_obj.get('selected')\" );\n", | |
" id = cb_obj.get('selected')['1d']['indices'][0];\n", | |
" $('#block').text( cb_obj.get('data')['color'][id]);\n", | |
" console.log( cb_obj.get('data') );\n", | |
" IPython.notebook.kernel.execute( 'current_color = \"' + cb_obj.get('data')['color'][id] + '\"')\n", | |
" debugger;\n", | |
" \"\"\"\n", | |
")\n", | |
"\n", | |
"show(p)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<h1 id=\"block\">Nothing Clicked Yet</h1>" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"IPython.display.HTML('<h1 id=\"block\">Nothing Clicked Yet</h1>')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"ename": "NameError", | |
"evalue": "name 'current_color' is not defined", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-11-2fe19ac91680>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcurrent_color\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m: name 'current_color' is not defined" | |
] | |
} | |
], | |
"source": [ | |
"current_color" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"> After Clicking" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'firebrick'" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"current_color" | |
] | |
}, | |
{ | |
"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