An Example to Run a Bokeh Server Programmatically

1666 views
Skip to first unread message

adam...@gmail.com

unread,
Aug 11, 2016, 10:32:12 AM8/11/16
to Bokeh Discussion - Public
Working on Windows 7 with Bokeh 0.12.1. Runs a Bokeh Server by creating a Tornado io_loop, sending that loop to bokeh.server.server.Server then running the io_loop. Embeds two bokeh servers in a Flask app. Check it out!
serve_in_code.zip

Bryan Van de Ven

unread,
Aug 11, 2016, 11:20:47 AM8/11/16
to bo...@continuum.io
Adam,

This is pretty terrific. I had to make some minor cosmetic changes to run on non-Windows. Would you be interested in making a contribution based on this? I think people would be very interested and appreciative. Any or all of:


* a new example in the GH repo
* some narrative documentation in the Running a Bokeh Server chapter
* a blog post on https://bokeh.github.io/blog/

Would be pretty amazing. We can certainly help with making a PR, answering questions, etc.

Thanks!

Bryan

> On Aug 11, 2016, at 9:32 AM, adam...@gmail.com wrote:
>
> Working on Windows 7 with Bokeh 0.12.1. Runs a Bokeh Server by creating a Tornado io_loop, sending that loop to bokeh.server.server.Server then running the io_loop. Embeds two bokeh servers in a Flask app. Check it out!
>
> --
> 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/08ca44f5-b671-4c08-97e4-3db156d9b0fd%40continuum.io.
> For more options, visit https://groups.google.com/a/continuum.io/d/optout.
> <serve_in_code.zip>

Christian Tremblay

unread,
Aug 11, 2016, 11:30:17 AM8/11/16
to bo...@continuum.io
This is one of the coolest thing ever :)

Christian Tremblay, ing.
Tél : 450-305-6177, 225


Prenez
le contrôle de votre bâtiment



2016-08-11 10:32 GMT-04:00 <adam...@gmail.com>:
Working on Windows 7 with Bokeh 0.12.1. Runs a Bokeh Server by creating a Tornado io_loop, sending that loop to bokeh.server.server.Server then running the io_loop. Embeds two bokeh servers in a Flask app. Check it out!

--

sean...@gmail.com

unread,
Aug 11, 2016, 8:45:26 PM8/11/16
to Bokeh Discussion - Public
Adam, this is AMAZING and exactly what I've been looking for!! As Bryan mentioned, it required a little change for Mac OS X but nothing major. Thank you so much for sharing!

narayan...@gmail.com

unread,
Aug 25, 2016, 4:44:35 PM8/25/16
to Bokeh Discussion - Public
Tableau needs to watch its back now.

On Thursday, August 11, 2016 at 10:32:12 AM UTC-4, Adam Hubbell wrote:

rootsum...@gmail.com

unread,
Nov 3, 2016, 5:47:29 AM11/3/16
to Bokeh Discussion - Public, narayan...@gmail.com
This is great for those who are more used to building WSGI apps!

One can also achieve similar interactivity using https://github.com/jupyter-incubator/dashboards

This way you design your application as a Jupyter notebook and deploy it as an interactive web app in a click of a button.

Playground with examples: http://jupyter.cloudet.xyz/


PS I equally prefer flask and Jupyter dashboards.

jorda...@gmail.com

unread,
Sep 3, 2018, 11:42:59 AM9/3/18
to Bokeh Discussion - Public
I was excited to see this, but unfortunately the auto_server feature in bokeh 0.13 has been replaced with server_session(), so I'm finding it difficult to get this code up and running. :( Does this project live in a github repository somewhere?

Bryan Van de ven

unread,
Sep 3, 2018, 1:16:32 PM9/3/18
to bo...@continuum.io
Hi,

That was derry exploratory work. Since, then, well-defined (and much simpler) ways to embed Bokeh apps in notebooks have been added as supported features:

https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb

Thanks,

Bryan
> --
> 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/427116e7-387f-426c-9d43-72bbf9e0039c%40continuum.io.

devild...@gmail.com

unread,
Mar 19, 2019, 10:11:55 PM3/19/19
to Bokeh Discussion - Public
I am trying to run your code but auto load is not working any more do you have any other version of this code as i am desperately looking for this solution . Thanks , Martin

devild...@gmail.com

unread,
Mar 19, 2019, 10:11:55 PM3/19/19
to Bokeh Discussion - Public
is there a chance for updated code i have rendered serveit.py file to keep up with the latest libraries but i am faced with this error :

TypeError: '<=' not supported between instances of 'AsyncIOMainLoop' and 'int'

any help would be appreciated

code :

imports to run a bokeh server in code, as opposed to running 'bokeh serve *args*' as a command line argument
from bokeh.server.server import Server
from bokeh.command.util import build_single_handler_applications

#imports to run my server
from bokeh.embed import server_document #to put bokeh in the flask app
from flask import Flask,render_template, redirect, url_for, request,session
import tornado.wsgi
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.autoreload
import sys
from collections import OrderedDict
import threading
import time

