import json
import os
import sys
sys.path.append('/root/.jupyter/extensions/')

c.JupyterApp.ip = '*'
c.JupyterApp.port = 80
c.JupyterApp.open_browser = False
c.JupyterApp.allow_credentials = True
c.JupyterApp.nbserver_extensions = ['jupyter_nbgallery.status', 'jupyter_nbgallery.post']
c.JupyterApp.reraise_server_extension_failures = True
c.JupyterApp.extra_static_paths = ['/root/.jupyter/static']
c.JupyterApp.extra_nbextensions_path = ['/root/.jupyter/extensions/']
c.JupyterApp.tornado_settings = {'static_url_prefix': '/Jupyter/static/'}
c.JupyterApp.allow_origin = 'https://nb.gallery'

# needed to receive notebooks from the gallery
c.JupyterApp.disable_check_xsrf = True

def load_config():
  return json.loads(open('/root/.jupyter/nbconfig/common.json').read())

def save_config(config):
  with open('/root/.jupyter/nbconfig/common.json', 'w') as output:
    output.write(json.dumps(config, indent=2))

# Override gallery location
nbgallery_url = os.getenv('NBGALLERY_URL')
if nbgallery_url:
  print('Setting nbgallery url to %s' % nbgallery_url)
  c.JupyterApp.allow_origin = nbgallery_url
  config = load_config()
  config['nbgallery']['url'] = nbgallery_url
  save_config(config)

# Override client name
client_name = os.getenv('NBGALLERY_CLIENT_NAME')
if client_name:
  config = load_config()
  config['nbgallery']['client']['name'] = client_name
  save_config(config)

@@ -675,16 +675,220 @@
 = True%0A%0A
+# Update config from environment%0Aconfig_prefix = 'NBGALLERY_CONFIG_'%0Afor var in %5Bx for x in os.environ if x.startswith(config_prefix)%5D:%0A  c.JupyterApp%5Bvar%5Blen(config_prefix):%5D.lower()%5D = os.environ%5Bvar%5D%0A%0A
 def load

Allow setting jupyter config from environment