示例#1
0
    def convert(self, remove_executed=False):
        """
        Convert the executed notebook to a restructured text (RST) file.

        Parameters
        ----------
        delete_executed : bool, optional
            Controls whether to remove the executed notebook or not.
        """

        if not path.exists(self._executed_nb_path):
            raise IOError(
                "Executed notebook file doesn't exist! Expected: {0}".format(
                    self._executed_nb_path))

        if path.exists(self._rst_path) and not self.overwrite:
            logger.debug(
                "RST version of notebook already exists at {0}. Use "
                "overwrite=True or --overwrite (at cmd line) to re-run".format(
                    self._rst_path))
            return self._rst_path

        # Initialize the resources dict - see:
        # https://github.com/jupyter/nbconvert/blob/master/nbconvert/nbconvertapp.py#L327
        resources = {}
        resources['config_dir'] = ''  # we don't need to specify config
        resources['unique_key'] = self.nb_name

        # path to store extra files, like plots generated
        resources['output_files_dir'] = 'nboutput'

        # Exports the notebook to RST
        logger.debug('Exporting notebook to RST...')
        exporter = RSTExporter()

        if self.template_file:
            exporter.template_file = self.template_file
        output, resources = exporter.from_filename(self._executed_nb_path,
                                                   resources=resources)

        # Write the output RST file
        writer = FilesWriter()
        output_file_path = writer.write(output,
                                        resources,
                                        notebook_name=self.nb_name)

        if remove_executed:  # optionally, clean up the executed notebook file
            remove(self._executed_nb_path)

        return output_file_path
示例#2
0
    def convert(self):
        """
        Converts the executed notebook to a restructured text (RST) file.
        Returns
        -------
        output_file_path : str``
            The path to the converted notebook
        """

        # Only convert if executed notebook exists
        if not os.path.exists(self.executed_nb_path):
            raise IOError(
                "Executed notebook file doesn't exist! Expected: {0}".format(
                    self.executed_nb_path))

        # Initialize the resources dict
        resources = dict()
        resources['unique_key'] = self.nb_name

        # path to store extra files, like plots generated
        resources['output_files_dir'] = 'nboutput/'

        # Exports the notebook to RST
        _logger.info("Exporting executed notebook to RST format")
        exporter = RSTExporter()

        # If a RST template file has been specified use this template
        if self.rst_template:
            exporter.template_file = self.rst_template

        output, resources = exporter.from_filename(self.executed_nb_path,
                                                   resources=resources)

        # Write the output RST file
        writer = FilesWriter()
        output_file_path = writer.write(output,
                                        resources,
                                        notebook_name=self.nb_name)

        return output_file_path
示例#3
0
    def convert(self, remove_executed=False):
        """
        Convert the executed notebook to a restructured text (RST) file.

        Parameters
        ----------
        delete_executed : bool, optional
            Controls whether to remove the executed notebook or not.
        """

        if not path.exists(self._executed_nb_path):
            raise IOError("Executed notebook file doesn't exist! Expected: {0}"
                          .format(self._executed_nb_path))

        if path.exists(self._rst_path) and not self.overwrite:
            logger.debug("RST version of notebook already exists at {0}. Use "
                         "overwrite=True or --overwrite (at cmd line) to re-run"
                         .format(self._rst_path))
            return self._rst_path

        # Initialize the resources dict - see:
        # https://github.com/jupyter/nbconvert/blob/master/nbconvert/nbconvertapp.py#L327
        resources = {}
        resources['config_dir'] = '' # we don't need to specify config
        resources['unique_key'] = self.nb_name

        # path to store extra files, like plots generated
        resources['output_files_dir'] = 'nboutput'

        # Exports the notebook to RST
        logger.debug('Exporting notebook to RST...')
        exporter = RSTExporter()

        if self.template_file:
            exporter.template_file = self.template_file
        output, resources = exporter.from_filename(self._executed_nb_path,
                                                   resources=resources)

        # Write the output RST file
        writer = FilesWriter()
        output_file_path = writer.write(output, resources,
                                        notebook_name=self.nb_name)

        # read the executed notebook, grab the keywords from the header,
        # add them in to the RST as filter keywords
        with open(self._executed_nb_path) as f:
            nb = nbformat.read(f, as_version=IPYTHON_VERSION)

        top_cell_text = nb['cells'][0]['source']
        match = re.search('## [kK]eywords\s+(.*)', top_cell_text)

        if match:
            keywords = match.groups()[0].split(',')
            keywords = [clean_keyword(k) for k in keywords if k.strip()]
            keyword_filters = ['filter{0}'.format(k) for k in keywords]
        else:
            keyword_filters = []

        # Add metatags to top of RST files to get rendered into HTML, used for
        # the search and filter functionality in Learn Astropy
        meta_tutorials = '.. meta::\n    :keywords: {0}\n'
        filters = ['filterTutorials'] + keyword_filters
        meta_tutorials = meta_tutorials.format(', '.join(filters))
        with open(output_file_path, 'r') as f:
            rst_text = f.read()

        with open(output_file_path, 'w') as f:
            rst_text = '{0}\n{1}'.format(meta_tutorials, rst_text)
            f.write(rst_text)

        if remove_executed: # optionally, clean up the executed notebook file
            remove(self._executed_nb_path)

        return output_file_path
