launch Server from inside python application

Currently, I am able to serve the following animation using ‘bokeh serve’ and connect to it by executing the python file.
#!/usr/bin/env python
from bokeh.models import Range1d
from bokeh.plotting import figure, show
from bokeh.client import push_session
from bokeh.io import curdoc
import random
import math
if name == “main”:
session = push_session(curdoc())

n = 5
p = figure(x_range=Range1d(-n-1, n+1), y_range=Range1d(-n-1, n+1))
p.circle([0], [0], line_color='black', fill_color=None, radius=n)
p.circle([n], [n], color='red', radius=0.3, name='ball')

def update(n=5):
    ball = p.select_one({'name': 'ball'})
   
    x = random.uniform(-n, n)
    y = random.choice([-1.0, 1.0]) * math.sqrt(n*n-x*x)
   
    ball.data_source.data = {'x': [x], 'y': [y]}
   
curdoc().add_periodic_callback(update, 100)
session.show(p)
session.loop_until_closed()

According to the documentation and a post here, it is possible to launch the Server(bokeh.server.server.Server) from inside the file so that executing the file will be the only thing that is necessary (i.e. ‘bokeh serve’ is not needed). I know it is possible to access some of the Server elements directly using ServerContext etc, but I am missing how it might be possible to serve from within an app.
Ultimately, I want to embed an animation like this that requires the Server inside of a Flask app, without ‘bokeh serve’. Additionally, I’d like to only run ONE Tornado server for both Flask and Bokeh. How do I connect the two together?

Ari,

I'm not sure I follow. The post you linked seems to state the opposite: that a separate process is probably always needed. The exception might be if you were creating a Tornado app to begin with, where (possibly) the IOLoop could be shared. But that is not the case here, you are not going to get away from needing to run "bokeh serve" either as a part of your deployment automation, or perhaps using subprocess from within Flask somehow.

Bryan

···

On Aug 9, 2016, at 12:09 PM, Ari Krumbein <[email protected]> wrote:

Currently, I am able to serve the following animation using 'bokeh serve' and connect to it by executing the python file.

#!/usr/bin/env python

from bokeh.models import Range1d
from bokeh.plotting import figure, show
from bokeh.client import push_session
from bokeh.io import curdoc
import random
import math

if __name__ == "__main__":

    session = push_session(curdoc())
    
    n = 5
    p = figure(x_range=Range1d(-n-1, n+1), y_range=Range1d(-n-1, n+1))
    p.circle([0], [0], line_color='black', fill_color=None, radius=n)
    p.circle([n], [n], color='red', radius=0.3, name='ball')
    
    def update(n=5):
        ball = p.select_one({'name': 'ball'})
        
        x = random.uniform(-n, n)
        y = random.choice([-1.0, 1.0]) * math.sqrt(n*n-x*x)
        
        ball.data_source.data = {'x': , 'y': [y]}
        
    curdoc().add_periodic_callback(update, 100)
    session.show(p)
    session.loop_until_closed()

According to the documentation and a post here, it is possible to launch the Server(bokeh.server.server.Server) from inside the file so that executing the file will be the only thing that is necessary (i.e. 'bokeh serve' is not needed). I know it is possible to access some of the Server elements directly using ServerContext etc, but I am missing how it might be possible to serve from within an app.

Ultimately, I want to embed an animation like this that requires the Server inside of a Flask app, without 'bokeh serve'. Additionally, I'd like to only run ONE Tornado server for both Flask and Bokeh. How do I connect the two together?

--
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/aa89aa51-59c5-426e-925b-9cf583f4f147%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

Bryan,
In regards to sharing the IOLoop between Flask and Bokeh, here is a trivial example of a Flask/Tornado setup,
#!/usr/bin/env python
from flask import Flask
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
app = Flask(name)
@app.route(‘/’)
def hello_world():
return ‘Hello, World!’
if name == ‘main’:
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(8888)
IOLoop.instance().start()
How would I then integrate the original example (the animation) into this app? Would I call all of the code, including push_session, in the app route? I saw that push_session has an optional parameter for an IOLoop, which I assume I would provide from the instance I create in that last line, so as not to create a second one.

Ari

···

On Tuesday, August 9, 2016 at 1:29:38 PM UTC-4, Bryan Van de ven wrote:

Ari,

I’m not sure I follow. The post you linked seems to state the opposite: that a separate process is probably always needed. The exception might be if you were creating a Tornado app to begin with, where (possibly) the IOLoop could be shared. But that is not the case here, you are not going to get away from needing to run “bokeh serve” either as a part of your deployment automation, or perhaps using subprocess from within Flask somehow.

Bryan

On Aug 9, 2016, at 12:09 PM, Ari Krumbein [email protected] wrote:

Currently, I am able to serve the following animation using ‘bokeh serve’ and connect to it by executing the python file.

#!/usr/bin/env python

from bokeh.models import Range1d

from bokeh.plotting import figure, show

from bokeh.client import push_session

from bokeh.io import curdoc

import random

import math

if name == “main”:

session = push_session(curdoc())
n = 5
p = figure(x_range=Range1d(-n-1, n+1), y_range=Range1d(-n-1, n+1))
p.circle([0], [0], line_color='black', fill_color=None, radius=n)
p.circle([n], [n], color='red', radius=0.3, name='ball')
def update(n=5):
    ball = p.select_one({'name': 'ball'})
    x = random.uniform(-n, n)
    y = random.choice([-1.0, 1.0]) * math.sqrt(n*n-x*x)
    ball.data_source.data = {'x': [x], 'y': [y]}
curdoc().add_periodic_callback(update, 100)
session.show(p)
session.loop_until_closed()

