Ejemplo n.º 1
0
def client(qtbot):
    """Construct a NotebookClient for use in tests."""
    plugin = MockPlugin()
    client = NotebookClient(plugin, '/path/notebooks/ham.ipynb')
    server_info = {
        'notebook_dir': '/path/notebooks',
        'url': 'fake_url',
        'token': 'fake_token'
    }
    client.register(server_info)
    return client
Ejemplo n.º 2
0
def plugin(qtbot):
    """
    Construct mock plugin with NotebookClient for use in tests.

    Use `plugin.client` to access the client.
    """
    plugin = MockPlugin()
    qtbot.addWidget(plugin)
    client = NotebookClient(plugin, '/path/notebooks/ham.ipynb')
    plugin.client = client
    server_info = {
        'notebook_dir': '/path/notebooks',
        'url': 'fake_url',
        'token': 'fake_token'
    }
    client.register(server_info)
    return plugin
Ejemplo n.º 3
0
def plugin_without_server(qtbot):
    """
    Construct mock plugin with NotebookClient but no notebook server.

    Use `plugin.client` to access the client.
    """
    plugin = MockPlugin()
    qtbot.addWidget(plugin)
    client = NotebookClient(plugin, '/path/notebooks/ham.ipynb')
    plugin.client = client
    return plugin
    def create_new_client(self, filename=None):
        """
        Create a new notebook or load a pre-existing one.

        This function also creates and selects a welcome tab, if no tabs are
        present.

        Parameters
        ----------
        filename : str, optional
            File name of the notebook to load in the new client. The default
            is None, meaning that a new notebook should be created.

        Returns
        -------
        filename : str or None
            File name of notebook that is opened, or None if unsuccessful.
        """
        # Generate the notebook name (in case of a new one)
        if not filename:
            if not osp.isdir(NOTEBOOK_TMPDIR):
                os.makedirs(NOTEBOOK_TMPDIR)
            nb_name = 'untitled' + str(self.untitled_num) + '.ipynb'
            filename = osp.join(NOTEBOOK_TMPDIR, nb_name)
            kernelspec = dict(display_name='Python 3 (Spyder)', name='python3')
            metadata = dict(kernelspec=kernelspec)
            nb_contents = nbformat.v4.new_notebook(metadata=metadata)
            nbformat.write(nb_contents, filename)
            self.untitled_num += 1

        # Open the notebook with nbopen and get the url we need to render
        try:
            server_info = nbopen(filename)
        except (subprocess.CalledProcessError, NBServerError):
            QMessageBox.critical(
                self, _("Server error"),
                _("The Jupyter Notebook server failed to start or it is "
                  "taking too much time to do it. Please start it in a "
                  "system terminal with the command 'jupyter notebook' to "
                  "check for errors."))
            # Create a welcome widget
            # See issue 93
            self.untitled_num -= 1
            self.maybe_create_welcome_client()
            return

        welcome_client = self.maybe_create_welcome_client()
        client = NotebookClient(self, filename, self.actions)
        self.add_tab(client)
        client.register(server_info)
        client.load_notebook()
        if welcome_client:
            self.setCurrentIndex(0)
        return filename
    def maybe_create_welcome_client(self):
        """
        Create a welcome tab if there are no tabs.

        Returns
        -------
        client : NotebookClient or None
            The client in the created tab, or None if no tab is created.
        """
        if self.count() == 0:
            welcome = open(WELCOME).read()
            client = NotebookClient(self,
                                    WELCOME,
                                    self.actions,
                                    ini_message=welcome)
            self.add_tab(client)
            return client
Ejemplo n.º 6
0
    def create_new_client(self, filename=None):
        """
        Create a new notebook or load a pre-existing one.

        Parameters
        ----------
        filename : str, optional
            File name of the notebook to load in the new client. The default
            is None, meaning that a new notebook should be created.

        Returns
        -------
        client : NotebookClient or None
            Notebook client that is opened, or None if unsuccessful.
        """
        # Generate the notebook name (in case of a new one)
        if not filename:
            if not osp.isdir(NOTEBOOK_TMPDIR):
                os.makedirs(NOTEBOOK_TMPDIR)
            nb_name = 'untitled' + str(self.untitled_num) + '.ipynb'
            filename = osp.join(NOTEBOOK_TMPDIR, nb_name)
            kernelspec = dict(display_name='Python 3 (Spyder)', name='python3')
            metadata = dict(kernelspec=kernelspec)
            nb_contents = nbformat.v4.new_notebook(metadata=metadata)
            nbformat.write(nb_contents, filename)
            self.untitled_num += 1

        client = NotebookClient(self, filename, self.actions)
        self.add_tab(client)
        interpreter = self.get_interpreter()
        server_info = self.server_manager.get_server(filename,
                                                     interpreter,
                                                     start=True)
        if server_info:
            logger.debug('Using existing server at %s',
                         server_info['notebook_dir'])
            client.register(server_info)
            client.load_notebook()
        return client