From d2d50f127a5361ec16ab2a91b775443bfca80e55 Mon Sep 17 00:00:00 2001 From: Kevin Bates Date: Sat, 16 May 2020 01:44:39 -0700 Subject: [PATCH] Add env variable support for port options (#5221) * Add env variable support for port options In order to better support use cases relating to containerized environments, this change adds environment variable support for the `--port` option. Since anyone setting a specific port probably doesn't want port retry logic enabled, the `--port-retries` option has also been backed by an env. Option `--port` will be backed by env `JUPYTER_PORT` and still defaults to `8888`. Option `--port-retries` will be backed by env `JUPYTER_PORT_RETRIES` and still defaults to `50`. The CLI options will override those set via the envrionment, but environment values override those set via configuration files. Closes #5212 * Fixup after merge --- notebook/notebookapp.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/notebook/notebookapp.py b/notebook/notebookapp.py index 68c9e1a40..0c3c3ea34 100755 --- a/notebook/notebookapp.py +++ b/notebook/notebookapp.py @@ -789,10 +789,28 @@ class NotebookApp(JupyterApp): or containerized setups for example).""") ) - port = Integer(DEFAULT_NOTEBOOK_PORT, config=True, - help=_("The port the notebook server will listen on.") + port_env = 'JUPYTER_PORT' + port_default_value = DEFAULT_NOTEBOOK_PORT + port = Integer(port_default_value, config=True, + help=_("The port the notebook server will listen on (env: JUPYTER_PORT).") + ) + + @default('port') + def port_default(self): + return int(os.getenv(self.port_env, self.port_default_value)) + + port_retries_env = 'JUPYTER_PORT_RETRIES' + port_retries_default_value = 50 + port_retries = Integer(port_retries_default_value, config=True, + help=_("The number of additional ports to try if the specified port is not " + "available (env: JUPYTER_PORT_RETRIES).") ) + @default('port_retries') + def port_retries_default(self): + return int(os.getenv(self.port_retries_env, self.port_retries_default_value)) + + sock = Unicode(u'', config=True, help=_("The UNIX socket the notebook server will listen on.") ) @@ -823,9 +841,6 @@ class NotebookApp(JupyterApp): ) return value - port_retries = Integer(50, config=True, - help=_("The number of additional ports to try if the specified port is not available.") - ) certfile = Unicode(u'', config=True, help=_("""The full path to an SSL/TLS certificate file.""")