According to the documentation and a post here, it is possible to launch the Server(bokeh.server.server.Server) from inside the file so that executing the file will be the only thing that is necessary (i.e. ‘bokeh serve’ is not needed). I know it is possible to access some of the Server elements directly using ServerContext etc, but I am missing how it might be possible to serve from within an app.

Ultimately, I want to embed an animation like this that requires the Server inside of a Flask app, without ‘bokeh serve’. Additionally, I’d like to only run ONE Tornado server for both Flask and Bokeh. How do I connect the two together?


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/aa89aa51-59c5-426e-925b-9cf583f4f147%40continuum.io.

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

Ari,

I only had in mind a pure Tornado app that might "integrate" a Bokeh server into itself. I have no experience that would lead me to believe combining Flask and Tornado in this way will succeed. The intended usage of the Bokeh server is as a separate process. If you manage to succeed with something like this, we would be very keen to hear about it, and perhaps add an example or documentation, but it's nothing I can give any concrete guidance on/

Bryan

···

On Aug 9, 2016, at 1:38 PM, Ari Krumbein <[email protected]> wrote:

Bryan,

In regards to sharing the IOLoop between Flask and Bokeh, here is a trivial example of a Flask/Tornado setup,

#!/usr/bin/env python

from flask import Flask
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    http_server = HTTPServer(WSGIContainer(app))
    http_server.listen(8888)
    IOLoop.instance().start()

How would I then integrate the original example (the animation) into this app? Would I call all of the code, including push_session, in the app route? I saw that push_session has an optional parameter for an IOLoop, which I assume I would provide from the instance I create in that last line, so as not to create a second one.

Ari

On Tuesday, August 9, 2016 at 1:29:38 PM UTC-4, Bryan Van de ven wrote:
Ari,

I'm not sure I follow. The post you linked seems to state the opposite: that a separate process is probably always needed. The exception might be if you were creating a Tornado app to begin with, where (possibly) the IOLoop could be shared. But that is not the case here, you are not going to get away from needing to run "bokeh serve" either as a part of your deployment automation, or perhaps using subprocess from within Flask somehow.

Bryan

> On Aug 9, 2016, at 12:09 PM, Ari Krumbein <[email protected]> wrote:
>
> Currently, I am able to serve the following animation using 'bokeh serve' and connect to it by executing the python file.
>
> #!/usr/bin/env python
>
> from bokeh.models import Range1d
> from bokeh.plotting import figure, show
> from bokeh.client import push_session
> from bokeh.io import curdoc
> import random
> import math
>
> if __name__ == "__main__":
>
> session = push_session(curdoc())
>
> n = 5
> p = figure(x_range=Range1d(-n-1, n+1), y_range=Range1d(-n-1, n+1))
> p.circle([0], [0], line_color='black', fill_color=None, radius=n)
> p.circle([n], [n], color='red', radius=0.3, name='ball')
>
> def update(n=5):
> ball = p.select_one({'name': 'ball'})
>
> x = random.uniform(-n, n)
> y = random.choice([-1.0, 1.0]) * math.sqrt(n*n-x*x)
>
> ball.data_source.data = {'x': , 'y': [y]}
>
> curdoc().add_periodic_callback(update, 100)
> session.show(p)
> session.loop_until_closed()
>
> According to the documentation and a post here, it is possible to launch the Server(bokeh.server.server.Server) from inside the file so that executing the file will be the only thing that is necessary (i.e. 'bokeh serve' is not needed). I know it is possible to access some of the Server elements directly using ServerContext etc, but I am missing how it might be possible to serve from within an app.
>
>
> Ultimately, I want to embed an animation like this that requires the Server inside of a Flask app, without 'bokeh serve'. Additionally, I'd like to only run ONE Tornado server for both Flask and Bokeh. How do I connect the two together?
>
>
> --
> 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 bokeh+un...@continuum.io.
> To post to this group, send email to bo...@continuum.io.
> To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/aa89aa51-59c5-426e-925b-9cf583f4f147%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/8b133970-382a-42ab-afba-d7aad794cdb1%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

Ari,

I think Adam Hubbell figured it out! See the example below.

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

Best,

Sean

···

On Tuesday, August 9, 2016 at 1:09:37 PM UTC-4, Ari Krumbein wrote:

Currently, I am able to serve the following animation using ‘bokeh serve’ and connect to it by executing the python file.
#!/usr/bin/env python
from bokeh.models import Range1d
from bokeh.plotting import figure, show
from bokeh.client import push_session
from bokeh.io import curdoc
import random
import math
if name == “main”:
session = push_session(curdoc())

n = 5
p = figure(x_range=Range1d(-n-1, n+1), y_range=Range1d(-n-1, n+1))
p.circle([0], [0], line_color='black', fill_color=None, radius=n)
p.circle([n], [n], color='red', radius=0.3, name='ball')

def update(n=5):
    ball = p.select_one({'name': 'ball'})
   
    x = random.uniform(-n, n)
    y = random.choice([-1.0, 1.0]) * math.sqrt(n*n-x*x)
   
    ball.data_source.data = {'x': [x], 'y': [y]}
   
curdoc().add_periodic_callback(update, 100)
session.show(p)
session.loop_until_closed()

According to the documentation and a post here, it is possible to launch the Server(bokeh.server.server.Server) from inside the file so that executing the file will be the only thing that is necessary (i.e. ‘bokeh serve’ is not needed). I know it is possible to access some of the Server elements directly using ServerContext etc, but I am missing how it might be possible to serve from within an app.
Ultimately, I want to embed an animation like this that requires the Server inside of a Flask app, without ‘bokeh serve’. Additionally, I’d like to only run ONE Tornado server for both Flask and Bokeh. How do I connect the two together?