Skip to content

Commit

Permalink
simplify handling of config
Browse files Browse the repository at this point in the history
  • Loading branch information
Zsailer committed Aug 3, 2019
1 parent 44cccc7 commit 5358a80
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 69 deletions.
55 changes: 9 additions & 46 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ def _(x):
class Voila(ExtensionApp):
name = 'voila'
version = __version__
examples = 'voila example.ipynb --port 8888'

examples = 'jupyter voila example.ipynb'
extension_name = 'voila'

flags = {
'no-browser': ({'Voila': {'open_browser': False}}, _('Don\'t open the notebook in a browser after startup.'))
'no-browser': ({'Voila': {'open_browser': False}}, _('Don\'t open the notebook in a browser after startup.')),
'standalone': ({'ServerApp': {'standalone': True}}, _('Run the server without enabling extensions.'))
}

description = Unicode(
Expand Down Expand Up @@ -100,15 +100,13 @@ class Voila(ExtensionApp):
)
)
aliases = {
'port': 'Voila.port',
'port': 'ServerApp.port',
'static': 'Voila.static_root',
'strip_sources': 'VoilaConfiguration.strip_sources',
'strip_sources': 'Voila.strip_sources',
'autoreload': 'Voila.autoreload',
'template': 'VoilaConfiguration.template',
'theme': 'VoilaConfiguration.theme',
'base_url': 'Voila.base_url',
'server_url': 'Voila.server_url',
'enable_nbextensions': 'VoilaConfiguration.enable_nbextensions'
'template': 'Voila.template',
'theme': 'Voila.theme',
'enable_nbextensions': 'Voila.enable_nbextensions',
}

template = Unicode(
Expand All @@ -132,27 +130,6 @@ class Voila(ExtensionApp):
)
connection_dir = Unicode()

base_url = Unicode(
'/',
config=True,
help=_(
'Path for voila API calls. If server_url is unset, this will be \
used for both the base route of the server and the client. \
If server_url is set, the server will server the routes prefixed \
by server_url, while the client will prefix by base_url (this is \
useful in reverse proxies).'
)
)

server_url = Unicode(
None,
config=True,
allow_none=True,
help=_(
'Path to prefix to voila API handlers. Leave unset to default to base_url'
)
)

notebook_path = Unicode(
None,
config=True,
Expand Down Expand Up @@ -258,23 +235,9 @@ def initialize_templates(self):
nbui = gettext.translation('nbui', localedir=os.path.join(ROOT, 'i18n'), fallback=True)
env.install_gettext_translations(nbui, newstyle=False)

template_settings = dict(
voila_template_paths=self.template_paths,
voila_jinja2_env=env,
nbconvert_template_paths=self.nbconvert_template_paths
)
template_settings = {'voila_jinja2_env': env}
self.settings.update(**template_settings)

def initialize_settings(self):
voila_configuration = dict(
template=self.template,
theme=self.theme,
strip_sources=self.strip_sources,
enable_nbextensions=self.enable_nbextensions,
notebook_path=self.notebook_path,
)
self.settings['voila_configuration'] = voila_configuration

def initialize_handlers(self):
handlers = [
('/voila/render' + path_regex, VoilaHandler),
Expand Down
29 changes: 6 additions & 23 deletions voila/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,14 @@


class VoilaHandler(ExtensionHandler):

@property
def notebook_path(self):
return self.settings.get('notebook_path', [])

@property
def nbconvert_template_paths(self):
return self.settings.get('nbconvert_template_paths', [])

@property
def exporter_config(self):
return self.settings.get('config', None)

@property
def voila_configuration(self):
return self.settings['voila_configuration']


@tornado.web.authenticated
@tornado.gen.coroutine
def get(self, path=None):
# if the handler got a notebook_path argument, always serve that
notebook_path = self.notebook_path or path
notebook_path = self.config.notebook_path or path

if self.voila_configuration['enable_nbextensions']:
if self.config.enable_nbextensions:
# generate a list of nbextensions that are enabled for the classical notebook
# a template can use that to load classical notebook extensions, but does not have to
notebook_config = self.config_manager.get('notebook')
Expand Down Expand Up @@ -73,16 +56,16 @@ def get(self, path=None):
'kernel_id': kernel_id,
'base_url': self.base_url,
'nbextensions': nbextensions,
'theme': self.voila_configuration['theme']
'theme': self.config.theme
}

exporter = VoilaExporter(
template_path=self.nbconvert_template_paths,
config=self.exporter_config,
template_path=self.config.nbconvert_template_paths,
config=self.server_config,
contents_manager=self.contents_manager # for the image inlining
)

if self.voila_configuration['strip_sources']:
if self.config.strip_sources:
exporter.exclude_input = True
exporter.exclude_output_prompt = True
exporter.exclude_input_prompt = True
Expand Down

0 comments on commit 5358a80

Please sign in to comment.