Skip to content

Instantly share code, notes, and snippets.

@reszelaz
Last active October 28, 2020 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reszelaz/5692bcd98426db302abf523d5618dded to your computer and use it in GitHub Desktop.
Save reszelaz/5692bcd98426db302abf523d5618dded to your computer and use it in GitHub Desktop.
Notebook demonstrating data streaming with bokeh plots. Unfortunally I have not managed to make it show the plot before the cell finishes execution.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import nest_asyncio\n",
"nest_asyncio.apply()\n",
"\n",
"from bokeh.models import ColumnDataSource\n",
"from bokeh.layouts import row\n",
"from bokeh.plotting import figure\n",
"\n",
"document = None\n",
"callback = None\n",
"\n",
"def modify_doc(doc):\n",
" i = 1\n",
" data = {\"x_values\": [0],\n",
" \"y_values\": [0]}\n",
" source = ColumnDataSource(data=data)\n",
"\n",
" opts = dict(plot_width=250, plot_height=250, min_border=0)\n",
" p1 = figure(**opts)\n",
" p1.circle(x=\"x_values\", y=\"y_values\", source=source, size=5)\n",
" doc.add_root(row(p1))\n",
" \n",
" def update_source():\n",
" data = source.data\n",
" data[\"x_values\"].append(data[\"x_values\"][-1] + 1)\n",
" data[\"y_values\"].append(data[\"y_values\"][-1] + 1)\n",
" source.data.update(data)\n",
"\n",
" global callback\n",
" callback = doc.add_periodic_callback(update_source, 1000)\n",
" global document\n",
" document = doc"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:bokeh.server.util:Host wildcard '*' will allow connections originating from multiple (or possibly all) hostnames or IPs. Use non-wildcard values to restrict access explicitly\n"
]
}
],
"source": [
"import threading\n",
"import tornado\n",
"\n",
"from bokeh.application.handlers import FunctionHandler\n",
"from bokeh.application import Application\n",
"from bokeh.server.server import Server\n",
"\n",
"handler = FunctionHandler(modify_doc)\n",
"app = Application(handler)\n",
"io_loop = tornado.ioloop.IOLoop.current(instance=True)\n",
"\n",
"t = threading.Thread(target=io_loop.start)\n",
"# t.daemon = True\n",
"t.start()\n",
"server = Server({'/': app}, port=5002, host=[\"*\"], allow_websocket_origin=[\"*\"], io_loop=io_loop)\n",
"server.start()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<script id=\"1001\">\n",
" var xhr = new XMLHttpRequest()\n",
" xhr.responseType = 'blob';\n",
" xhr.open('GET', \"http://127.0.0.1:5002/autoload.js?bokeh-autoload-element=1001&bokeh-absolute-url=http://127.0.0.1:5002\", true);\n",
" \n",
" xhr.onload = function (event) {\n",
" var script = document.createElement('script'),\n",
" src = URL.createObjectURL(event.target.response);\n",
" script.src = src;\n",
" document.body.appendChild(script);\n",
" };\n",
"xhr.send();\n",
"</script>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:bokeh.protocol:Bad header with no msgtype was: {'events': [{'kind': 'ModelChanged', 'model': {'id': '1006'}, 'attr': 'start', 'new': -0.18761586820851694}, {'kind': 'ModelChanged', 'model': {'id': '1006'}, 'attr': 'end', 'new': 2.2067174651248167}], 'references': []}\n",
"ERROR:bokeh.server.views.ws:Bokeh Server protocol error: No 'msgtype' in header, closing connection\n",
"ERROR:bokeh.protocol:Bad header with no msgtype was: {'events': [{'kind': 'ModelChanged', 'model': {'id': '1006'}, 'attr': 'start', 'new': -0.18761586820851694}, {'kind': 'ModelChanged', 'model': {'id': '1006'}, 'attr': 'end', 'new': 2.2067174651248167}], 'references': []}\n",
"ERROR:bokeh.server.views.ws:Bokeh Server protocol error: No 'msgtype' in header, closing connection\n",
"ERROR:bokeh.protocol:Bad header with no msgtype was: {'events': [{'kind': 'ModelChanged', 'model': {'id': '1006'}, 'attr': 'start', 'new': -0.18761586820851694}, {'kind': 'ModelChanged', 'model': {'id': '1006'}, 'attr': 'end', 'new': 2.2067174651248167}], 'references': []}\n",
"ERROR:bokeh.server.views.ws:Bokeh Server protocol error: No 'msgtype' in header, closing connection\n",
"ERROR:bokeh.protocol:Bad header with no msgtype was: {'events': [{'kind': 'ModelChanged', 'model': {'id': '1006'}, 'attr': 'start', 'new': -0.18761586820851694}, {'kind': 'ModelChanged', 'model': {'id': '1006'}, 'attr': 'end', 'new': 2.2067174651248167}], 'references': []}\n",
"ERROR:bokeh.server.views.ws:Bokeh Server protocol error: No 'msgtype' in header, closing connection\n"
]
}
],
"source": [
"import time\n",
"from IPython.display import HTML, display\n",
"from bokeh.embed import server_document\n",
"script = server_document(url=\"http://127.0.0.1:5002/\")\n",
"display(HTML(script))\n",
"\n",
"for i in range(3):\n",
" time.sleep(1)\n",
"\n",
"document.remove_periodic_callback(callback)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"document.add_next_tick_callback(document.clear)\n",
"server.stop()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment