How to send plot updates to the Bokeh server?

In the Bokeh streaming ohlc example this line;

curdoc().add_periodic_callback(update, 50)

is used to generate synthetic data and update the Bokeh server plot on a timed callback every 50 milliseconds.

In the real world the plot would need to be updated as-and-when new data arrived from the market. What Bokeh commands/calls could be used to send new data updates to the plot/server in an event driven manner?

e.g.

def market_update(dt, o, h, l, c):

new_data = dict(

time=[dt],

open=[o],

high=[h],

low=[l],

close=[c],

)

curdoc().update_and_display_modified_plot(new_data) # I made this up :wink:

Thanks,

GoFaster

Hi,

To handle external async events (e.g. updates from Redis or a ZMQ socket handler, or whatever) you'll want to fillow the same general advice in the "Updating From Threads" section of the User's Guide:

  http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#updating-from-threads

Which is to say, whatever event handler receives the new data, it should schedule a "next_tick_callback" to safely modify the Bokeh document from.

Thanks,

Bryan

···

On Aug 24, 2016, at 8:27 AM, GoFaster <[email protected]> wrote:

In the Bokeh streaming ohlc example this line;

curdoc().add_periodic_callback(update, 50)

is used to generate synthetic data and update the Bokeh server plot on a timed callback every 50 milliseconds.

In the real world the plot would need to be updated as-and-when new data arrived from the market. What Bokeh commands/calls could be used to send new data updates to the plot/server in an event driven manner?

e.g.

def market_update(dt, o, h, l, c):
    new_data = dict(
        time=[dt],
        open=[o],
        high=[h],
        low=[l],
        close=[c],
    )
    curdoc().update_and_display_modified_plot(new_data) # I made this up :wink:

Thanks,

GoFaster

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/27fa0685-a579-4708-a95a-7e26b66dc3e5%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

As proof-of-concept I’ve created an ohlc_RT demonstration piece which updates in real time with fractally synthesised ohlc data. I’m having issues with the stream and patch calls. The code contains a Chart class which upon instantiation plots a chunk of fractal synthesised ohlc historical data to the chart; next as real time data is received and a candlestick closes then it is streamed to the Bokeh server. The first call to stream will plot and extend the output on the chart but after that their is no further streaming. I haven’t seen patch operate yet. It runs using ‘bokeh serve --show ohlc_RT’ like the other Bokeh examples. I suspect that something is not correct in my implementation of add_next_tick_callback()

http://pastebin.com/Zp7BDCRn

Anyone have any tips to get this working as intended?

Thanks

GoFaster

···

On Wednesday, 24 August 2016 14:40:31 UTC+1, Bryan Van de ven wrote:

Hi,

