Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. #imports to run a bokeh server in code, as opposed to running 'bokeh serve *args*' as a command line argument
  2. from bokeh.server.server import Server
  3. from bokeh.command.util import build_single_handler_applications
  4. from bokeh.embed import server_document
  5.  
  6. #imports to run my server
  7. from flask import Flask,render_template, redirect, url_for, request,session
  8. import tornado.wsgi
  9. import tornado.httpserver
  10. import tornado.ioloop
  11. import tornado.options
  12. import tornado.autoreload
  13. import sys
  14. from collections import OrderedDict
  15. import threading
  16. import time
  17.  
  18. #setup flask
  19. flaskApp = Flask(__name__)
  20. flaskApp.debug = True
  21.  
  22. #initialize some values, sanatize the paths to the bokeh plots
  23. files = ['bokeh\\ex1.py','bokeh\\ex2.py']
  24. argvs = {}
  25. urls = []
  26. for i in files:
  27.     argvs[i]=None
  28.     urls.append(i.split('\\')[-1].split('.')[0])
  29. host = 'localhost'
  30. app_port = 5567
  31. bokeh_port = 6060
  32.  
  33. def run_bokeh_server(bok_io_loop):
  34.     ##turn file paths into bokeh apps
  35.     apps = build_single_handler_applications(files,argvs)
  36.     ##args lifted from bokeh serve call to Server, with the addition of my own io_loop
  37.     kwags = {
  38.         'io_loop':bok_io_loop,
  39.         'generade_session_ids':True,
  40.         'redirect_root':True,
  41.         'use_x_headers':False,
  42.         'secret_key':None,
  43.         'num_procs':1,
  44.         'host':['%s:%d'%(host,app_port),'%s:%d'%(host,bokeh_port)],
  45.         'sign_sessions':False,
  46.         'develop':False,
  47.         'port':bokeh_port,
  48.         'use_index':True
  49.     }
  50.     srv = Server(apps,**kwags)
  51.  
  52.  
  53. @flaskApp.route('/',methods=['GET']) #a sample page to display the bokeh docs
  54. def graph_page():
  55.     #pull the bokeh server apps
  56.     bokeh_scripts = {}
  57.     for plot in urls:
  58.         bokeh_scripts[plot]=server_document(url='http://%s:%d'%(host,bokeh_port), app_path="/"+plot) # pulls the bokeh apps off of the bokeh server
  59.  
  60.     #order the plots
  61.     all_divs= OrderedDict()
  62.     all_divs.update(bokeh_scripts)
  63.     all_divs = OrderedDict(sorted(all_divs.items(), key=lambda x: x[0]))
  64.  
  65.     #throw the plots on a jinja2 template
  66.     return render_template('graph_template.html',div_dict=all_divs)
  67.  
  68.  
  69. def rest_of_tornado(io_loop_here):
  70.     ##a test to see if I can shutdown while the server is running. This will eventually get a button somewhere.
  71.     print('starting countdown')
  72.     time.sleep(300)
  73.     print('countdown finished')
  74.     io_loop.stop()
  75.  
  76.  
  77. if __name__ == "__main__":
  78.     #initialize the tornado server
  79.     http_server = tornado.httpserver.HTTPServer(
  80.         tornado.wsgi.WSGIContainer(flaskApp)
  81.     )
  82.     http_server.listen(app_port)
  83.     io_loop = tornado.ioloop.IOLoop.instance()
  84.     tornado.autoreload.start(io_loop)
  85.  
  86.     #call the turn off test
  87.     nadostop = threading.Thread(target=rest_of_tornado,args=(io_loop,))
  88.     nadostop.start()
  89.    
  90.     #add the io_loop to the bokeh server
  91.     run_bokeh_server(io_loop)
  92.     print('starting the server on http://%s:%d/'%(host,app_port))
  93.  
  94.     #run the bokeh server
  95.     io_loop.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement