Example #1
0
    def test_no_notebook(self, mock_get_browser_controller,
                         mock__show_file_with_state,
                         mock__show_server_with_state,
                         mock__show_notebook_with_state, mock_get_comms):
        mock_get_browser_controller.return_value = "controller"
        mock_get_comms.return_value = "comms"
        s = io.State()

        s.output_file("foo.html")
        io._show_with_state("obj", s, "browser", "new")
        self.assertFalse(mock__show_notebook_with_state.called)
        self.assertFalse(mock__show_server_with_state.called)
        self._check_func_called(mock__show_file_with_state,
                                ("obj", s, "new", "controller"), {})

        s._session_coords = _SessionCoordinates(
            dict(session_id="fakesession",
                 url="http://example.com",
                 app_path='/'))
        s._server_enabled = True
        io._show_with_state("obj", s, "browser", "new")
        self.assertFalse(mock__show_notebook_with_state.called)
        self._check_func_called(mock__show_server_with_state,
                                ("obj", s, "new", "controller"), {})
        self._check_func_called(mock__show_file_with_state,
                                ("obj", s, "new", "controller"), {})
Example #2
0
def show_session(session_id=None,
                 url='default',
                 app_path='/',
                 session=None,
                 browser=None,
                 new="tab",
                 controller=None):
    """Open a browser displaying a session document.

        If you have a session from ``pull_session()`` or
        ``push_session`` you can ``show_session(session=mysession)``.
        If you don't need to open a connection to the server yourself,
        you can show a new session in a browser by providing just the
        ``url`` and ``app_path``.

        Args:

        session_id : string, optional
             The name of the session, None to autogenerate a random one (default: None)

        url : str, optional
             The base server URL to connect to (default: 'default')

        app_path : str, optional
             Relative path to the app on the server (defualt: '/')

        session (ClientSession, optional) : session to get session ID and server URL from
            If you specify this, you don't need to specify session_id and url

        browser (str, optional) : browser to show with (default: None)
            For systems that support it, the **browser** argument allows
            specifying which browser to display in, e.g. "safari", "firefox",
            "opera", "windows-default" (see the ``webbrowser`` module
            documentation in the standard lib for more details).

        new (str, optional) : new file output mode (default: "tab")
            For file-based output, opens or raises the browser window
            showing the current output file.  If **new** is 'tab', then
            opens a new tab. If **new** is 'window', then opens a new window.

        """

    if session is not None:
        server_url = server_url_for_websocket_url(session._connection.url)
        session_id = session.id
    else:
        coords = _SessionCoordinates(
            dict(session_id=session_id, url=url, app_path=app_path))
        server_url = coords.server_url
        session_id = coords.session_id

    if controller is None:
        from bokeh.util.browser import get_browser_controller
        controller = get_browser_controller(browser=browser)

    controller.open(server_url + "?bokeh-session-id=" +
                    _encode_query_param(session_id),
                    new=_new_param[new])
Example #3
0
def pull_session(session_id=None, url='default', app_path='/', io_loop=None):
    """ Create a session by loading the current server-side document.

    ``session.document`` will be a fresh document loaded from
    the server. While the connection to the server is open,
    changes made on the server side will be applied to this
    document, and changes made on the client side will be
    synced to the server.

    If you don't plan to modify ``session.document`` you probably
    don't need to use this function; instead you can directly
    ``show_session()`` or ``autoload_server()`` without downloading
    the session's document into your process first. It's much
    more efficient to avoid downloading the session if you don't need
    to.

    In a production scenario, the ``session_id`` should be
    unique for each browser tab, which keeps users from
    stomping on each other. It's neither scalable nor secure to
    use predictable session IDs or to share session IDs across
    users.

    For a notebook running on a single machine, ``session_id``
    could be something human-readable such as ``"default"`` for
    convenience.

    If you allow ``pull_session()`` to generate a unique
    ``session_id``, you can obtain the generated ID with the
    ``id`` property on the returned ``ClientSession``.

    Args:
        session_id (string, optional) :
            The name of the session, None to autogenerate a random one (default: None)
        url (str, optional) :
            The base server URL to connect to (default: 'default')
        app_path (str, optional) :
            Relative path to the app on the server (default: '/')
        io_loop (``tornado.ioloop.IOLoop``, optional) :
            The IOLoop to use for the websocket
    Returns:
        session (ClientSession) :
            A new ClientSession connected to the server

    """
    coords = _SessionCoordinates(
        dict(session_id=session_id, url=url, app_path=app_path))
    session = ClientSession(session_id=session_id,
                            websocket_url=coords.websocket_url,
                            io_loop=io_loop)
    session.pull()
    return session