#setup flask
flaskApp = Flask(__name__)
flaskApp.debug = True

#initialize some values, sanatize the paths to the bokeh plots
files = ['bokeh\\ex1.py','bokeh\\ex2.py']
argvs = {}
urls = []
for i in files:
argvs[i]=None
urls.append(i.split('\\')[-1].split('.')[0])
host = 'localhost'
app_port = 5567
bokeh_port = 6060

def run_bokeh_server(bok_io_loop):
##turn file paths into bokeh apps
apps = build_single_handler_applications(files,argvs)
##args lifted from bokeh serve call to Server, with the addition of my own io_loop
kwags = {
'io_loop':bok_io_loop,
'generade_session_ids':True,
'redirect_root':True,
'use_x_headers':False,
'secret_key':None,
'num_procs':1,
'host':['%s:%d'%(host,app_port),'%s:%d'%(host,bokeh_port)],
'sign_sessions':False,
'develop':False,
'port':bokeh_port,
'use_index':True
}
srv = Server(apps,**kwags)


@flaskApp.route('/',methods=['GET']) #a sample page to display the bokeh docs
def graph_page():
#pull the bokeh server apps
bokeh_scripts = {}
for plot in urls:
bokeh_scripts[plot]=server_document(model=None, url='http://%s:%d'%(host,bokeh_port), app_path="/"+plot) # pulls the bokeh apps off of the bokeh server

#order the plots
all_divs= OrderedDict()
all_divs.update(bokeh_scripts)
all_divs = OrderedDict(sorted(all_divs.items(), key=lambda x: x[0]))

#throw the plots on a jinja2 template
return render_template('graph_template.html',div_dict=all_divs)


def rest_of_tornado(io_loop_here):
##a test to see if I can shutdown while the server is running. This will eventually get a button somewhere.
print('starting countdown')
time.sleep(300)
print('countdown finished')
io_loop.stop()


if __name__ == "__main__":
#initialize the tornado server
http_server = tornado.httpserver.HTTPServer(
tornado.wsgi.WSGIContainer(flaskApp)
)
http_server.listen(app_port)
io_loop = tornado.ioloop.IOLoop.instance()
tornado.autoreload.start(io_loop)

#call the turn off test
nadostop = threading.Thread(target=rest_of_tornado,args=(io_loop,))
nadostop.start()

#add the io_loop to the bokeh server
run_bokeh_server(io_loop)
print('starting the server on http://%s:%d/'%(host,app_port))

#run the bokeh server
io_loop.start()

Bryan Van de ven

unread,
Mar 19, 2019, 10:16:22 PM3/19/19
to Bokeh Discussion - Public
Hi, that error is not coming for Bokeh. My best guess is it coming from inside Tornado, but without more information (e.g. the full stack trace no just the single line you have, and all the relevant package versions) all I can suggest is trying different versions of dependencies, especially Tornado.

Thanks,

Bryan
> --
> 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/b616a035-fd0c-4cd1-a58b-5fd420983915%40continuum.io.
Message has been deleted

devild...@gmail.com

unread,
Mar 20, 2019, 8:58:57 AM3/20/19
to Bokeh Discussion - Public
hi again the full trace back is as follows

Traceback (most recent call last):
File "C:\Users\Dell1525\Desktop\flaskapp\env1\serve\serveit.py", line 84, in <module>
tornado.autoreload.start(io_loop)
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\tornado\autoreload.py", line 129, in start
scheduler = ioloop.PeriodicCallback(callback, check_time)
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\tornado\ioloop.py", line 1194, in __init__
if callback_time <= 0:
TypeError: '<=' not supported between instances of 'AsyncIOMainLoop' and 'int'

also do i need to allow specific websocket when running bokeh scripts using this example as i did in single file example for example:

bokeh serve bok5.py --allow-websocket-origin=127.0.0.1:5000

devild...@gmail.com

unread,
Mar 20, 2019, 9:06:13 AM3/20/19
to Bokeh Discussion - Public
just to make sure i have all dependencies i am including dependency tree as well:

