def jp_large_contents_manager(tmp_path):
    """Returns a LargeFileManager instance."""
    return LargeFileManager(root_dir=str(tmp_path))
Пример #2
0
    def start(self):
        self.connection_dir = tempfile.mkdtemp(prefix='voila_',
                                               dir=self.connection_dir_root)
        self.log.info('Storing connection files in %s.' % self.connection_dir)
        self.log.info('Serving static files from %s.' % self.static_root)

        self.kernel_spec_manager = KernelSpecManager(parent=self)

        self.kernel_manager = MappingKernelManager(
            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,
            allow_remote_access=True,
            autoreload=self.autoreload,
            voila_jinja2_env=env,
            jinja2_env=env,
            static_path='/',
            server_root_dir='/',
            contents_manager=self.contents_manager,
            config_manager=self.config_manager)

        self.app.settings.update(self.tornado_settings)

        handlers = []

        handlers.extend([
            (url_path_join(self.server_url,
                           r'/api/kernels/%s' % _kernel_id_regex),
             KernelHandler),
            (url_path_join(self.server_url,
                           r'/api/kernels/%s/channels' % _kernel_id_regex),
             ZMQChannelsHandler),
            (url_path_join(self.server_url,
                           r'/voila/static/(.*)'), MultiStaticFileHandler, {
                               'paths': self.static_paths,
                               'default_filename': 'index.html'
                           })
        ])

        # Serving notebook extensions
        if self.voila_configuration.enable_nbextensions:
            handlers.append((
                url_path_join(self.server_url, r'/voila/nbextensions/(.*)'),
                FileFindHandler,
                {
                    'path': self.nbextensions_path,
                    'no_cache_paths':
                    ['/'],  # don't cache anything in nbextensions
                },
            ))
        handlers.append((
            url_path_join(self.server_url, r'/voila/files/(.*)'),
            WhiteListFileHandler,
            {
                'whitelist': self.voila_configuration.file_whitelist,
                'blacklist': self.voila_configuration.file_blacklist,
                'path': self.root_dir,
            },
        ))

        tree_handler_conf = {'voila_configuration': self.voila_configuration}
        if self.notebook_path:
            handlers.append(
                (url_path_join(self.server_url, r'/(.*)'), VoilaHandler, {
                    'notebook_path':
                    os.path.relpath(self.notebook_path, self.root_dir),
                    'nbconvert_template_paths':
                    self.nbconvert_template_paths,
                    'config':
                    self.config,
                    'voila_configuration':
                    self.voila_configuration
                }))
        else:
            self.log.debug('serving directory: %r', self.root_dir)
            handlers.extend([
                (self.server_url, VoilaTreeHandler, tree_handler_conf),
                (url_path_join(self.server_url, r'/voila/tree' + path_regex),
                 VoilaTreeHandler, tree_handler_conf),
                (url_path_join(self.server_url, r'/voila/render/(.*)'),
                 VoilaHandler, {
                     'nbconvert_template_paths': self.nbconvert_template_paths,
                     'config': self.config,
                     'voila_configuration': self.voila_configuration
                 }),
            ])

        self.app.add_handlers('.*$', handlers)
        self.listen()
