BOKEH Streaming and launching server and application from python script

Greetings:

I have been successful running your python example to stream data based on the youtube video “Introduction to Bokeh Streaming”. My question is, can you call the bokeh server from a python script or within the python code instead of calling it outside of the script via command line. Basically I want to somehow automate the python -m bokeh serve myapp.py at the end of my code. I ask because bokeh streaming will be part of a program that plots streaming pressure and temperature values from a LABJACK IO board.

Thanks.

–Vivek

Hi,

Please have a look at the new User's Guide section and linked examples for Bokeh 0.12.4 (released today):

  http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#embedding-bokeh-server-as-a-library

Thanks,

Bryan

···

On Jan 9, 2017, at 2:31 PM, [email protected] wrote:

Greetings:

I have been successful running your python example to stream data based on the youtube video "Introduction to Bokeh Streaming". My question is, can you call the bokeh server from a python script or within the python code instead of calling it outside of the script via command line. Basically I want to somehow automate the python -m bokeh serve myapp.py at the end of my code. I ask because bokeh streaming will be part of a program that plots streaming pressure and temperature values from a LABJACK IO board.

Thanks.

--Vivek

--
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/9d091c44-635d-4a26-8ead-63aab3d03d29%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Thanks Bryan…BTW the project we are working on is for a thin film reactor that we are open source controlling here at NASA and thus utilizing BOKEH to stream pressure and temperature data. I will have a few more questions but I will post them to the forum.

–Vivek

···

On Mon, Jan 9, 2017 at 3:39 PM, Bryan Van de Ven [email protected] wrote:

Hi,