- Jinja2 [required: >=2.7, installed: 2.10]
- MarkupSafe [required: >=0.23, installed: 1.1.0]
- numpy [required: >=1.7.1, installed: 1.15.4]
- packaging [required: >=16.8, installed: 18.0]
- pyparsing [required: >=2.0.2, installed: 2.3.0]
- six [required: Any, installed: 1.12.0]
- pillow [required: >=4.0, installed: 5.3.0]
- python-dateutil [required: >=2.1, installed: 2.7.5]
- six [required: >=1.5, installed: 1.12.0]
- PyYAML [required: >=3.10, installed: 3.13]
- six [required: >=1.5.2, installed: 1.12.0]
- tornado [required: >=4.3, installed: 5.1.1]
Cython==0.29.2
Flask==1.0.2
- click [required: >=5.1, installed: 7.0]
- itsdangerous [required: >=0.24, installed: 1.1.0]
- Jinja2 [required: >=2.10, installed: 2.10]
- MarkupSafe [required: >=0.23, installed: 1.1.0]
- Werkzeug [required: >=0.14, installed: 0.14.1]
matplotlib==3.0.2
- cycler [required: >=0.10, installed: 0.10.0]
- six [required: Any, installed: 1.12.0]
- kiwisolver [required: >=1.0.1, installed: 1.0.1]
- setuptools [required: Any, installed: 40.6.2]
- numpy [required: >=1.10.0, installed: 1.15.4]
- pyparsing [required: >=2.0.1,!=2.1.6,!=2.1.2,!=2.0.4, installed: 2.3.0]
- python-dateutil [required: >=2.1, installed: 2.7.5]
- six [required: >=1.5, installed: 1.12.0]
pandas==0.23.4
- numpy [required: >=1.9.0, installed: 1.15.4]
- python-dateutil [required: >=2.5.0, installed: 2.7.5]
- six [required: >=1.5, installed: 1.12.0]
- pytz [required: >=2011k, installed: 2018.7]
pipdeptree==0.13.2
- pip [required: >=6.0.0, installed: 19.0.3]
wheel==0.32.3
xlrd==1.1.0

devild...@gmail.com

unread,
Mar 20, 2019, 9:55:05 AM3/20/19
to Bokeh Discussion - Public
Forgot to mention i am running bokeh 1.0.2

devild...@gmail.com

unread,
Mar 20, 2019, 10:41:45 AM3/20/19
to Bokeh Discussion - Public
Sorry for so many posts but i am desperate to get this working for university project, based on your suggestions i was able to start the script after downgrading tornado to 4.4.1 but now the Bokeh server_document do not like the original arguments in the code first error was about the model so i removed it leaving bokeh_scripts[plot]=server_document(url='http://%s:%d'%(host,bokeh_port), app_path="/"+plot) but removing that brought another error. i will give you a walk through of what i did

step 1 start bokeh scripts:

(env1) C:\Users\Dell1525\Desktop\flaskapp\env1\serve\bokeh>bokeh serve ex1.py ex2.py --allow-websocket-origin=127.0.0.1:5567
2019-03-20 14:07:19,005 Starting Bokeh server version 1.0.2 (running on Tornado 4.4.1)
2019-03-20 14:07:19,011 Bokeh app running at: http://localhost:5006/ex1
2019-03-20 14:07:19,011 Bokeh app running at: http://localhost:5006/ex2
2019-03-20 14:07:19,011 Starting Bokeh server with process id: 22420

Step 2: run the serveit.py (renamed copy for testing purposes)

(env1) C:\Users\Dell1525\Desktop\flaskapp\env1\serve>serveitorg.py
starting countdown
starting the server on http://localhost:5567/

when i try to access the page on local host i am getting error:
This page isn’t working localhost didn’t send any data. ERR_EMPTY_RESPONSE

and this trace back in console:

(env1) C:\Users\Dell1525\Desktop\flaskapp\env1\serve>serveitorg.py
starting countdown
starting the server on http://localhost:5567/
ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\tornado\http1connection.py", line 238, in _read_message
delegate.finish()
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\tornado\httpserver.py", line 287, in finish
self.server.request_callback(self.request)
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\tornado\wsgi.py", line 277, in __call__
WSGIContainer.environ(request), start_response)
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Dell1525\Desktop\flaskapp\env1\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Dell1525\Desktop\flaskapp\env1\serve\serveitorg.py", line 58, in graph_page
bokeh_scripts[plot]=server_document(url='http://%s:%d'%(host,bokeh_port), app_path="/"+plot) # pulls the bokeh apps off of the bokeh server
TypeError: server_document() got an unexpected keyword argument 'app_path'
ERROR:tornado.application:Uncaught exception

the modified serveit.py code : https://pastebin.com/9cRDvC9t

thanks for help and i feel i am close to getting this right , Regards Martin



Bryan Van de ven

unread,
Mar 20, 2019, 11:18:17 AM3/20/19
to bo...@continuum.io
Hi,

If you consult the reference guide you can see that there is no "app_path" argument to "server_document":

https://bokeh.pydata.org/en/latest/docs/reference/embed.html#bokeh.embed.server_document

The "url" argument should just be the full URL to the Bokeh server app, whatever it is.

Thanks,

Bryan
> --
> 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/1a02c446-6a2f-4801-b9f7-dd00611ae2c2%40continuum.io.

devild...@gmail.com

unread,
Mar 20, 2019, 2:07:41 PM3/20/19
to Bokeh Discussion - Public
than you so much after running each server separately using the function bokeh serve ex2.py --port 5200 --allow-websocket-origin=localhost:5567 and specifying the full path before render template i got it working . Once again thank you for being so helpful
Reply all
Reply to author
Forward
Message has been deleted
0 new messages