def bk_worker(sockets, port): """ Worker thread to run Bokeh Server """ asyncio.set_event_loop(asyncio.new_event_loop()) websocket_origins = [f"{BOKEH_ADDR}:{port}", f"{FLASK_ADDR}:{FLASK_PORT}"] # bokeh applications _bkapp_maps = Application(FunctionHandler(bkapp_maps)) _bkapp_trends = Application(FunctionHandler(bkapp_trends)) _bkapp_histograms = Application(FunctionHandler(bkapp_histograms)) _bkapp_models = Application(FunctionHandler(bkapp_models)) bokeh_tornado = BokehTornado( { '/bkapp-maps': _bkapp_maps, '/bkapp-trends': _bkapp_trends, '/bkapp-histograms': _bkapp_histograms, '/bkapp-models': _bkapp_models }, extra_websocket_origins=websocket_origins, **{'use_xheaders': True}) bokeh_http = HTTPServer(bokeh_tornado, xheaders=True) bokeh_http.add_sockets(sockets) server = BaseServer(IOLoop.current(), bokeh_tornado, bokeh_http) server.start() server.io_loop.start()
def test_two_handlers_in_init(self) -> None: def add_roots(doc): doc.add_root(AnotherModelInTestApplication()) doc.add_root(SomeModelInTestApplication()) def add_one_root(doc): doc.add_root(AnotherModelInTestApplication()) handler = FunctionHandler(add_roots) handler2 = FunctionHandler(add_one_root) a = baa.Application(handler, handler2) doc = a.create_document() assert len(doc.roots) == 3
def test_safe_to_fork(self) -> None: def add_roots(doc): doc.add_root(AnotherModelInTestApplication()) doc.add_root(SomeModelInTestApplication()) def add_one_root(doc): doc.add_root(AnotherModelInTestApplication()) handler = FunctionHandler(add_roots) handler2 = FunctionHandler(add_one_root) a = baa.Application(handler, handler2) assert a.safe_to_fork a.create_document() assert not a.safe_to_fork
def test_no_static_path(self) -> None: a = baa.Application() def add_roots(doc): doc.add_root(AnotherModelInTestApplication()) doc.add_root(SomeModelInTestApplication()) def add_one_root(doc): doc.add_root(AnotherModelInTestApplication()) handler = FunctionHandler(add_roots) a.add(handler) handler2 = FunctionHandler(add_one_root) a.add(handler2) assert a.static_path == None
def test_static_path(): a = Application() def add_roots(doc): doc.add_root(AnotherModelInTestApplication()) doc.add_root(SomeModelInTestApplication()) def add_one_root(doc): doc.add_root(AnotherModelInTestApplication()) handler = FunctionHandler(add_roots) handler._static = "foo" a.add(handler) handler2 = FunctionHandler(add_one_root) a.add(handler2) assert a.static_path == "foo"
def test_two_handlers(): a = Application() def add_roots(doc): doc.add_root(AnotherModelInTestApplication()) doc.add_root(SomeModelInTestApplication()) def add_one_root(doc): doc.add_root(AnotherModelInTestApplication()) handler = FunctionHandler(add_roots) a.add(handler) handler2 = FunctionHandler(add_one_root) a.add(handler2) doc = a.create_document() assert len(doc.roots) == 3
def test_excess_static_path(self) -> None: a = baa.Application() def add_roots(doc): doc.add_root(AnotherModelInTestApplication()) doc.add_root(SomeModelInTestApplication()) def add_one_root(doc): doc.add_root(AnotherModelInTestApplication()) handler = FunctionHandler(add_roots) handler._static = "foo" a.add(handler) handler2 = FunctionHandler(add_one_root) handler2._static = "bar" with pytest.raises(RuntimeError) as e: a.add(handler2) assert "More than one static path" in str(e.value)
def test_pull_document(self, ManagedServerLoop: MSL) -> None: application = Application() def add_roots(doc: Document): doc.add_root(AnotherModelInTestClientServer(bar=43)) doc.add_root( SomeModelInTestClientServer(foo=42, data=bytes( [0x00, 0x01, 0xFE, 0xFF]))) handler = FunctionHandler(add_roots) application.add(handler) with ManagedServerLoop(application) as server: client_session = pull_session(session_id=ID("test_pull_document"), url=url(server), io_loop=server.io_loop) assert len(client_session.document.roots) == 2 server_session = server.get_session('/', client_session.id) assert len(server_session.document.roots) == 2 results = {} for r in server_session.document.roots: if hasattr(r, 'foo'): results['foo'] = r.foo if hasattr(r, 'bar'): results['bar'] = r.bar assert results['foo'] == 42 assert results['bar'] == 43 client_session.close() client_session._loop_until_closed() assert not client_session.connected
def test_launch_server_with_stream(self): obj = Curve([]) stream = RangeXY(source=obj) launched = [] def modify_doc(doc): bokeh_renderer(obj, doc=doc) launched.append(True) server.stop() handler = FunctionHandler(modify_doc) app = Application(handler) server = Server({'/': app}, port=0) server.start() url = "http://localhost:" + str(server.port) + "/" pull_session(session_id='Test', url=url, io_loop=server.io_loop) self.assertTrue(len(launched)==1) cb = bokeh_renderer.last_plot.callbacks[0] self.assertIsInstance(cb, RangeXYCallback) self.assertEqual(cb.streams, [stream]) x_range = bokeh_renderer.last_plot.handles['x_range'] self.assertIn(cb.on_change, x_range._callbacks['start']) self.assertIn(cb.on_change, x_range._callbacks['end']) y_range = bokeh_renderer.last_plot.handles['y_range'] self.assertIn(cb.on_change, y_range._callbacks['start']) self.assertIn(cb.on_change, y_range._callbacks['end'])
def run(self): setproctitle.setproctitle("stream_manager") io_loop = IOLoop.current() def modify_doc(doc): h_input = Div(text="""<h2>Upload your mp4 file</h2> """, max_height=40) file_input = FileInput() source = ColumnDataSource(dict()) def value_changed(attr, old, new): # print(type(data)) # with open("fake_video.mp4", "wb") as binary_file: # Write bytes to file # binary_file.write(data) print("init new process") # data = b64decode(new) print(file_input.filename) self.spawn_new_worker(file_input.filename) print(self.pool) file_input.on_change("filename", value_changed) doc.add_root(column(h_input, file_input)) bokeh_app = Application(FunctionHandler(modify_doc)) server = Server({"/": bokeh_app}, io_loop=io_loop, prefix="stream-debugger") server.start() server.show("/") io_loop.start()
def run_app(show=True): def modify_doc(doc): app = SimEvalApp() l = app.get_layout() doc.add_root(l) doc.title = 'Similarity evaluation dashboard' io_loop = IOLoop.instance() bokeh_app = Application(FunctionHandler(modify_doc)) server = Server( {'/': bokeh_app}, io_loop=io_loop, allow_websocket_origin="*", port=SIM_EVAL_PORT, host='*', address='localhost', use_xheaders=True ) server.start() gh.print_and_flush('Starting Bokeh ioloop. Url: http://localhost:{}/'.format(SIM_EVAL_PORT)) if show: io_loop.add_callback(server.show, "/") io_loop.start()
def view_metamodel(meta_model_comp, resolution, port_number, browser): """ Visualize a metamodel. Parameters ---------- meta_model_comp : MetaModelStructuredComp or MetaModelUnStructuredComp The metamodel component. resolution : int Number of points to control contour plot resolution. port_number : int Bokeh plot port number. browser : bool Boolean to show the browser """ from bokeh.application.application import Application from bokeh.application.handlers import FunctionHandler def make_doc(doc): MetaModelVisualization(meta_model_comp, resolution, doc=doc) server = Server({'/': Application(FunctionHandler(make_doc))}, port=int(port_number)) if browser: server.io_loop.add_callback(server.show, "/") else: print('Server started, to view go to http://localhost:{}/'.format(port_number)) server.io_loop.start()
def _launcher(self, obj, threaded=False, io_loop=None): if io_loop: io_loop.make_current() launched = [] def modify_doc(doc): bokeh_renderer(obj, doc=doc) launched.append(True) handler = FunctionHandler(modify_doc) app = Application(handler) server = Server({'/': app}, port=0, io_loop=io_loop) server.start() self._port = server.port self._server = server if threaded: server.io_loop.add_callback(self._loaded.set) thread = threading.current_thread() state._thread_id = thread.ident if thread else None io_loop.start() else: url = "http://localhost:" + str(server.port) + "/" session = pull_session(session_id='Test', url=url, io_loop=server.io_loop) self.assertTrue(len(launched) == 1) return session, server return None, server
def get_function_handler_for_bokeh(self, url): modulelist = get_sub_direct('Modules') modulelist.sort() for sub in modulelist: if sub != 'Menu' and os.path.isfile( os.path.join(os.path.dirname(__file__), 'Modules', sub, 'config.xml')): e = xml.etree.ElementTree.parse( os.path.join(os.path.dirname(__file__), 'Modules', sub, 'config.xml')).getroot() if (e.find('menu').find('url').text == url): function = e.find('menu').find('callback').text module = self.load_spec(self, sub) classd = getattr(module, 'Layout')( GenericHandler.build_menu(GenericHandler)) for point in e.find('menu').findall('point'): if (point.find('url').text == url): function = point.find('callback').text module = self.load_spec(self, sub) classd = getattr(module, 'Layout')( GenericHandler.build_menu(GenericHandler)) print(url) return FunctionHandler(getattr(classd, function))
def main(): """Launch bokeh server and connect to it """ print('\n' + ('-' * 100)) print('Select file...') print('-' * 100 + '\n') global allData #, env root = tk.Tk() root.attributes("-topmost", True) root.withdraw() filepath = filedialog.askopenfilename() if filepath: allData = pickle.load(open(filepath, 'rb')) # env_path = os.path.dirname(filepath) + '/Environment ' + filepath[-22:] # env = pickle.load(open(env_path, 'rb')) # allData = tqdm.tqdm(preProcessing(allData), total=allData.shape[0], file=sys.stdout) # allData = preProcessing(allData) print('\n' + ('-' * 100)) print('ALL DONE!') print('-' * 100 + '\n') print("Preparing a bokeh application.") io_loop = IOLoop.current() bokeh_app = Application(FunctionHandler(modify_doc)) server = Server({'/app': bokeh_app}, io_loop=io_loop, port=5001) server.start() print("Opening Bokeh application on http://localhost:5006/") server.show('/app') io_loop.start()
def test_pull_document(self): application = Application() def add_roots(doc): doc.add_root(AnotherModelInTestClientServer(bar=43)) doc.add_root(SomeModelInTestClientServer(foo=42)) handler = FunctionHandler(add_roots) application.add(handler) with ManagedServerLoop(application) as server: client_session = pull_session(session_id='test_pull_document', url=url(server), io_loop=server.io_loop) assert len(client_session.document.roots) == 2 server_session = server.get_session('/', client_session.id) assert len(server_session.document.roots) == 2 results = {} for r in server_session.document.roots: if hasattr(r, 'foo'): results['foo'] = r.foo if hasattr(r, 'bar'): results['bar'] = r.bar assert results['foo'] == 42 assert results['bar'] == 43 client_session.close() client_session.loop_until_closed(suppress_warning=True) assert not client_session.connected
def test_pull_large_document(self, ManagedServerLoop) -> None: application = Application() def add_roots(doc): import numpy as np rows, cols = (40000, 100) columns=['x'+str(i) for i in range(cols)] a = np.random.randn(cols, rows) source = ColumnDataSource(data=dict(zip(columns, a))) doc.add_root(source) handler = FunctionHandler(add_roots) application.add(handler) with ManagedServerLoop(application) as server: client_session = pull_session(session_id='test_pull_document', url=url(server), io_loop=server.io_loop, max_message_size=50000000) assert len(client_session.document.roots) == 1 server_session = server.get_session('/', client_session.id) assert len(server_session.document.roots) == 1 results = {} for r in server_session.document.roots: if hasattr(r, 'data'): results['data'] = r.data assert len(list(results['data'].keys())) == 100 assert all(len(x) == 40000 for x in results['data'].values()) client_session.close() client_session._loop_until_closed() assert not client_session.connected
def test_excess_static_path(): a = Application() def add_roots(doc): doc.add_root(AnotherModelInTestApplication()) doc.add_root(SomeModelInTestApplication()) def add_one_root(doc): doc.add_root(AnotherModelInTestApplication()) handler = FunctionHandler(add_roots) handler._static = "foo" a.add(handler) handler2 = FunctionHandler(add_one_root) handler2._static = "bar" with pytest.raises(RuntimeError): a.add(handler2)
def main(): logger = logging.getLogger() logger.setLevel(logging.ERROR) io_loop = IOLoop.current() bapp = Application(FunctionHandler(make_document)) server = Server({"/read_eeg": bapp}, io_loop=io_loop, port=5010, allow_websocket_origin=["*"]) server.start() io_loop.start()
def start_bokeh_server(self): self.bokeh_app = Application(FunctionHandler(self.generate_plots)) self.bokeh_server = Server({'/': self.bokeh_app}, num_procs=1) self.bokeh_server.start() # start io loop for bokeh_server self.bokeh_server.io_loop.add_callback(self.bokeh_server.show, '/') self.bokeh_server.io_loop.start()
def app(self_or_cls, plot, show=False, new_window=False, websocket_origin=None): """ Creates a bokeh app from a HoloViews object or plot. By default simply attaches the plot to bokeh's curdoc and returns the Document, if show option is supplied creates an Application instance and displays it either in a browser window or inline if notebook extension has been loaded. Using the new_window option the app may be displayed in a new browser tab once the notebook extension has been loaded. A websocket origin is required when launching from an existing tornado server (such as the notebook) and it is not on the default port ('localhost:8888'). """ renderer = self_or_cls.instance(mode='server') # If show=False and not in notebook context return document if not show and not self_or_cls.notebook_context: doc, _ = renderer(plot) return doc def modify_doc(doc): renderer(plot, doc=doc) handler = FunctionHandler(modify_doc) app = Application(handler) if not show: # If not showing and in notebook context return app return app elif self_or_cls.notebook_context and not new_window: # If in notebook, show=True and no new window requested # display app inline opts = dict( notebook_url=websocket_origin) if websocket_origin else {} return bkshow(app, **opts) # If app shown outside notebook or new_window requested # start server and open in new browser tab from tornado.ioloop import IOLoop loop = IOLoop.current() opts = dict(allow_websocket_origin=[websocket_origin ]) if websocket_origin else {} opts['io_loop' if bokeh_version > '0.12.7' else 'loop'] = loop server = Server({'/': app}, port=0, **opts) def show_callback(): server.show('/') server.io_loop.add_callback(show_callback) server.start() try: loop.start() except RuntimeError: pass return server
def test_one_handler(self) -> None: a = baa.Application() def add_roots(doc): doc.add_root(AnotherModelInTestApplication()) doc.add_root(SomeModelInTestApplication()) handler = FunctionHandler(add_roots) a.add(handler) doc = a.create_document() assert len(doc.roots) == 2
def add_plot(self, path, func): """ Registers a new plot in the bokeh server. Args: path: path where the plot will respond to queries func: the function that renders the plot. """ self.apps[path] = Application(FunctionHandler(func))
def main(host="localhost", port=5002): global PORT PORT = port fname = os.path.basename(__file__).strip('.py') bokeh_app = Application(FunctionHandler(Handler.modify_doc)) io_loop = tornado.ioloop.IOLoop.current() bokeh_server = Server({'/bokeh/{name}'.format(name=fname): bokeh_app}, io_loop=io_loop, address=host, port=PORT) bokeh_server.start()
def main(): bokeh_app = Application(FunctionHandler(modify_doc)) io_loop = IOLoop.current() logger.info('Opening Bokeh application on http://localhost:5006/') server = Server({'/': bokeh_app}, io_loop=io_loop) server.start() io_loop.add_callback(server.show, "/") print('server') io_loop.start()
def __init__(self): """Base class to allow callbacks & interactions with the bokeh client. See difference between 'bokeh server' and 'bokeh client'.""" self.layout = layout() # Application and callback enabler def add_doc(doc): doc.add_root(self.layout) self.app = Application(FunctionHandler(add_doc))
def get_bokeh_app(self): """ Generate a single instance of the bokeh app. :return: Bokeh app created. """ if self.bokeh_app is None: handler = FunctionHandler(self.AvgWater.setup_bokeh_server) self.bokeh_app = Application(handler) return self.bokeh_app
def test_empty_func(): def noop(doc): pass handler = FunctionHandler(noop) doc = Document() handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) assert not doc.roots
def show(self, port=0, websocket_origin=None): """ Starts a bokeh server and displays the Viewable in a new tab Arguments --------- port: int (optional) Allows specifying a specific port (default=0 chooses random open port) websocket_origin: str or list(str) (optional) A list of hosts that can connect to the websocket. This is typically required when embedding a server app in an external web site. If None, "localhost" is used. Returns ------- server: bokeh Server instance """ def modify_doc(doc): return self.server_doc(doc) handler = FunctionHandler(modify_doc) app = Application(handler) from tornado.ioloop import IOLoop loop = IOLoop.current() if websocket_origin and not isinstance(websocket_origin, list): websocket_origin = [websocket_origin] opts = dict(allow_websocket_origin=websocket_origin ) if websocket_origin else {} opts['io_loop'] = loop server = Server({'/': app}, port=port, **opts) def show_callback(): server.show('/') server.io_loop.add_callback(show_callback) server.start() def sig_exit(*args, **kwargs): loop.add_callback_from_signal(do_stop) def do_stop(*args, **kwargs): loop.stop() signal.signal(signal.SIGINT, sig_exit) try: loop.start() except RuntimeError: pass return server
def test_safe_to_fork(): def noop(doc): pass handler = FunctionHandler(noop) doc = Document() assert handler.safe_to_fork handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) assert not handler.safe_to_fork