How to use voila - 10 common examples

To help you get started, we’ve selected a few voila examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github voila-dashboards / voila / voila / app.py View on Github external
)
    )
    aliases = {
        'port': 'Voila.port',
        'static': 'Voila.static_root',
        'strip_sources': 'VoilaConfiguration.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'
    }
    classes = [
        VoilaConfiguration,
        VoilaExecutePreprocessor,
        VoilaExporter,
        VoilaCSSPreprocessor
    ]
    connection_dir_root = Unicode(
        config=True,
        help=_(
            'Location of temporry connection files. Defaults '
            'to system `tempfile.gettempdir()` value.'
        )
    )
    connection_dir = Unicode()

    base_url = Unicode(
        '/',
        config=True,
        help=_(
github voila-dashboards / voila / voila / handler.py View on Github external
def _jinja_cell_generator(self, nb, kernel_id):
        """Generator that will execute a single notebook cell at a time"""
        km = self.kernel_manager.get_kernel(kernel_id)

        nb, resources = ClearOutputPreprocessor().preprocess(nb, {'metadata': {'path': self.cwd}})
        ep = VoilaExecutePreprocessor(config=self.traitlet_config)

        with ep.setup_preprocessor(nb, resources, km=km):
            for cell_idx, cell in enumerate(nb.cells):
                res = ep.preprocess_cell(cell, resources, cell_idx, store_history=False)

                yield res[0]
github voila-dashboards / voila / voila / execute.py View on Github external
def executenb(nb, cwd=None, km=None, **kwargs):
    resources = {}
    if cwd is not None:
        resources['metadata'] = {'path': cwd}  # pragma: no cover
    # Clear any stale output, in case of exception
    nb, resources = ClearOutputPreprocessor().preprocess(nb, resources)
    ep = VoilaExecutePreprocessor(**kwargs)
    return ep.preprocess(nb, resources, km=km)[0]
github voila-dashboards / voila / voila / execute.py View on Github external
def __init__(self, **kwargs):
        super(VoilaExecutePreprocessor, self).__init__(**kwargs)
        self.output_hook_stack = collections.defaultdict(list)  # maps to list of hooks, where the last is used
        self.output_objects = {}
github voila-dashboards / voila / voila / app.py View on Github external
parent=self,
            connection_dir=self.connection_dir,
            kernel_spec_manager=self.kernel_spec_manager,
            allowed_message_types=[
                'comm_open',
                'comm_close',
                'comm_msg',
                'comm_info_request',
                'kernel_info_request',
                'shutdown_request'
            ]
        )

        jenv_opt = {"autoescape": True}  # we might want extra options via cmd line like notebook server
        env = jinja2.Environment(loader=jinja2.FileSystemLoader(self.template_paths), extensions=['jinja2.ext.i18n'], **jenv_opt)
        nbui = gettext.translation('nbui', localedir=os.path.join(ROOT, 'i18n'), fallback=True)
        env.install_gettext_translations(nbui, newstyle=False)
        self.contents_manager = LargeFileManager(parent=self)

        # we create a config manager that load both the serverconfig and nbconfig (classical notebook)
        read_config_path = [os.path.join(p, 'serverconfig') for p in jupyter_config_path()]
        read_config_path += [os.path.join(p, 'nbconfig') for p in jupyter_config_path()]
        self.config_manager = ConfigManager(parent=self, read_config_path=read_config_path)

        # default server_url to base_url
        self.server_url = self.server_url or self.base_url

        self.app = tornado.web.Application(
            base_url=self.base_url,
            server_url=self.server_url or self.base_url,
            kernel_manager=self.kernel_manager,
            kernel_spec_manager=self.kernel_spec_manager,
github voila-dashboards / voila / voila / watchdog.py View on Github external
self.callback = tornado.ioloop.PeriodicCallback(lambda: self.ping(''), 6000)
        path = path.strip('/') + '.ipynb'
        if path not in event_handlers:
            handlers = []
            watchdog_observer = watchdog.observers.Observer()
            # sometimes useful to add this when triggering does not work
            from watchdog.events import LoggingEventHandler
            logging_handler = LoggingEventHandler()
            watchdog_observer.schedule(logging_handler, '.', recursive=True)

            notebook_handler = WatchDogEventHandler(regexes=['\\./' + path])
            watchdog_observer.schedule(notebook_handler, '.', recursive=True)
            handlers.append(notebook_handler)
            
            misc_handler = WatchDogEventHandler(regexes=[str(ROOT) +r'/templates/.*', str(ROOT / 'static/main.js'), str(ROOT / 'static/dist/libwidgets.js')])
            watchdog_observer.schedule(misc_handler, str(ROOT), recursive=True)
            handlers.append(misc_handler)
            
            watchdog_observer.start()
            event_handlers[path] = handlers
            
            tornado.autoreload.add_reload_hook(self._on_reload)

        self.handlers = event_handlers[path]
        for handler in self.handlers:
            handler.listeners.append(self)
github voila-dashboards / voila / voila / server_extension.py View on Github external
template_paths = []

    # common configuration options between the server extension and the application
    voila_configuration = VoilaConfiguration(parent=server_app)
    collect_template_paths(
        nbconvert_template_paths,
        static_paths,
        template_paths,
        voila_configuration.template
    )

    jenv_opt = {"autoescape": True}
    env = Environment(loader=FileSystemLoader(template_paths), extensions=['jinja2.ext.i18n'], **jenv_opt)
    web_app.settings['voila_jinja2_env'] = env

    nbui = gettext.translation('nbui', localedir=os.path.join(ROOT, 'i18n'), fallback=True)
    env.install_gettext_translations(nbui, newstyle=False)

    host_pattern = '.*$'
    base_url = url_path_join(web_app.settings['base_url'])

    tree_handler_conf = {
        'voila_configuration': voila_configuration
    }
    web_app.add_handlers(host_pattern, [
        (url_path_join(base_url, '/voila/render/(.*)'), VoilaHandler, {
            'config': server_app.config,
            'nbconvert_template_paths': nbconvert_template_paths,
            'voila_configuration': voila_configuration
        }),
        (url_path_join(base_url, '/voila'), VoilaTreeHandler, tree_handler_conf),
        (url_path_join(base_url, '/voila/tree' + path_regex), VoilaTreeHandler, tree_handler_conf),
github voila-dashboards / voila / voila / watchdog.py View on Github external
def open(self, path=''):
        self.callback = tornado.ioloop.PeriodicCallback(lambda: self.ping(''), 6000)
        path = path.strip('/') + '.ipynb'
        if path not in event_handlers:
            handlers = []
            watchdog_observer = watchdog.observers.Observer()
            # sometimes useful to add this when triggering does not work
            from watchdog.events import LoggingEventHandler
            logging_handler = LoggingEventHandler()
            watchdog_observer.schedule(logging_handler, '.', recursive=True)

            notebook_handler = WatchDogEventHandler(regexes=['\\./' + path])
            watchdog_observer.schedule(notebook_handler, '.', recursive=True)
            handlers.append(notebook_handler)
            
            misc_handler = WatchDogEventHandler(regexes=[str(ROOT) +r'/templates/.*', str(ROOT / 'static/main.js'), str(ROOT / 'static/dist/libwidgets.js')])
            watchdog_observer.schedule(misc_handler, str(ROOT), recursive=True)
            handlers.append(misc_handler)
            
            watchdog_observer.start()
            event_handlers[path] = handlers
            
            tornado.autoreload.add_reload_hook(self._on_reload)

        self.handlers = event_handlers[path]
        for handler in self.handlers:
            handler.listeners.append(self)
github voila-dashboards / voila / voila / app.py View on Github external
'Directory holding static assets (HTML, JS and CSS files).'
        )
    )
    aliases = {
        'port': 'Voila.port',
        'static': 'Voila.static_root',
        'strip_sources': 'VoilaConfiguration.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'
    }
    classes = [
        VoilaConfiguration,
        VoilaExecutePreprocessor,
        VoilaExporter,
        VoilaCSSPreprocessor
    ]
    connection_dir_root = Unicode(
        config=True,
        help=_(
            'Location of temporry connection files. Defaults '
            'to system `tempfile.gettempdir()` value.'
        )
    )
    connection_dir = Unicode()

    base_url = Unicode(
        '/',
        config=True,
github voila-dashboards / voila / voila / app.py View on Github external
arg = self.extra_args[0]
            # I am not sure why we need to check if self.notebook_path is set, can we get rid of this?
            if not self.notebook_path:
                if os.path.isdir(arg):
                    self.root_dir = arg
                elif os.path.isfile(arg):
                    self.notebook_path = arg
                else:
                    raise ValueError('argument is neither a file nor a directory: %r' % arg)
        elif len(self.extra_args) != 0:
            raise ValueError('provided more than 1 argument: %r' % self.extra_args)

        # then we load the config
        self.load_config_file('voila', path=self.config_file_paths)
        # common configuration options between the server extension and the application
        self.voila_configuration = VoilaConfiguration(parent=self)
        self.setup_template_dirs()
        signal.signal(signal.SIGTERM, self._handle_signal_stop)