With the server running in the background, let us implement the real-time data update
routine:
In [ 58 ]: import bokeh.plotting as bp
from bokeh.objects import Glyph
Before any updating takes place, there needs to be an object to be updated. This again is a
line plot — if only with very little data at first. The output is directed to the IPython
Notebook the code is executed in. However, in fact it is redirected again to the server,
which in this case can be accessed locally via http://localhost:5006/:
In [ 59 ]: bp.output_notebook(“default”)
bp.line(ticks[‘time’], ticks[‘bid’],
x_axis_type=‘datetime’, legend=instrument)
Out[59]: Using saved session configuration for http://localhost:5006/
To override, pass ‘load_from_config=False’ to Session
<bokeh.objects.Plot at 0x7fdb7e1b2e10>
We need to get access to our current plot (i.e., the most recently generated plot). Calling
the function curplot returns the object we are looking for:
In [ 60 ]: bp.curplot()
Out[60]: <bokeh.objects.Plot at 0x7fdb7e1b2e10>
Such a Plot object consists of a number of rendering objects that accomplish different
plotting tasks, like plotting a Grid or plotting the line (= Glyph) representing the financial
data. All rendering objects are stored in a list attribute called renderers:
In [ 61 ]: bp.curplot().renderers
Out[61]: [<bokeh.objects.DatetimeAxis at 0x7fdbaece6b50>,
<bokeh.objects.Grid at 0x7fdb7e161190>,
<bokeh.objects.LinearAxis at 0x7fdb7e161090>,
<bokeh.objects.Grid at 0x7fdb7e1614d0>,
<bokeh.objects.BoxSelectionOverlay at 0x7fdb7e161490>,
<bokeh.objects.BoxSelectionOverlay at 0x7fdb7e161550>,
<bokeh.objects.Legend at 0x7fdb7e161650>,
<bokeh.objects.Glyph at 0x7fdb7e161610>]
The following list comprehension returns the first rendering object of type Glyph:
In [ 62 ]: renderer = [r for r in bp.curplot().renderers
if isinstance(r, Glyph)][ 0 ]
The glyph attribute of the object contains the type of the Glyph object — in this case, as
expected, a Line object:
In [ 63 ]: renderer.glyph
Out[63]: <bokeh.glyphs.Line at 0x7fdb7e161590>
With the rendering object, we can access its data source directly:
In [ 64 ]: renderer.data_source
Out[64]: <bokeh.objects.ColumnDataSource at 0x7fdb7e1b2ed0>
In [ 65 ]: renderer.data_source.data
Out[65]: {‘x’: 2014-09-29 06:14:34.749878+00:00 2014-09-29 06:14:34.749878+00
:00
Name: time, dtype: object, ‘y’: 2014-09-29 06:14:34.749878+00:00 1.
2582
Name: bid, dtype: float64}
In [ 66 ]: ds = renderer.data_source
This is the object that we will work with and that is to be updated whenever new data
arrives. The following while loop runs for a predetermined period of time only. During
the loop, a new request object is generated and the JSON data is read. The new data is