Example #4
0
def push_session(document,
                 session_id=None,
                 url='default',
                 app_path='/',
                 io_loop=None):
    """ Create a session by pushing the given document to the server,
       overwriting any existing server-side document.

       ``session.document`` in the returned session will be your
       supplied document. While the connection to the server is
       open, changes made on the server side will be applied to
       this document, and changes made on the client side will be
       synced to the server.

       In a production scenario, the ``session_id`` should be
       unique for each browser tab, which keeps users from
       stomping on each other. It's neither scalable nor secure to
       use predictable session IDs or to share session IDs across
       users.

       For a notebook running on a single machine, ``session_id``
       could be something human-readable such as ``"default"`` for
       convenience.

       If you allow ``push_session()`` to generate a unique
       ``session_id``, you can obtain the generated ID with the
       ``id`` property on the returned ``ClientSession``.

       Args:
            document : bokeh.document.Document
                The document to be pushed and set as session.document
            session_id : string, optional
                The name of the session, None to autogenerate a random one (default: None)
            url : str, optional
                The base server URL to connect to (default: 'default')
            app_path : str, optional
                Relative path to the app on the server (defualt: '/')
            io_loop : tornado.ioloop.IOLoop, optional
                The IOLoop to use for the websocket
       Returns:
            session : ClientSession
                A new ClientSession connected to the server

    """
    coords = _SessionCoordinates(
        dict(session_id=session_id, url=url, app_path=app_path))
    session = ClientSession(session_id=coords.session_id,
                            websocket_url=coords.websocket_url,
                            io_loop=io_loop)
    session.push(document)
    return session
Example #5
0
def show_session(session_id=None, url='default', app_path='/',
                 session=None, browser=None, new="tab", controller=None):
        """Open a browser displaying a session document.

        If you have a session from ``pull_session()`` or
        ``push_session`` you can ``show_session(session=mysession)``.
        If you don't need to open a connection to the server yourself,
        you can show a new session in a browser by providing just the
        ``url`` and ``app_path``.

        Args:

        session_id : string, optional
             The name of the session, None to autogenerate a random one (default: None)

        url : str, optional
             The base server URL to connect to (default: 'default')

        app_path : str, optional
             Relative path to the app on the server (defualt: '/')

        session (ClientSession, optional) : session to get session ID and server URL from
            If you specify this, you don't need to specify session_id and url

        browser (str, optional) : browser to show with (default: None)
            For systems that support it, the **browser** argument allows
            specifying which browser to display in, e.g. "safari", "firefox",
            "opera", "windows-default" (see the ``webbrowser`` module
            documentation in the standard lib for more details).

        new (str, optional) : new file output mode (default: "tab")
            For file-based output, opens or raises the browser window
            showing the current output file.  If **new** is 'tab', then
            opens a new tab. If **new** is 'window', then opens a new window.

        """

        if session is not None:
            server_url = server_url_for_websocket_url(session._connection.url)
            session_id = session.id
        else:
            coords = _SessionCoordinates(dict(session_id=session_id, url=url, app_path=app_path))
            server_url = coords.server_url
            session_id = coords.session_id

        if controller is None:
            import bokeh.browserlib as browserlib
            controller = browserlib.get_browser_controller(browser=browser)

        controller.open(server_url + "?bokeh-session-id=" + _encode_query_param(session_id),
                        new=_new_param[new])
Example #6
0
    def test(self, mock_push):
        s = io.State()
        s._session_coords = _SessionCoordinates(dict(session_id="thesession",
                                                     url="http://example.com",
                                                     app_path='/foo'))
        s._server_enabled = True
        controller = Mock()

        io._show_server_with_state("obj", s, "window", controller)
        self._check_func_called(mock_push, (), {"state": s})
        self._check_func_called(controller.open, ("http://example.com/foo?bokeh-session-id=thesession",), {"new": 1})

        io._show_server_with_state("obj", s, "tab", controller)
        self._check_func_called(mock_push, (), {"state": s})
        self._check_func_called(controller.open, ("http://example.com/foo?bokeh-session-id=thesession",), {"new": 2})