示例#4
0
    def convert(self, remove_executed=False):
        """
        Convert the executed notebook to a restructured text (RST) file.

        Parameters
        ----------
        delete_executed : bool, optional
            Controls whether to remove the executed notebook or not.
        """

        if not path.exists(self._executed_nb_path):
            raise IOError("Executed notebook file doesn't exist! Expected: {0}"
                          .format(self._executed_nb_path))

        if path.exists(self._rst_path) and not self.overwrite:
            logger.debug("RST version of notebook already exists at {0}. Use "
                         "overwrite=True or --overwrite (at cmd line) to re-run"
                         .format(self._rst_path))
            return self._rst_path

        # Initialize the resources dict - see:
        # https://github.com/jupyter/nbconvert/blob/master/nbconvert/nbconvertapp.py#L327
        resources = {}
        resources['config_dir'] = '' # we don't need to specify config
        resources['unique_key'] = self.nb_name

        # path to store extra files, like plots generated
        resources['output_files_dir'] = 'nboutput'

        # these keywords are used to build the filter keywords
        # TODO: add a pre-processor that extracts the keywords from the markdown
        # cell in the header and adds them to this list
        # NOTE: the split[-4] trick below is brittle in that it will break if
        # a notebook is, say, nested two layers deep instead of just one like
        # all of our notebooks thus far.
        resources['nb_keywords'] = [self.nb_path.split(sep)[-4]]

        # Exports the notebook to RST
        logger.debug('Exporting notebook to RST...')
        exporter = RSTExporter()

        if self.template_file:
            exporter.template_file = self.template_file
        output, resources = exporter.from_filename(self._executed_nb_path,
                                                   resources=resources)

        # Write the output RST file
        writer = FilesWriter()
        output_file_path = writer.write(output, resources,
                                        notebook_name=self.nb_name)

        # read the executed notebook, grab the keywords from the header,
        # add them in to the RST as filter keywords
        with open(self._executed_nb_path) as f:
            nb = nbformat.read(f, as_version=IPYTHON_VERSION)

        top_cell_text = nb['cells'][0]['source']
        match = re.search('## [kK]eywords\s+(.*)', top_cell_text)

        if match:
            keywords = match.groups()[0].split(',')
            keywords = [clean_keyword(k) for k in keywords if k.strip()]
            keyword_filters = ['filter{0}'.format(k) for k in keywords]
        else:
            keyword_filters = []

        # Add metatags to top of RST files to get rendered into HTML, used for
        # the search and filter functionality in Learn Astropy
        meta_tutorials = '.. meta::\n    :keywords: {0}\n'
        filters = ['filterTutorials'] + keyword_filters
        meta_tutorials = meta_tutorials.format(', '.join(filters))
        with open(output_file_path, 'r') as f:
            rst_text = f.read()

        with open(output_file_path, 'w') as f:
            rst_text = '{0}\n{1}'.format(meta_tutorials, rst_text)
            f.write(rst_text)

        if remove_executed: # optionally, clean up the executed notebook file
            remove(self._executed_nb_path)

        return output_file_path
示例#5
0
    def convert(self, remove_executed=False):
        """
        Convert the executed notebook to a restructured text (RST) file or HTML.

        Parameters
        ----------
        delete_executed : bool, optional
            Controls whether to remove the executed notebook or not.

        """

        if not path.exists(self._executed_nb_path):
            raise IOError(
                "Executed notebook file doesn't exist! Expected: {0}".format(
                    self._executed_nb_path))

        if path.exists(self._output_path) and not self.overwrite:
            logger.debug(
                "{0} version of notebook already exists at {1}. Use "
                "overwrite=True or --overwrite (at cmd line) to re-run".format(
                    self._output_type, self._output_path))
            return self._output_path

        # Initialize the resources dict - see:
        # https://github.com/jupyter/nbconvert/blob/master/nbconvert/nbconvertapp.py#L327
        resources = {}
        resources['config_dir'] = ''  # we don't need to specify config
        resources['unique_key'] = self.nb_name

        # path to store extra files, like plots generated
        resources['output_files_dir'] = 'nboutput'

        if self.base_path is None:
            path_to_root = ''
        else:
            path_to_root = path.relpath(self.base_path,
                                        start=path.split(self.nb_path)[0])
            path_to_root += path.sep
        resources['path_to_pages_root'] = request.pathname2url(path_to_root)

        # Exports the notebook to the output format
        logger.debug('Exporting notebook to {}...'.format(self._output_type))
        if self._output_type == 'RST':
            exporter = RSTExporter()
        elif self._output_type == 'HTML':
            exporter = HTMLExporter()
        else:
            raise ValueError('This should be impossible... output_type should '
                             'have been checked earlier, but it is '
                             'unrecognized')

        if self.template_file:
            exporter.template_file = self.template_file
        output, resources = exporter.from_filename(self._executed_nb_path,
                                                   resources=resources)

        # Write the output file
        writer = FilesWriter()
        output_file_path = writer.write(output,
                                        resources,
                                        notebook_name=self.nb_name)

        if self._output_type == 'RST':
            self._add_filter_keywords(output_file_path)

        if remove_executed:  # optionally, clean up the executed notebook file
            remove(self._executed_nb_path)

        title = ''
        try:
            with open(self.nb_path) as f:
                nb = nbformat.reader.read(f)
                title = nb['cells'][0]['source'].split('#')[1].split(
                    "\n")[0].strip()
        except Exception:
            print(
                'Failed to parse notebook title from first cell, please check notebook.'
            )

        page_info = dict(output_file_path=output_file_path,
                         name=self.nb_name.replace("_", ' ').title(),
                         title=title)

        return page_info