@@ -77,8 +77,6 @@ class NBViewer(Application):
77
77
78
78
name = Unicode ('nbviewer' )
79
79
80
- config_file = Unicode ('nbviewer_config.py' , help = "The config file to load" ).tag (config = True )
81
-
82
80
# Use this to insert custom configuration of handlers for NBViewer extensions
83
81
handler_settings = Dict ().tag (config = True )
84
82
@@ -335,9 +333,47 @@ def init_tornado_application(self):
335
333
# create the app
336
334
self .tornado_application = web .Application (handlers , debug = options .debug , ** settings )
337
335
336
+ # Mostly copied from JupyterHub because if it isn't broken then don't fix it
337
+ def write_config (self ):
338
+ """Write our default config to a .py config file"""
339
+ config_file_dir = os .path .dirname (os .path .abspath (options .config_file ))
340
+ if not os .path .isdir (config_file_dir ):
341
+ self .exit ("{} does not exist. The destination directory must exist before generating config file." .format (config_file_dir ))
342
+ if os .path .exists (options .config_file ) and not options .answer_yes :
343
+ answer = ''
344
+
345
+ def ask ():
346
+ prompt = "Overwrite %s with default config? [y/N]" % options .config_file
347
+ try :
348
+ return input (prompt ).lower () or 'n'
349
+ except KeyboardInterrupt :
350
+ print ('' ) # empty line
351
+ return 'n'
352
+
353
+ answer = ask ()
354
+ while not answer .startswith (('y' , 'n' )):
355
+ print ("Please answer 'yes' or 'no'" )
356
+ answer = ask ()
357
+ if answer .startswith ('n' ):
358
+ self .exit ("Not overwriting config file with default." )
359
+
360
+ # Inherited method from traitlets.Application
361
+ config_text = self .generate_config_file ()
362
+ if isinstance (config_text , bytes ):
363
+ config_text = config_text .decode ('utf8' )
364
+ print ("Writing default config to: %s" % options .config_file )
365
+ with open (options .config_file , mode = 'w' ) as f :
366
+ f .write (config_text )
367
+ self .exit ("Wrote default config file." )
368
+
338
369
def __init__ (self , * args , ** kwargs ):
339
370
super ().__init__ (* args , ** kwargs )
340
- self .load_config_file (self .config_file )
371
+
372
+ if options .generate_config :
373
+ self .write_config_file ()
374
+
375
+ # Inherited method from traitlets.Application
376
+ self .load_config_file (options .config_file )
341
377
self .init_tornado_application ()
342
378
343
379
def init_options ():
@@ -353,14 +389,17 @@ def init_options():
353
389
else :
354
390
default_host , default_port = '0.0.0.0' , 5000
355
391
392
+ define ("answer_yes" , default = False , help = "Answer yes to any questions (e.g. confirm overwrite)." , type = bool )
356
393
define ("base_url" , default = '/' , help = 'URL base for the server' )
357
394
define ("binder_base_url" , default = "https://mybinder.org/v2" , help = "URL base for binder notebook execution service" , type = str )
358
395
define ("cache_expiry_max" , default = 2 * 60 * 60 , help = "maximum cache expiry (seconds)" , type = int )
359
396
define ("cache_expiry_min" , default = 10 * 60 , help = "minimum cache expiry (seconds)" , type = int )
397
+ define ("config_file" , default = 'nbviewer_config.py' , help = "The config file to load" , type = str )
360
398
define ("content_security_policy" , default = "connect-src 'none';" , help = "Content-Security-Policy header setting" , type = str )
361
399
define ("debug" , default = False , help = "run in debug mode" , type = bool )
362
400
define ("default_format" , default = "html" , help = "format to use for legacy / URLs" , type = str )
363
401
define ("frontpage" , default = FRONTPAGE_JSON , help = "path to json file containing frontpage content" , type = str )
402
+ define ("generate_config" , default = False , help = "Generate default config file and then stop." , type = bool )
364
403
define ("host" , default = default_host , help = "run on the given interface" , type = str )
365
404
define ("ipywidgets_base_url" , default = "https://unpkg.com/" , help = "URL base for ipywidgets JS package" , type = str )
366
405
define ("jupyter_js_widgets_version" , default = "*" , help = "Version specifier for jupyter-js-widgets JS package" , type = str )
0 commit comments