Skip to content

Commit d2d50f1

Browse files
authored
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
1 parent cbfc4da commit d2d50f1

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

notebook/notebookapp.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -789,10 +789,28 @@ def _validate_ip(self, proposal):
789789
or containerized setups for example).""")
790790
)
791791

792-
port = Integer(DEFAULT_NOTEBOOK_PORT, config=True,
793-
help=_("The port the notebook server will listen on.")
792+
port_env = 'JUPYTER_PORT'
793+
port_default_value = DEFAULT_NOTEBOOK_PORT
794+
port = Integer(port_default_value, config=True,
795+
help=_("The port the notebook server will listen on (env: JUPYTER_PORT).")
796+
)
797+
798+
@default('port')
799+
def port_default(self):
800+
return int(os.getenv(self.port_env, self.port_default_value))
801+
802+
port_retries_env = 'JUPYTER_PORT_RETRIES'
803+
port_retries_default_value = 50
804+
port_retries = Integer(port_retries_default_value, config=True,
805+
help=_("The number of additional ports to try if the specified port is not "
806+
"available (env: JUPYTER_PORT_RETRIES).")
794807
)
795808

809+
@default('port_retries')
810+
def port_retries_default(self):
811+
return int(os.getenv(self.port_retries_env, self.port_retries_default_value))
812+
813+
796814
sock = Unicode(u'', config=True,
797815
help=_("The UNIX socket the notebook server will listen on.")
798816
)
@@ -823,9 +841,6 @@ def _validate_sock_mode(self, proposal):
823841
)
824842
return value
825843

826-
port_retries = Integer(50, config=True,
827-
help=_("The number of additional ports to try if the specified port is not available.")
828-
)
829844

830845
certfile = Unicode(u'', config=True,
831846
help=_("""The full path to an SSL/TLS certificate file.""")

0 commit comments

Comments
 (0)