Example #7
0
def pull_session(session_id=None, url='default', app_path='/', io_loop=None):
    """ Create a session by loading the current server-side document.

    ``session.document`` will be a fresh document loaded from
    the server. While the connection to the server is open,
    changes made on the server side will be applied to this
    document, and changes made on the client side will be
    synced to the server.

    If you don't plan to modify ``session.document`` you probably
    don't need to use this function; instead you can directly
    ``show_session()`` or ``autoload_server()`` without downloading
    the session's document into your process first. It's much
    more efficient to avoid downloading the session if you don't need
    to.

    In a production scenario, the ``session_id`` should be
    unique for each browser tab, which keeps users from
    stomping on each other. It's neither scalable nor secure to
    use predictable session IDs or to share session IDs across
    users.

    For a notebook running on a single machine, ``session_id``
    could be something human-readable such as ``"default"`` for
    convenience.

    If you allow ``pull_session()`` to generate a unique
    ``session_id``, you can obtain the generated ID with the
    ``id`` property on the returned ``ClientSession``.

    Args:
        session_id (string, optional) :
            The name of the session, None to autogenerate a random one (default: None)
        url (str, optional) :
            The base server URL to connect to (default: 'default')
        app_path (str, optional) :
            Relative path to the app on the server (default: '/')
        io_loop (``tornado.ioloop.IOLoop``, optional) :
            The IOLoop to use for the websocket
    Returns:
        session (ClientSession) :
            A new ClientSession connected to the server

    """
    coords = _SessionCoordinates(dict(session_id=session_id, url=url, app_path=app_path))
    session = ClientSession(session_id=session_id, websocket_url=coords.websocket_url, io_loop=io_loop)
    session.pull()
    return session
Example #8
0
def push_session(document, session_id=None, url='default', app_path='/', io_loop=None):
    """ Create a session by pushing the given document to the server,
       overwriting any existing server-side document.

       ``session.document`` in the returned session will be your
       supplied document. While the connection to the server is
       open, changes made on the server side will be applied to
       this document, and changes made on the client side will be
       synced to the server.

       In a production scenario, the ``session_id`` should be
       unique for each browser tab, which keeps users from
       stomping on each other. It's neither scalable nor secure to
       use predictable session IDs or to share session IDs across
       users.

       For a notebook running on a single machine, ``session_id``
       could be something human-readable such as ``"default"`` for
       convenience.

       If you allow ``push_session()`` to generate a unique
       ``session_id``, you can obtain the generated ID with the
       ``id`` property on the returned ``ClientSession``.

       Args:
            document : bokeh.document.Document
                The document to be pushed and set as session.document
            session_id : string, optional
                The name of the session, None to autogenerate a random one (default: None)
            url : str, optional
                The base server URL to connect to (default: 'default')
            app_path : str, optional
                Relative path to the app on the server (defualt: '/')
            io_loop : tornado.ioloop.IOLoop, optional
                The IOLoop to use for the websocket
       Returns:
            session : ClientSession
                A new ClientSession connected to the server

    """
    coords = _SessionCoordinates(dict(session_id=session_id, url=url, app_path=app_path))
    session = ClientSession(session_id=coords.session_id, websocket_url=coords.websocket_url, io_loop=io_loop)
    session.push(document)
    return session
Example #9
0
    def test_no_notebook(self, mock_get_browser_controller,
            mock__show_file_with_state, mock__show_server_with_state,
            mock__show_notebook_with_state):
        mock_get_browser_controller.return_value = "controller"
        s = io.State()

        s.output_file("foo.html")
        io._show_with_state("obj", s, "browser", "new")
        self.assertFalse(mock__show_notebook_with_state.called)
        self.assertFalse(mock__show_server_with_state.called)
        self._check_func_called(mock__show_file_with_state, ("obj", s, "new", "controller"), {})

        s._session_coords = _SessionCoordinates(dict(session_id="fakesession",
                                                     url="http://example.com",
                                                     app_path='/'))
        s._server_enabled = True
        io._show_with_state("obj", s, "browser", "new")
        self.assertFalse(mock__show_notebook_with_state.called)
        self._check_func_called(mock__show_server_with_state, ("obj", s, "new", "controller"), {})
        self._check_func_called(mock__show_file_with_state, ("obj", s, "new", "controller"), {})
Example #10
0
    def test(self, mock_push):
        s = io.State()
        s._session_coords = _SessionCoordinates(
            dict(session_id="thesession",
                 url="http://example.com",
                 app_path='/foo'))
        s._server_enabled = True
        controller = Mock()

        io._show_server_with_state("obj", s, "window", controller)
        self._check_func_called(mock_push, (), {"state": s})
        self._check_func_called(
            controller.open,
            ("http://example.com/foo?bokeh-session-id=thesession", ),
            {"new": 1})

        io._show_server_with_state("obj", s, "tab", controller)
        self._check_func_called(mock_push, (), {"state": s})
        self._check_func_called(
            controller.open,
            ("http://example.com/foo?bokeh-session-id=thesession", ),
            {"new": 2})