Create server application programmatically

I would like to run a Bokeh Server application alongside another Tornado application (a Dask worker), sharing the same IOLoop. I’ve gone through the logic in bokeh/command/subcommands/serve.py and the bokeh Server object. From what I can tell there are two options:

  1. Use the command infrastructure. This will parse arguments for me, set up application objects, handlers, etc. and plug them into a server and then start that server. Unfortunately this assumes that it’s going to start and control the IOLoop itself with no other applications active.
  2. Build things manually. It appears that I want to create a bunch of applications with handlers, connect them up to a Server object and then finally call the start method of that object’s BokehTornado with start_loop=False. Unfortunately, there is a large amount of manual work here that is pretty error prone.
    From a cursory glance it seems like there is a lot of very useful logic that is tied to using the command line interface. This makes sense given that it’s the common case, but it’s currently stopping me from using Bokeh server more widely throughout my application. Is there a clean way around this? If not then should I raise an issue?

With a few changes to Bokeh I can get something working without much pain: https://github.com/bokeh/bokeh/pull/5486

···

On Sat, Nov 26, 2016 at 1:03 PM, Matthew Rocklin [email protected] wrote:

  1. Use the command infrastructure. This will parse arguments for me, set up application objects, handlers, etc. and plug them into a server and then start that server. Unfortunately this assumes that it’s going to start and control the IOLoop itself with no other applications active.
  2. Build things manually. It appears that I want to create a bunch of applications with handlers, connect them up to a Server object and then finally call the start method of that object’s BokehTornado with start_loop=False. Unfortunately, there is a large amount of manual work here that is pretty error prone.
    From a cursory glance it seems like there is a lot of very useful logic that is tied to using the command line interface. This makes sense given that it’s the common case, but it’s currently stopping me from using Bokeh server more widely throughout my application. Is there a clean way around this? If not then should I raise an issue?

I would like to run a Bokeh Server application alongside another Tornado application (a Dask worker), sharing the same IOLoop. I’ve gone through the logic in bokeh/command/subcommands/serve.py and the bokeh Server object. From what I can tell there are two options:

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

To unsubscribe from this topic, visit https://groups.google.com/a/continuum.io/d/topic/bokeh/iupkKzzUNqU/unsubscribe.

To unsubscribe from this group and all its topics, 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/b25ab754-114e-4359-a773-dd8bbc38c009%40continuum.io.

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

Matt,

Have you seen this example of sharing an IOLoop?

https://groups.google.com/a/continuum.io/forum/m/#!topic/bokeh/LYmjTXzX43E

Hope that is what you are looking for.

Best,
Sean

Hi Sean,

Thanks! The bokeh.command.util.build_single_handler_applications was just the function I was looking for. Here is a minimal example:

from bokeh.server.server import Server

from bokeh.command.util import build_single_handler_applications

apps = build_single_handler_applications([‘examples/app/crossfilter/’], {})

from tornado.ioloop import IOLoop

loop = IOLoop()

server = Server(apps, io_loop=loop)

server.start(start_loop=False)

assert server.io_loop is loop

assert server.io_loop._running

server.io_loop.start()

-matt

···

On Sat, Nov 26, 2016 at 9:21 PM, [email protected] wrote:

Matt,

Have you seen this example of sharing an IOLoop?

https://groups.google.com/a/continuum.io/forum/m/#!topic/bokeh/LYmjTXzX43E

Hope that is what you are looking for.

Best,

Sean

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

To unsubscribe from this topic, visit https://groups.google.com/a/continuum.io/d/topic/bokeh/iupkKzzUNqU/unsubscribe.

To unsubscribe from this group and all its topics, 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/81454075-d0ca-49b0-86ad-a7591964dd3d%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

In general creating an app is just:

  app = Application(handler) # or a list of handlers but usually just one

where a handler is a Handler subclass that modifies a document (passed into a .modify_document method by the Server when a session is initiated). There are currently handlers the e.g. use python code in a script, or a directory to modify documents. Possibly also of interest, there is a FunctionHandler that will accept an arbitrary function do the document modifications:

  https://github.com/bokeh/bokeh/blob/master/bokeh/application/handlers/function.py

which is used like:

  app = Application(FunctionHandler(my_func))

The function my_func could check sys.argv, or could be a closure that bundles a programmatically specified "argv" or whatever.

FunctionHandler is maintained under test but I'm not aware of any examples of its use (and I notice that some sub-packages like bokeh.application are accidentally missing from the reference guide).

Bryan

···

On Nov 27, 2016, at 8:57 AM, Matthew Rocklin <[email protected]> wrote:

Hi Sean,

Thanks! The bokeh.command.util.build_single_handler_applications was just the function I was looking for. Here is a minimal example:

from bokeh.server.server import Server
from bokeh.command.util import build_single_handler_applications
apps = build_single_handler_applications(['examples/app/crossfilter/'], {})
from tornado.ioloop import IOLoop
loop = IOLoop()
server = Server(apps, io_loop=loop)
server.start(start_loop=False)
assert server.io_loop is loop
assert server.io_loop._running
server.io_loop.start()

-matt

On Sat, Nov 26, 2016 at 9:21 PM, <[email protected]> wrote:
Matt,

Have you seen this example of sharing an IOLoop?

Redirecting to Google Groups

Hope that is what you are looking for.

Best,
Sean

--
You received this message because you are subscribed to a topic in the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this topic, visit https://groups.google.com/a/continuum.io/d/topic/bokeh/iupkKzzUNqU/unsubscribe\.
To unsubscribe from this group and all its topics, 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/81454075-d0ca-49b0-86ad-a7591964dd3d%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/CAJ8oX-Hpiv48ofbiXoV%2BjkYmRtAdM-PGSZTTziLJJOBb1EG0TA%40mail.gmail.com\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.