From 08e7c9f3a7d40f364cab3dc888dddc2761b93aa1 2014-01-14 00:22:43 From: Thomas Kluyver Date: 2014-01-14 00:22:43 Subject: [PATCH] Command line entry point to list running notebook servers --- diff --git a/IPython/html/notebookapp.py b/IPython/html/notebookapp.py index d3dae64..2d4c99b 100644 --- a/IPython/html/notebookapp.py +++ b/IPython/html/notebookapp.py @@ -214,6 +214,27 @@ class NotebookWebApplication(web.Application): return new_handlers +class NbserverListApp(BaseIPythonApplication): + + description="List currently running notebook servers in this profile." + + flags = dict( + json=({'NbserverListApp': {'json': True}}, + "Produce machine-readable JSON output."), + ) + + json = Bool(False, config=True, + help="If True, each line of output will be a JSON object with the " + "details from the server info file.") + + def start(self): + if not self.json: + print("Currently running servers:") + for serverinfo in discover_running_servers(self.profile): + if self.json: + print(json.dumps(serverinfo)) + else: + print(serverinfo['url'], "::", serverinfo['notebookdir']) #----------------------------------------------------------------------------- # Aliases and Flags @@ -286,6 +307,10 @@ class NotebookApp(BaseIPythonApplication): FileNotebookManager] flags = Dict(flags) aliases = Dict(aliases) + + subcommands = dict( + list=(NbserverListApp, NbserverListApp.description.splitlines()[0]), + ) kernel_argv = List(Unicode) @@ -746,6 +771,9 @@ class NotebookApp(BaseIPythonApplication): This method takes no arguments so all configuration and initialization must be done prior to calling this method.""" + if self.subapp is not None: + return self.subapp.start() + info = self.log.info for line in self.notebook_info().split("\n"): info(line) @@ -789,7 +817,7 @@ class NotebookApp(BaseIPythonApplication): self.remove_server_info_file() -def discover_running_servers(profile='default'): +def list_running_servers(profile='default'): """Iterate over the server info files of running notebook servers. Given a profile name, find nbserver-* files in the security directory of diff --git a/IPython/html/tests/test_notebookapp.py b/IPython/html/tests/test_notebookapp.py index c5f8c07..4422da9 100644 --- a/IPython/html/tests/test_notebookapp.py +++ b/IPython/html/tests/test_notebookapp.py @@ -27,7 +27,7 @@ def test_help_output(): def test_server_info_file(): nbapp = notebookapp.NotebookApp(profile='nbserver_file_test') def get_servers(): - return list(notebookapp.discover_running_servers(profile='nbserver_file_test')) + return list(notebookapp.list_running_servers(profile='nbserver_file_test')) nbapp.initialize(argv=[]) nbapp.write_server_info_file() servers = get_servers()