To handle external async events (e.g. updates from Redis or a ZMQ socket handler, or whatever) you’ll want to fillow the same general advice in the “Updating From Threads” section of the User’s Guide:

    [http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#updating-from-threads](http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#updating-from-threads)

Which is to say, whatever event handler receives the new data, it should schedule a “next_tick_callback” to safely modify the Bokeh document from.

Thanks,

Bryan

On Aug 24, 2016, at 8:27 AM, GoFaster [email protected] wrote:

In the Bokeh streaming ohlc example this line;

curdoc().add_periodic_callback(update, 50)

is used to generate synthetic data and update the Bokeh server plot on a timed callback every 50 milliseconds.

In the real world the plot would need to be updated as-and-when new data arrived from the market. What Bokeh commands/calls could be used to send new data updates to the plot/server in an event driven manner?

e.g.

def market_update(dt, o, h, l, c):

new_data = dict(
    time=[dt],
    open=[o],
    high=[h],
    low=[l],
    close=[c],        
)
curdoc().update_and_display_modified_plot(new_data) # I made this up ;-)

Thanks,

GoFaster


You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/27fa0685-a579-4708-a95a-7e26b66dc3e5%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Hi,

Unfortunately the example in the pastebin is far too large and complicated to dig deeply into, it would take several hours at least just to try and tease out the relevant Bokeh parts from the domain-specific bits. However I was able to quickly create to a minimal app that I believe distills all the relevant Bokeh functionality you are looking for: every once in a while, stream a new "last" point, and in between those times, also update the last point in place. It's often good to start from a small working example and build up from there, so I hope this will be useful to you in that regard.

Thanks,

Bryan

···

#---------------------------------------------
# patch_stream.py

from functools import partial
from random import random
from threading import Thread
import time

from bokeh.models import ColumnDataSource
from bokeh.plotting import curdoc, figure

from tornado import gen

# this must only be modified from a Bokeh session allback
source = ColumnDataSource(data=dict(x=[0], y=[0]))

# This is important! Save curdoc() to make sure all threads
# see then same document.
doc = curdoc()

idx = 0
count = 0

@gen.coroutine
def stream_update(x, y):
    source.stream(dict(x=, y=[y]), rollover=500)

@gen.coroutine
def patch_update(idx, y):
    source.patch(dict(y=[(idx, y)]))

def blocking_task():
    global count, idx
    while True:
        time.sleep(0.2)

        y = random()

        count +=1

        # every fifth update, stream a new last point
        if count % 5 == 0:
            idx += 1
            x = idx/2.0
            doc.add_next_tick_callback(partial(stream_update, x=x, y=y))

        # other times, update the existing last point
        else:
            doc.add_next_tick_callback(partial(patch_update, idx=idx, y=y))

p = figure(y_range=[0,1])
l = p.circle(x='x', y='y', size=10, source=source)

doc.add_root(p)

thread = Thread(target=blocking_task)
thread.start()

On Sep 1, 2016, at 12:10 PM, [email protected]l.com wrote:

As proof-of-concept I've created an ohlc_RT demonstration piece which updates in real time with fractally synthesised ohlc data. I'm having issues with the stream and patch calls. The code contains a Chart class which upon instantiation plots a chunk of fractal synthesised ohlc historical data to the chart; next as real time data is received and a candlestick closes then it is streamed to the Bokeh server. The first call to stream will plot and extend the output on the chart but after that their is no further streaming. I haven't seen patch operate yet. It runs using 'bokeh serve --show ohlc_RT' like the other Bokeh examples. I suspect that something is not correct in my implementation of add_next_tick_callback()

http://pastebin.com/Zp7BDCRn

Anyone have any tips to get this working as intended?

Thanks

GoFaster

On Wednesday, 24 August 2016 14:40:31 UTC+1, Bryan Van de ven wrote:
Hi,

To handle external async events (e.g. updates from Redis or a ZMQ socket handler, or whatever) you'll want to fillow the same general advice in the "Updating From Threads" section of the User's Guide:

        http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#updating-from-threads

Which is to say, whatever event handler receives the new data, it should schedule a "next_tick_callback" to safely modify the Bokeh document from.

Thanks,

Bryan

> On Aug 24, 2016, at 8:27 AM, GoFaster <[email protected]> wrote:
>
> In the Bokeh streaming ohlc example this line;
>
> curdoc().add_periodic_callback(update, 50)
>
> is used to generate synthetic data and update the Bokeh server plot on a timed callback every 50 milliseconds.
>
> In the real world the plot would need to be updated as-and-when new data arrived from the market. What Bokeh commands/calls could be used to send new data updates to the plot/server in an event driven manner?
>
> e.g.
>
> def market_update(dt, o, h, l, c):
> new_data = dict(
> time=[dt],
> open=[o],
> high=[h],
> low=[l],
> close=[c],
> )
> curdoc().update_and_display_modified_plot(new_data) # I made this up :wink:
>
> Thanks,
>
> GoFaster
>
>
> --
> You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/27fa0685-a579-4708-a95a-7e26b66dc3e5%40continuum.io.
> For more options, visit https://groups.google.com/a/continuum.io/d/optout.

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/035a21cd-0329-49c2-8497-c52ad6fd664b%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Bryan,

I’ve now created a functional patching and streaming OHLC chart based on fractal data it’s available at: https://github.com/Formulator/candlestickmaker

Code polishing & enhancement contributions welcome!

···

Very many thanks for your assistance,

GoFaster