Пример #1
0
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
Пример #2
0
def test_func_adds_roots():
    def add_roots(doc):
        doc.add_root(AnotherModelInTestFunction())
        doc.add_root(SomeModelInTestFunction())
    handler = FunctionHandler(add_roots)
    doc = Document()
    handler.modify_document(doc)
    if handler.failed:
        raise RuntimeError(handler.error)
    assert len(doc.roots) == 2
Пример #3
0
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
Пример #4
0
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"
Пример #5
0
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)
Пример #6
0
 def test_excess_static_path(self):
     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)
Пример #7
0
    def test_two_handlers(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)
        doc = a.create_document()
        assert len(doc.roots) == 3
Пример #8
0
    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
Пример #9
0
    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()
Пример #10
0
    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'])
Пример #11
0
    def test_pull_large_document(self, ManagedServerLoop: MSL) -> 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=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 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()
Пример #13
0
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()
Пример #14
0
    def test_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)
        a.add(handler2)
        assert a.static_path == "foo"
    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()
Пример #16
0
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()
Пример #17
0
    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
Пример #18
0
    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))
Пример #19
0
def test_one_handler():
    a = 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
Пример #20
0
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()
Пример #21
0
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()
Пример #22
0
    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
Пример #23
0
    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))
Пример #24
0
    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 make_tool(self,
                  arr: Union[xr.DataArray, str],
                  notebook_url=None,
                  notebook_handle=True,
                  **kwargs):
        from bokeh.application import Application
        from bokeh.application.handlers import FunctionHandler
        from bokeh.io import show

        def generate_url(port):
            if port is None:
                return 'localhost:8888'

            return 'localhost:{}'.format(port)

        if notebook_url is None:
            if 'PORT' in arpes.config.CONFIG:
                notebook_url = 'localhost:{}'.format(
                    arpes.config.CONFIG['PORT'])
            else:
                notebook_url = 'localhost:8888'

        if isinstance(arr, str):
            arr = load_dataset(arr)
            if 'cycle' in arr.dims and len(arr.dims) > 3:
                warnings.warn('Summing over cycle')
                arr = arr.sum('cycle', keep_attrs=True)

        if self.auto_zero_nans and {'kx', 'ky', 'kz', 'kp'}.intersection(
                set(arr.dims)):
            # We need to copy and make sure to clear any nan values, because bokeh
            # does not send these over the wire for some reason
            arr = arr.copy()
            np.nan_to_num(arr.values, copy=False)

        # rebin any axes that have more than 800 pixels
        if self.auto_rebin and np.any(np.asarray(arr.shape) > self.rebin_size):
            reduction = {
                d: (s // self.rebin_size) + 1
                for d, s in arr.S.dshape.items()
            }
            warnings.warn('Rebinning with {}'.format(reduction))

            arr = rebin(arr, reduction=reduction)

            # TODO pass in a reference to the original copy of the array and make sure that
            # preparation tasks move over transparently

        self.arr = arr
        handler = FunctionHandler(self.tool_handler)
        app = Application(handler)
        show(app, notebook_url=notebook_url, notebook_handle=notebook_handle)

        return self.app_context
Пример #26
0
    def __init__(self, curves, active):
        self.io_loop = IOLoop.current()
        self.bokeh_app = Application(FunctionHandler(self.modify_doc))
        self.server = Server({'/': self.bokeh_app}, io_loop=self.io_loop)

        self.colors = ['blue', 'red', 'green', 'magenta']
        self.curves = curves
        self.active = active
        self.source = ColumnDataSource(data=self.curves.downsample())
        self.busy = False
        self.skip_update = False
Пример #27
0
def run_server(path_to_db,
               options=create_default_reporting_options(embedded=True),
               show_app=True,
               handlers=[],
               port=5100):
    """
    Run a server

    Note:
        the .db will be replaced by .json if a file with this name is present,
        it will be imported and used as configuration

    Args:
        path_to_db: the path to the SQLite database
        options: configuration options
        show_app: if ``True``, the application will be started in an open browser
        handlers: additional handlers for the Bokeh application
        port: the port of the server
    """
    def fn(doc):
        return report(sql_database_path=path_to_db, options=options, doc=doc)

    app_1 = Application(FunctionHandler(fn), *handlers)

    if len(options.config) == 0:
        json_config = path_to_db.replace('.db', '.json')
        if os.path.exists(json_config):
            with open(json_config, 'r') as f:
                config = json.load(f)
                options.config = config

    # here we expect to have a folder with the following structure
    # root/{app_name}/static/{...} for all the static resources (e.g., images, numpy arrays)
    app_directory = os.path.dirname(path_to_db)
    app_name = os.path.basename(app_directory)
    apps = {f'/{app_name}': app_1}
    print('Starting reporting server:', apps, ' port=', port)
    server = Server(apps, num_procs=1, port=port)

    # set up the `static` mapping
    handlers = [(
        f'/{app_name}/static/(.*)',
        web.StaticFileHandler,
        {
            'path': os.path.join(app_directory, 'static')
        },
    )]
    server._tornado.add_handlers(r".*", handlers)

    # start the server
    server.start()
    if show_app:
        server.io_loop.add_callback(server.show, "/")
    server.io_loop.start()
def main():
    print("Preparing a bokeh application.")
    io_loop = IOLoop.current()
    bokeh_app = Application(FunctionHandler(modify_doc))

    server = Server({"/": bokeh_app}, io_loop=io_loop)
    server.start()
    print("Opening Bokeh application on http://localhost:5006/")

    io_loop.add_callback(server.show, "/")
    io_loop.start()
Пример #29
0
    def get_bokeh_app(self):
        """
        Generate a single instance of the bokeh app.

        The plot manager handles all the data to the plots.
        :return: Bokeh app created.
        """
        if self.bokeh_app is None:
            handler = FunctionHandler(self.plot_manager.setup_bokeh_server)
            self.bokeh_app = Application(handler)

        return self.bokeh_app
Пример #30
0
def main():
    """Launch bokeh server and connect to it
    """

    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()
Пример #31
0
def bk_worker(bk_app_function, sockets):
    # can't use shortcuts here, since we are passing to low level BokehTornado
    bkapp = Application(FunctionHandler(bk_app_function))

    asyncio.set_event_loop(asyncio.new_event_loop())

    bokeh_tornado = BokehTornado({'/bkapp': bkapp},
                                 extra_websocket_origins=["localhost:8000"])
    bokeh_http = HTTPServer(bokeh_tornado)
    bokeh_http.add_sockets(sockets)

    server = BaseServer(IOLoop.current(), bokeh_tornado, bokeh_http)
    server.start()
    server.io_loop.start()
Пример #32
0
 def test_launch_simple_server(self):
     obj = Curve([])
     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)
