Skip to content

Commit f216147

Browse files
authored
Merge pull request #889 from krinsman/step7_9
Step7_9 (Create function calculating default host and port)
2 parents 027dfba + ed2f8ea commit f216147

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ If you'd like to write a new format, open a ticket, or speak up on [gitter](http
191191
We have some work yet to do to support your next big thing in notebook
192192
publishing, and we'd love to hear from you.
193193

194-
#### Config file
194+
## Config File and Command Line Configuration
195195

196196
NBViewer is configurable using a config file, by default called `nbviewer_config.py`. You can modify the name and location of the config file that NBViewer looks for using the `--config-file` command line flag. (The location is always a relative path, i.e. relative to where the command `python -m nbviewer` is run, and never an absolute path.)
197197

nbviewer/app.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,15 @@ def init_options():
382382
# already run
383383
return
384384

385-
# check if JupyterHub service options are available to use as defaults
386-
if 'JUPYTERHUB_SERVICE_URL' in os.environ:
387-
url = urlparse(os.environ['JUPYTERHUB_SERVICE_URL'])
388-
default_host, default_port = url.hostname, url.port
389-
else:
390-
default_host, default_port = '0.0.0.0', 5000
385+
# Make this a cached property of NBViewer during traitlets refactor
386+
def default_endpoint():
387+
# check if JupyterHub service options are available to use as defaults
388+
if 'JUPYTERHUB_SERVICE_URL' in os.environ:
389+
url = urlparse(os.environ['JUPYTERHUB_SERVICE_URL'])
390+
default_host, default_port = url.hostname, url.port
391+
else:
392+
default_host, default_port = '0.0.0.0', 5000
393+
return {'host': default_host, 'port': default_port}
391394

392395
define("answer_yes", default=False, help="Answer yes to any questions (e.g. confirm overwrite).", type=bool)
393396
define("base_url", default='/', help='URL base for the server')
@@ -400,7 +403,7 @@ def init_options():
400403
define("default_format", default="html", help="format to use for legacy / URLs", type=str)
401404
define("frontpage", default=FRONTPAGE_JSON, help="path to json file containing frontpage content", type=str)
402405
define("generate_config", default=False, help="Generate default config file and then stop.", type=bool)
403-
define("host", default=default_host, help="run on the given interface", type=str)
406+
define("host", default=default_endpoint()['host'], help="run on the given interface", type=str)
404407
define("ipywidgets_base_url", default="https://unpkg.com/", help="URL base for ipywidgets JS package", type=str)
405408
define("jupyter_js_widgets_version", default="*", help="Version specifier for jupyter-js-widgets JS package", type=str)
406409
define("jupyter_widgets_html_manager_version", default="*", help="Version specifier for @jupyter-widgets/html-manager JS package", type=str)
@@ -411,7 +414,7 @@ def init_options():
411414
define("mc_threads", default=1, help="number of threads to use for Async Memcache", type=int)
412415
define("no_cache", default=False, help="Do not cache results", type=bool)
413416
define("no_check_certificate", default=False, help="Do not validate SSL certificates", type=bool)
414-
define("port", default=default_port, help="run on the given port", type=int)
417+
define("port", default=default_endpoint()['port'], help="run on the given port", type=int)
415418
define("processes", default=0, help="use processes instead of threads for rendering", type=int)
416419
define("provider_rewrites", default=default_rewrites, help="Full dotted package(s) that provide `uri_rewrites`", type=str, multiple=True, group="provider")
417420
define("providers", default=default_providers, help="Full dotted package(s) that provide `default_handlers`", type=str, multiple=True, group="provider")

0 commit comments

Comments
 (0)