Please have a look at the new User’s Guide section and linked examples for Bokeh 0.12.4 (released today):

    [http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#embedding-bokeh-server-as-a-library](http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#embedding-bokeh-server-as-a-library)

Thanks,

Bryan

On Jan 9, 2017, at 2:31 PM, [email protected] wrote:

Greetings:

I have been successful running your python example to stream data based on the youtube video “Introduction to Bokeh Streaming”. My question is, can you call the bokeh server from a python script or within the python code instead of calling it outside of the script via command line. Basically I want to somehow automate the python -m bokeh serve myapp.py at the end of my code. I ask because bokeh streaming will be part of a program that plots streaming pressure and temperature values from a LABJACK IO board.

Thanks.

–Vivek

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/9d091c44-635d-4a26-8ead-63aab3d03d29%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/DD20EB25-F707-4CEE-9040-62C2D7D91C2C%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Vivek,

That sounds really interesting! I'd love to collaborate on a blog post or some kind of write-up at some point, if that is an option!

Thanks,

Bryan

···

On Jan 10, 2017, at 8:30 AM, Vivek Dwivedi <[email protected]> wrote:

Thanks Bryan...BTW the project we are working on is for a thin film reactor that we are open source controlling here at NASA and thus utilizing BOKEH to stream pressure and temperature data. I will have a few more questions but I will post them to the forum.

--Vivek

On Mon, Jan 9, 2017 at 3:39 PM, Bryan Van de Ven <[email protected]> wrote:
Hi,

Please have a look at the new User's Guide section and linked examples for Bokeh 0.12.4 (released today):

        http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#embedding-bokeh-server-as-a-library

Thanks,

Bryan

> On Jan 9, 2017, at 2:31 PM, [email protected] wrote:
>
> Greetings:
>
> I have been successful running your python example to stream data based on the youtube video "Introduction to Bokeh Streaming". My question is, can you call the bokeh server from a python script or within the python code instead of calling it outside of the script via command line. Basically I want to somehow automate the python -m bokeh serve myapp.py at the end of my code. I ask because bokeh streaming will be part of a program that plots streaming pressure and temperature values from a LABJACK IO board.
>
> Thanks.
>
> --Vivek
>
>
>
>
>
> --
> 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/9d091c44-635d-4a26-8ead-63aab3d03d29%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/DD20EB25-F707-4CEE-9040-62C2D7D91C2C%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/CAF%3DFfxDmF-%2Bp%2BpPTjxpsod2h1VU%2BhMoSnu46DbJe3n73WMa8Fw%40mail.gmail.com.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Hi Bryan:

I am not getting the embedded application to work right, I basically want to automattically open a browswer with my data being streamed right to it…below is the code… yes let me talk to legal to see how we can proceed further, I did notice NIST was using Bokeh as well and they did a write up on it.

from bokeh.io import curdoc
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, output_file, show
import numpy as np
from bokeh.server.server import Server
from tornado.ioloop import IOLoop
from tornado.web import RequestHandler
from bokeh.application import Application
from bokeh.application.handlers import FunctionHandler

io_loop = IOLoop.current()

source = ColumnDataSource(dict(x = , y = , avg = ))

fig = figure()

fig.line(source=source, x=‘x’, y=‘y’, line_width = 2, alpha = .85, color =‘red’)
fig.line(source=source, x=‘x’, y=‘avg’, line_width = 2, alpha = .85, color =‘blue’)

ct = 0
sine_sum = 0

def update_Data(doc):
global ct, sine_sum
ct += 1
sine = np.sin(ct)
sine_sum += sine
new_data = dict(x = [ct], y = [sine], avg = [sine_sum/ct])
source.stream(new_data,100)

curdoc().add_root(fig)
curdoc().add_periodic_callback(update_Data,100)

bokeh_app = Application(FunctionHandler(update_Data))

server = Server({’/’: bokeh_app}, io_loop=io_loop)
server.start()
#usage
#vivekd@Lumbergh:~/Desktop$ python -m bokeh serve --show main.py

if name == ‘main’:
print(‘Opening Bokeh application on http://localhost:5006/’)

io_loop.add_callback(server.show, "/")
io_loop.start()
···

On Tue, Jan 10, 2017 at 9:49 AM, Bryan Van de Ven [email protected] wrote:

Vivek,

That sounds really interesting! I’d love to collaborate on a blog post or some kind of write-up at some point, if that is an option!

Thanks,

Bryan

On Jan 10, 2017, at 8:30 AM, Vivek Dwivedi [email protected] wrote:

Thanks Bryan…BTW the project we are working on is for a thin film reactor that we are open source controlling here at NASA and thus utilizing BOKEH to stream pressure and temperature data. I will have a few more questions but I will post them to the forum.

–Vivek

On Mon, Jan 9, 2017 at 3:39 PM, Bryan Van de Ven [email protected] wrote:

Hi,

Please have a look at the new User’s Guide section and linked examples for Bokeh 0.12.4 (released today):

    [http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#embedding-bokeh-server-as-a-library](http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#embedding-bokeh-server-as-a-library)

Thanks,

Bryan

On Jan 9, 2017, at 2:31 PM, [email protected] wrote:

Greetings:

I have been successful running your python example to stream data based on the youtube video “Introduction to Bokeh Streaming”. My question is, can you call the bokeh server from a python script or within the python code instead of calling it outside of the script via command line. Basically I want to somehow automate the python -m bokeh serve myapp.py at the end of my code. I ask because bokeh streaming will be part of a program that plots streaming pressure and temperature values from a LABJACK IO board.

Thanks.

–Vivek

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/9d091c44-635d-4a26-8ead-63aab3d03d29%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/DD20EB25-F707-4CEE-9040-62C2D7D91C2C%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/CAF%3DFfxDmF-%2Bp%2BpPTjxpsod2h1VU%2BhMoSnu46DbJe3n73WMa8Fw%40mail.gmail.com.

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/547E537B-5363-466B-9EB5-1FE6B30294D5%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

I should also mention when I run the code below I do not get an error but my browser does not automatically launch and the data is not automatically streamed.

Thanks.

Code Below...

from bokeh.io import curdoc
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, output_file, show
import numpy as np
from bokeh.server.server import Server
from tornado.ioloop import IOLoop
from tornado.web import RequestHandler
from bokeh.application import Application
from bokeh.application.handlers import FunctionHandler

io_loop = IOLoop.current()

source = ColumnDataSource(dict(x = , y = , avg = ))

fig = figure()

fig.line(source=source, x='x', y='y', line_width = 2, alpha = .85, color ='red')
fig.line(source=source, x='x', y='avg', line_width = 2, alpha = .85, color ='blue')

ct = 0
sine_sum = 0

def update_Data(doc):
    global ct, sine_sum
    ct += 1
    sine = np.sin(ct)
    sine_sum += sine
    new_data = dict(x = [ct], y = [sine], avg = [sine_sum/ct])
    source.stream(new_data,100)
    
curdoc().add_root(fig)
curdoc().add_periodic_callback(update_Data,100)

bokeh_app = Application(FunctionHandler(update_Data))

server = Server({'/': bokeh_app}, io_loop=io_loop)
server.start()
#usage
#vivekd@Lumbergh:~/Desktop$ python -m bokeh serve --show main.py

if __name__ == '__main__':
    print('Opening Bokeh application on http://localhost:5006/')

    io_loop.add_callback(server.show, "/")
    io_loop.start()

Hi,

It looks like I need to document / explain some things better. First, the point of this kind of embedding is to be able to run it like a regular python script, e.g.:

  python app.py

and NOT have to use "bokeh serve". This has been asked for by an number of folks. Next I need to explain that the function that gets passed to FunctionHandler needs to build the *entire* app, not just be one callback. With that in mind, here is your example updated to work. I had to make some small changes around the callback, since "global" does not have the same meaning for a function inside a function:

    from bokeh.models import ColumnDataSource
    import numpy as np
    from bokeh.server.server import Server
    from tornado.ioloop import IOLoop
    from bokeh.application import Application
    from bokeh.application.handlers import FunctionHandler
    from bokeh.driving import count

    io_loop = IOLoop.current()

    def build_doc(doc):
        source = ColumnDataSource(dict(x=, y=, avg=))

        fig = figure()

        fig.line(source=source, x='x', y='y', line_width=2, alpha=.85, color='red')
        fig.line(source=source, x='x', y='avg', line_width=2, alpha=.85, color='blue')

        @count()
        def update_Data(ct):
            sine = np.sin(ct)
            sine_sum = sum(source.data['y']) + sine
            new_data = dict(x=[ct], y=[sine], avg=[sine_sum/(ct+1)])
            source.stream(new_data, 100)

        doc.add_root(fig)
        doc.add_periodic_callback(update_Data, 100)

    bokeh_app = Application(FunctionHandler(build_doc))

    server = Server({'/': bokeh_app}, io_loop=io_loop)
    server.start()

    if __name__ == '__main__':
        print('Opening Bokeh application on http://localhost:5006/')

        io_loop.add_callback(server.show, "/")
        io_loop.start()

Thanks,

Bryan

···

On Jan 10, 2017, at 2:13 PM, Vivek Dwivedi <[email protected]> wrote:

I should also mention when I run the code below I do not get an error but my browser does not automatically launch and the data is not automatically streamed.

Thanks.

Code Below...

from bokeh.io import curdoc
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, output_file, show
import numpy as np
from bokeh.server.server import Server
from tornado.ioloop import IOLoop
from tornado.web import RequestHandler
from bokeh.application import Application
from bokeh.application.handlers import FunctionHandler

io_loop = IOLoop.current()

source = ColumnDataSource(dict(x = , y = , avg = ))

fig = figure()

fig.line(source=source, x='x', y='y', line_width = 2, alpha = .85, color ='red')
fig.line(source=source, x='x', y='avg', line_width = 2, alpha = .85, color ='blue')

ct = 0
sine_sum = 0

def update_Data(doc):
   global ct, sine_sum
   ct += 1
   sine = np.sin(ct)
   sine_sum += sine
   new_data = dict(x = [ct], y = [sine], avg = [sine_sum/ct])
   source.stream(new_data,100)

curdoc().add_root(fig)
curdoc().add_periodic_callback(update_Data,100)

bokeh_app = Application(FunctionHandler(update_Data))

server = Server({'/': bokeh_app}, io_loop=io_loop)
server.start()
#usage
#vivekd@Lumbergh:~/Desktop$ python -m bokeh serve --show main.py

if __name__ == '__main__':
   print('Opening Bokeh application on http://localhost:5006/')

   io_loop.add_callback(server.show, "/")
   io_loop.start()

--
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/3d507311-1e73-4739-bb0f-4b669809beb5%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.