Пример #3
0
    def start(self):
        self.connection_dir = tempfile.mkdtemp(prefix='voila_',
                                               dir=self.connection_dir_root)
        self.log.info('Storing connection files in %s.' % self.connection_dir)
        self.log.info('Serving static files from %s.' % self.static_root)

        kernel_manager = MappingKernelManager(
            connection_dir=self.connection_dir,
            allowed_message_types=[
                '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)
        contents_manager = LargeFileManager(
            parent=self)  # TODO: make this configurable like notebook

        # 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)

        self.app = tornado.web.Application(kernel_manager=kernel_manager,
                                           allow_remote_access=True,
                                           autoreload=self.autoreload,
                                           voila_jinja2_env=env,
                                           jinja2_env=env,
                                           static_path='/',
                                           server_root_dir='/',
                                           contents_manager=contents_manager,
                                           config_manager=self.config_manager)

        base_url = self.app.settings.get('base_url', '/')

        handlers = []

        handlers.extend([
            (url_path_join(base_url, r'/api/kernels/%s' % _kernel_id_regex),
             KernelHandler),
            (url_path_join(base_url,
                           r'/api/kernels/%s/channels' % _kernel_id_regex),
             ZMQChannelsHandler),
            (url_path_join(base_url,
                           r'/voila/static/(.*)'), MultiStaticFileHandler, {
                               'paths': self.static_paths,
                               'default_filename': 'index.html'
                           })
        ])

        # this handler serves the nbextensions similar to the classical notebook
        handlers.append((
            url_path_join(base_url, r'/voila/nbextensions/(.*)'),
            FileFindHandler,
            {
                'path': self.nbextensions_path,
                'no_cache_paths':
                ['/'],  # don't cache anything in nbextensions
            },
        ))

        if self.notebook_path:
            handlers.append((url_path_join(base_url, r'/'), VoilaHandler, {
                'notebook_path':
                os.path.relpath(self.notebook_path, self.root_dir),
                'strip_sources':
                self.strip_sources,
                'nbconvert_template_paths':
                self.nbconvert_template_paths,
                'config':
                self.config
            }))
        else:
            handlers.extend([
                (base_url, VoilaTreeHandler),
                (url_path_join(base_url,
                               r'/voila/tree' + path_regex), VoilaTreeHandler),
                (url_path_join(base_url,
                               r'/voila/render' + path_regex), VoilaHandler, {
                                   'strip_sources': self.strip_sources
                               }),
            ])

        self.app.add_handlers('.*$', handlers)
        self.listen()
Пример #4
0
    def start(self):
        connection_dir = tempfile.mkdtemp(prefix='voila_',
                                          dir=self.connection_dir_root)
        self.log.info('Storing connection files in %s.' % connection_dir)
        self.log.info('Serving static files from %s.' % self.static_root)

        kernel_manager = MappingKernelManager(connection_dir=connection_dir,
                                              allowed_message_types=[
                                                  '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(TEMPLATE_ROOT),
                                 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)
        contents_manager = LargeFileManager(
        )  # TODO: make this configurable like notebook

        webapp = tornado.web.Application(kernel_manager=kernel_manager,
                                         allow_remote_access=True,
                                         autoreload=self.autoreload,
                                         voila_jinja2_env=env,
                                         jinja2_env=env,
                                         static_path='/',
                                         server_root_dir='/',
                                         contents_manager=contents_manager)

        base_url = webapp.settings.get('base_url', '/')

        handlers = []

        handlers.extend([
            (url_path_join(base_url, r'/api/kernels/%s' % _kernel_id_regex),
             KernelHandler),
            (url_path_join(base_url,
                           r'/api/kernels/%s/channels' % _kernel_id_regex),
             ZMQChannelsHandler),
            (url_path_join(base_url, r'/voila/static/(.*)'),
             tornado.web.StaticFileHandler, {
                 'path': self.static_root,
                 'default_filename': 'index.html'
             })
        ])

        if self.notebook_path:
            handlers.append((url_path_join(base_url, r'/'), VoilaHandler, {
                'notebook_path': self.notebook_path,
                'strip_sources': self.strip_sources,
                'custom_template_path': self.custom_template_path,
                'config': self.config
            }))
        else:
            handlers.extend([
                (base_url, VoilaTreeHandler),
                (url_path_join(base_url,
                               r'/voila/tree' + path_regex), VoilaTreeHandler),
                (url_path_join(base_url,
                               r'/voila/render' + path_regex), VoilaHandler, {
                                   'strip_sources': self.strip_sources
                               }),
            ])

        webapp.add_handlers('.*$', handlers)

        webapp.listen(self.port)
        self.log.info('Voila listening on port %s.' % self.port)

        try:
            tornado.ioloop.IOLoop.current().start()
        finally:
            shutil.rmtree(connection_dir)
import pytest
import tornado

from jupyter_server.services.contents.largefilemanager import LargeFileManager
from ...utils import expected_http_error

contents_manager = pytest.fixture(
    lambda tmp_path: LargeFileManager(root_dir=str(tmp_path)))


def test_save(contents_manager):
    cm = contents_manager
    model = cm.new_untitled(type='notebook')
    name = model['name']
    path = model['path']

    # Get the model with 'content'
    full_model = cm.get(path)
    # Save the notebook
    model = cm.save(full_model, path)
    assert isinstance(model, dict)
    assert 'name' in model
    assert 'path' in model
    assert model['name'] == name
    assert model['path'] == path


@pytest.mark.parametrize('model,err_message', [
    ({
        'name': 'test',
        'path': 'test',