sphinx_nbexamples/__init__.py
def create_rst(self, nb, in_dir, odir):
        """Create the rst file from the notebook node"""
        raw_rst, resources = nbconvert.export_by_name('rst', nb)
        # remove ipython magics
        rst_content = ''
        i0 = 0
        m = None
        # HACK: we insert the bokeh style sheets here as well, since for some
        # themes (e.g. the sphinx_rtd_theme) it is not sufficient to include
        # the style sheets only via app.add_stylesheet
        bokeh_str = ''
        if 'bokeh' in raw_rst and self.insert_bokeh:
            bokeh_str += self.BOKEH_TEMPLATE.format(
                version=self.insert_bokeh)
        if 'bokeh' in raw_rst and self.insert_bokeh_widgets:
            bokeh_str += self.BOKEH_WIDGETS_TEMPLATE.format(
                version=self.insert_bokeh_widgets)
        for m in code_blocks.finditer(raw_rst):
            lines = m.group().splitlines(True)
            header, content = lines[0], ''.join(lines[1:])
            no_magics = magic_patt.sub('\g<1>', content)
            # if the code cell only contained magic commands, we skip it
            if no_magics.strip():
                rst_content += (
                    raw_rst[i0:m.start()] + bokeh_str + header + no_magics)
                bokeh_str = ''
                i0 = m.end()
            else:
                rst_content += raw_rst[i0:m.start()]
                i0 = m.end()
        if m is not None:
            rst_content += bokeh_str + raw_rst[m.end():]
        else:
            rst_content = raw_rst
        rst_content = '.. _%s:\n\n' % self.reference + \
            rst_content
        url = self.url
        if url is not None:
            rst_content += self.CODE_DOWNLOAD_NBVIEWER.format(
                pyfile=os.path.basename(self.py_file),
                nbfile=os.path.basename(self.outfile),
                url=url)
        else:
            rst_content += self.CODE_DOWNLOAD.format(
                pyfile=os.path.basename(self.py_file),
                nbfile=os.path.basename(self.outfile))
        supplementary_files = self.supplementary_files
        other_supplementary_files = self.other_supplementary_files
        if supplementary_files or other_supplementary_files:
            for f in (supplementary_files or []) + (
                    other_supplementary_files or []):
                if not os.path.exists(os.path.join(odir, f)):
                    copyfile(os.path.join(in_dir, f), os.path.join(odir, f))
        if supplementary_files:
            rst_content += self.data_download(supplementary_files)

        rst_file = self.get_out_file()
        outputs = sorted(resources['outputs'], key=rst_content.find)
        base = os.path.join('images', os.path.splitext(
            os.path.basename(self.infile))[0] + '_%i.png')
        out_map = {os.path.basename(original): base % i
                   for i, original in enumerate(outputs)}
        for original, final in six.iteritems(out_map):
            rst_content = rst_content.replace(original, final)
        with open(rst_file, 'w') \
                as f:
            f.write(rst_content.rstrip() + '\n')
        pictures = []
        for original in outputs:
            fname = os.path.join(odir, out_map[os.path.basename(original)])
            pictures.append(fname)
            if six.PY3:
                f = open(fname, 'w+b')
            else:
                f = open(fname, 'w')
            f.write(resources['outputs'][original])
            f.close()
        self.pictures = pictures
def create_rst(self, nb, in_dir, odir):
        """Create the rst file from the notebook node"""
        raw_rst, resources = nbconvert.export_by_name('rst', nb)
        # remove ipython magics
        rst_content = ''
        i0 = 0
        m = None
        # HACK: we insert the bokeh style sheets here as well, since for some
        # themes (e.g. the sphinx_rtd_theme) it is not sufficient to include
        # the style sheets only via app.add_stylesheet
        bokeh_str = ''
        if 'bokeh' in raw_rst and self.insert_bokeh:
            bokeh_str += self.BOKEH_TEMPLATE.format(
                version=self.insert_bokeh)
        if 'bokeh' in raw_rst and self.insert_bokeh_widgets:
            bokeh_str += self.BOKEH_WIDGETS_TEMPLATE.format(
                version=self.insert_bokeh_widgets)
        for m in code_blocks.finditer(raw_rst):
            lines = m.group().splitlines(True)
            header, content = lines[0], ''.join(lines[1:])
            no_magics = magic_patt.sub('\g<1>', content)
            # if the code cell only contained magic commands, we skip it
            if no_magics.strip():
                rst_content += (
                    raw_rst[i0:m.start()] + bokeh_str + header + no_magics)
                bokeh_str = ''
                i0 = m.end()
            else:
                rst_content += raw_rst[i0:m.start()]
                i0 = m.end()
        if m is not None:
            rst_content += bokeh_str + raw_rst[m.end():]
        else:
            rst_content = raw_rst
        rst_content = '.. _%s:\n\n' % self.reference + \
            rst_content
        url = self.url
        if url is not None:
            rst_content += self.CODE_DOWNLOAD_NBVIEWER.format(
                pyfile=os.path.basename(self.py_file),
                nbfile=os.path.basename(self.outfile),
                url=url)
        else:
            rst_content += self.CODE_DOWNLOAD.format(
                pyfile=os.path.basename(self.py_file),
                nbfile=os.path.basename(self.outfile))
        supplementary_files = self.supplementary_files
        other_supplementary_files = self.other_supplementary_files
        if supplementary_files or other_supplementary_files:
            for f in (supplementary_files or []) + (
                    other_supplementary_files or []):
                if not os.path.exists(os.path.join(odir, f)):
                    copyfile(os.path.join(in_dir, f), os.path.join(odir, f))
        if supplementary_files:
            rst_content += self.data_download(supplementary_files)

        rst_file = self.get_out_file()
        outputs = sorted(resources['outputs'], key=rst_content.find)
        base = os.path.join('images', os.path.splitext(
            os.path.basename(self.infile))[0] + '_%i.png')
        out_map = {os.path.basename(original): base % i
                   for i, original in enumerate(outputs)}
        for original, final in six.iteritems(out_map):
            rst_content = rst_content.replace(original, final)
        with open(rst_file, 'w') \
                as f:
            f.write(rst_content.rstrip() + '\n')
        pictures = []
        for original in outputs:
            fname = os.path.join(odir, out_map[os.path.basename(original)])
            pictures.append(fname)
            if six.PY3:
                f = open(fname, 'w+b')
            else:
                f = open(fname, 'w')
            f.write(resources['outputs'][original])
            f.close()
        self.pictures = pictures
Create the rst file from the notebook node