def _configurable_serverapp(config=jp_server_config, base_url=jp_base_url, argv=jp_argv, environ=jp_environ, http_port=jp_http_port, tmp_path=tmp_path, root_dir=jp_root_dir, **kwargs): c = Config(config) c.NotebookNotary.db_file = ":memory:" token = hexlify(os.urandom(4)).decode("ascii") app = ServerApp.instance( # Set the log level to debug for testing purposes log_level='DEBUG', port=http_port, port_retries=0, open_browser=False, root_dir=str(root_dir), base_url=base_url, config=c, allow_root=True, token=token, **kwargs) app.init_signal = lambda: None app.log.propagate = True app.log.handlers = [] # Initialize app without httpserver app.initialize(argv=argv, new_httpserver=False) app.log.propagate = True app.log.handlers = [] # Start app without ioloop app.start_app() return app
def initialize_server(cls, argv=None, load_other_extensions=True, **kwargs): """Creates an instance of ServerApp and explicitly sets this extension to enabled=True (i.e. superceding disabling found in other config from files). The `launch_instance` method uses this method to initialize and start a server. """ jpserver_extensions = {cls.get_extension_package(): True} find_extensions = cls.load_other_extensions if "jpserver_extensions" in cls.serverapp_config: jpserver_extensions.update( cls.serverapp_config["jpserver_extensions"]) cls.serverapp_config["jpserver_extensions"] = jpserver_extensions find_extensions = False serverapp = ServerApp.instance(jpserver_extensions=jpserver_extensions, **kwargs) serverapp.aliases.update(cls.aliases) serverapp.initialize( argv=argv or [], starter_extension=cls.name, find_extensions=find_extensions, ) return serverapp
def test_resolve_file_to_run_and_root_dir(prefix_path, root_dir, file_to_run, expected_output): # Verify that the Singleton instance is cleared before the test runs. ServerApp.clear_instance() # Setup the file_to_run path, in case the server checks # if the directory exists before initializing the server. file_to_run = prefix_path(file_to_run) if file_to_run.is_absolute(): file_to_run.parent.mkdir(parents=True, exist_ok=True) kwargs = {"file_to_run": str(file_to_run)} # Setup the root_dir path, in case the server checks # if the directory exists before initializing the server. if root_dir: root_dir = prefix_path(root_dir) if root_dir.is_absolute(): root_dir.parent.mkdir(parents=True, exist_ok=True) kwargs["root_dir"] = str(root_dir) # Create the notebook in the given location serverapp = ServerApp.instance(**kwargs) if expected_output is SystemExit: with pytest.raises(SystemExit): serverapp._resolve_file_to_run_and_root_dir() else: relpath = serverapp._resolve_file_to_run_and_root_dir() assert relpath == str(pathlib.Path(expected_output)) # Clear the singleton instance after each run. ServerApp.clear_instance()
def initialize_server(argv=[], load_other_extensions=True, **kwargs): """Get an instance of the Jupyter Server.""" # Get a jupyter server instance serverapp = ServerApp.instance(**kwargs) # Initialize ServerApp config. # Parses the command line looking for # ServerApp configuration. serverapp.initialize(argv=argv, load_extensions=load_other_extensions) return serverapp
def jupyter_server_app(jupyter_server_args, jupyter_server_config): jupyter_server_app = ServerApp.instance() # we monkey patch old_listen = httpserver.HTTPServer.listen httpserver.HTTPServer.listen = lambda *x, **y: None # NOTE: in voila's conftest.py we call config after initialize jupyter_server_config(jupyter_server_app) jupyter_server_app.initialize(jupyter_server_args) yield jupyter_server_app httpserver.HTTPServer.listen = old_listen ServerApp.clear_instance()
def _default_serverapp(self): # load the current global instance, if any if ServerApp.initialized(): try: return ServerApp.instance() except Exception: # error retrieving instance, e.g. MultipleInstanceError pass # serverapp accessed before it was defined, # declare an empty one return ServerApp()
def initialize_server(cls, argv=[], load_other_extensions=True, **kwargs): """Creates an instance of ServerApp where this extension is enabled (superceding disabling found in other config from files). This is necessary when launching the ExtensionApp directly from the `launch_instance` classmethod. """ # The ExtensionApp needs to add itself as enabled extension # to the jpserver_extensions trait, so that the ServerApp # initializes it. config = Config(cls._jupyter_server_config()) serverapp = ServerApp.instance(**kwargs, argv=[], config=config) serverapp.initialize(argv=argv, find_extensions=load_other_extensions) return serverapp
def _configurable_serverapp( config=jp_server_config, base_url=jp_base_url, argv=jp_argv, environ=jp_environ, http_port=jp_http_port, tmp_path=tmp_path, root_dir=jp_root_dir, **kwargs, ): c = Config(config) c.NotebookNotary.db_file = ":memory:" token = hexlify(os.urandom(4)).decode("ascii") c.IdentityProvider.token = token # Allow tests to configure root_dir via a file, argv, or its # default (cwd) by specifying a value of None. if root_dir is not None: kwargs["root_dir"] = str(root_dir) app = ServerApp.instance( # Set the log level to debug for testing purposes log_level="DEBUG", port=http_port, port_retries=0, open_browser=False, base_url=base_url, config=c, allow_root=True, **kwargs, ) app.init_signal = lambda: None app.log.propagate = True app.log.handlers = [] # Initialize app without httpserver app.initialize(argv=argv, new_httpserver=False) # Reroute all logging StreamHandlers away from stdin/stdout since pytest hijacks # these streams and closes them at unfortunate times. stream_handlers = [ h for h in app.log.handlers if isinstance(h, logging.StreamHandler) ] for handler in stream_handlers: handler.setStream(jp_logging_stream) app.log.propagate = True app.log.handlers = [] # Start app without ioloop app.start_app() return app
def test_urls(config, public_url, local_url, connection_url): # Verify we're working with a clean instance. ServerApp.clear_instance() serverapp = ServerApp.instance(**config) # If a token is generated (not set by config), update # expected_url with token. if serverapp._token_generated: public_url = public_url.replace("<generated>", serverapp.token) local_url = local_url.replace("<generated>", serverapp.token) connection_url = connection_url.replace("<generated>", serverapp.token) assert serverapp.public_url == public_url assert serverapp.local_url == local_url assert serverapp.connection_url == connection_url # Cleanup singleton after test. ServerApp.clear_instance()
def initialize_server(cls, argv=[], load_other_extensions=True, **kwargs): """Creates an instance of ServerApp and explicitly sets this extension to enabled=True (i.e. superceding disabling found in other config from files). The `launch_instance` method uses this method to initialize and start a server. """ serverapp = ServerApp.instance( jpserver_extensions={cls.get_extension_package(): True}, **kwargs) serverapp.initialize( argv=argv, starter_extension=cls.name, find_extensions=cls.load_other_extensions, ) return serverapp
def serverapp( config={}, argv=[], environ=environ, http_port=http_port, tmp_path=tmp_path, home_dir=home_dir, data_dir=data_dir, config_dir=config_dir, runtime_dir=runtime_dir, root_dir=root_dir, **kwargs ): c = Config(config) c.NotebookNotary.db_file = ":memory:" token = hexlify(os.urandom(4)).decode("ascii") url_prefix = "/" app = ServerApp.instance( port=http_port, port_retries=0, open_browser=False, config_dir=str(config_dir), data_dir=str(data_dir), runtime_dir=str(runtime_dir), root_dir=str(root_dir), base_url=url_prefix, config=c, allow_root=True, token=token, **kwargs ) app.init_signal = lambda: None app.log.propagate = True app.log.handlers = [] # Initialize app without httpserver app.initialize(argv=argv, new_httpserver=False) app.log.propagate = True app.log.handlers = [] # Start app without ioloop app.start_app() return app
def initialize_server(cls, argv=[], load_other_extensions=True, **kwargs): """Creates an instance of ServerApp where this extension is enabled (superceding disabling found in other config from files). This is necessary when launching the ExtensionApp directly from the `launch_instance` classmethod. """ # The ExtensionApp needs to add itself as enabled extension # to the jpserver_extensions trait, so that the ServerApp # initializes it. config = Config({ "ServerApp": { "jpserver_extensions": { cls.extension_name: True }, "open_browser": True, "default_url": cls.extension_url } }) serverapp = ServerApp.instance(**kwargs, argv=[], config=config) serverapp.initialize(argv=argv, load_extensions=load_other_extensions) return serverapp
def jp_server_cleanup(io_loop): yield app: ServerApp = ServerApp.instance() loop = io_loop.asyncio_loop loop.run_until_complete(app._cleanup()) ServerApp.clear_instance()
from jupyter_server.serverapp import ServerApp success_msg = lambda name: """\n Congratulations! The server extension, {}, was successfully found and linked! """.format(name) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('extension_name') args = parser.parse_args() extension_name = args.extension_name app = ServerApp.instance( # Set the log level to debug for testing purposes port_retries=0, open_browser=False, ) app.initialize(argv=[]) extensions = app.extension_manager.extensions # Verify that the extension was found. assert extension_name in extensions # Verify that the extension is loaded and linked assert extension_name in app.extension_manager.linked_extensions # Print the success message. print(success_msg(extension_name))