Пример #33
0
def main():
    """Launch the server and connect to it."""
    print("Preparing a bokeh application.")
    io_loop = IOLoop.current()
    bokeh_app = Application(FunctionHandler(modify_doc))

    server = Server({"/": bokeh_app},
                    io_loop=io_loop,
                    prefix="stream-debugger")
    server.start()
    print("Opening Bokeh application on http://localhost:5006/")

    io_loop.add_callback(server.show, "/")
    io_loop.start()
Пример #34
0
    def assemble_bokeh_doc(self, widgets, plots, plot_update, theme):
        def main_doc(doc):
            doc.add_root(widgets)  # add the widgets to the document
            doc.add_root(
                plots
            )  # Add four plots to document, using the gridplot method of arranging them
            doc.add_periodic_callback(
                plot_update,
                150)  # Add a periodic callback to be run every x milliseconds
            doc.theme = theme

        # Create bokeh app
        self.bokeh_app = Application(
            FunctionHandler(main_doc)
        )  # Application is "a factory for Document instances" and FunctionHandler "runs a function which modifies a document"
Пример #35
0
def bk_worker(sockets, port):
    """ Worker thread to  run Bokeh Server """
    _bkapp = Application(FunctionHandler(bkapp))
    asyncio.set_event_loop(asyncio.new_event_loop())

    websocket_origins = [f"{BOKEH_ADDR}:{port}", f"{FLASK_ADDR}:{FLASK_PORT}"]
    bokeh_tornado = BokehTornado({BOKEH_PATH: _bkapp},
                                 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()
Пример #36
0
def launch_app():
    """This is where the server is launched, and we connect to the server.
    Recipe from the bokeh reference pages.
    """
    print('Opening climtrend on server...')
    io_loop = IOLoop.current()
    bokeh_app = Application(FunctionHandler(modify_doc))
    server = Server({"/": bokeh_app}, io_loop=io_loop)
    server.start()
    print('Running climtrend on http://localhost:5006/. ' +
          'Press Ctrl + c to exit.')

    # This equivalent to running bokeh serve --show from the command line. Kind
    # of. Not good for deployment.
    io_loop.add_callback(server.show, "/")
    io_loop.start()