Esempio n. 1
0
def notebooks_to_rst(app):
    from glob import glob

    # post "big-split", nbconvert is a separate namespace
    from nbconvert.nbconvertapp import NbConvertApp
    from nbconvert.writers import FilesWriter

    class OrphanizerWriter(FilesWriter):
        def write(self, output, resources, **kwargs):
            output = ':orphan:\n\n' + output
            FilesWriter.write(self, output, resources, **kwargs)

    olddir = os.path.abspath(os.curdir)
    try:
        srcdir = os.path.abspath(os.path.split(__file__)[0])
        os.chdir(os.path.join(srcdir, 'stginga', 'notebooks'))
        nbs = glob('*.ipynb')

        app = NbConvertApp()
        app.initialize(argv=[])
        app.writer = OrphanizerWriter()

        app.export_format = 'rst'
        app.notebooks = nbs

        app.start()
    except:
        pass
    finally:
        os.chdir(olddir)
Esempio n. 2
0
def notebooks_to_rst(app):
    from glob import glob

    try:
        # post "big-split", nbconvert is a separate namespace
        from nbconvert.nbconvertapp import NbConvertApp
        from nbconvert.writers import FilesWriter
    except ImportError:
        from IPython.nbconvert.nbconvertapp import NbConvertApp
        from IPython.nbconvert.writers import FilesWriter

    class OrphanizerWriter(FilesWriter):
        def write(self, output, resources, **kwargs):
            output = ':orphan:\n\n' + output
            FilesWriter.write(self, output, resources, **kwargs)

    olddir = os.path.abspath(os.curdir)
    try:
        srcdir = os.path.abspath(os.path.split(__file__)[0])
        os.chdir(os.path.join(srcdir, 'astroimtools', 'notebooks'))
        nbs = glob('*.ipynb')

        app = NbConvertApp()
        app.initialize(argv=[])
        app.writer = OrphanizerWriter()

        app.export_format = 'rst'
        app.notebooks = nbs

        app.start()
    except:
        pass
    finally:
        os.chdir(olddir)
Esempio n. 3
0
        try:
            nb.metadata.notebook
        except AttributeError:
            nb.metadata.notebook = notebook_name

        md_writer.files = files

        return nb, resources


app = NbConvertApp(output_base=output_name)
md_exporter = MarkdownExporter(template_file='./index.md.j2',
                               preprocessors=[CustomPreprocess])
md_writer = FilesWriter(build_directory=output_dir)
app.exporter = md_exporter
app.writer = md_writer
app.convert_single_notebook(notebook)

if len(md_writer.files) > 0:
    rootLogger.info("Collating files...")
    for file in md_writer.files:
        src = normpath(join(output_dir, file))
        dst = join(output_dir, output_name + '_files', basename(file))
        rename(src, dst)
        rootLogger.info("Moving '{}'".format(src))
        rootLogger.info("to '{}'".format(dst))
    rootLogger.info("...done.")

src = join(output_dir, output_name + '_files')
dst = join('../docs/assets/images/posts/', output_name + '_files')
Esempio n. 4
0
def notebooks_to_rst(app):
    from glob import glob

    try:
        # post "big-split", nbconvert is a separate namespace
        from nbconvert.nbconvertapp import NbConvertApp
        from nbconvert.writers import FilesWriter
        from nbconvert.preprocessors import Preprocessor, ExecutePreprocessor, execute
        from nbconvert.exporters import RSTExporter
        from nbformat import NotebookNode
    except ImportError:
        try:
            from IPython.nbconvert.nbconvertapp import NbConvertApp
            from IPython.nbconvert.writers import FilesWriter
            from IPython.nbconvert.preprocessors import Preprocessor, ExecutePreprocessor, execute
            from IPython.nbconvert.exporters import RSTExporter
            from IPython.nbformat import NotebookNode
        except ImportError:
            raise ImportError(
                "Failed to find Jupyter or IPython. Cannot build "
                "the notebooks embedded in the docs. Proceeding "
                "the rest of the doc build, but additional "
                "warnings are likely."
            )
            return

    class OrphanizerWriter(FilesWriter):
        def write(self, output, resources, **kwargs):
            output = ":orphan:\n\n" + output
            FilesWriter.write(self, output, resources, **kwargs)

    class AddSysPath(Preprocessor):
        """
        Adds the local system path to the top of the notebook.  This makes sure
        when build_sphinx is invoked that the notebook actually runs with the
        current build.
        """

        def preprocess(self, nb, resources):
            syspathstr = "sys.path = {} + sys.path".format(str(sys.path))
            cell = {
                "cell_type": "code",
                "execution_count": None,
                "metadata": {},
                "outputs": [],
                "source": "import sys\n" + syspathstr,
            }
            nb.cells.insert(0, NotebookNode(cell))
            return nb, resources

    class RemoveSysPath(Preprocessor):
        """
        Removes the sys.path cell added by AddSysPath
        """

        def preprocess(self, nb, resources):
            if "sys.path" in nb.cells[0].source:
                del nb.cells[0]
            return nb, resources

    class MonkeypatchCellExecutionError(execute.CellExecutionError):
        def __str__(self):
            sstr = super(MonkeypatchCellExecutionError, self).__str__()
            return sstr + " Traceback:\n" + str(self.traceback)

    execute.CellExecutionError = MonkeypatchCellExecutionError

    olddir = os.path.abspath(os.curdir)
    try:
        srcdir = os.path.abspath(os.path.split(__file__)[0])
        if os.path.isdir("notebooks"):
            os.chdir(os.path.join(srcdir, "notebooks"))
            nbs = glob("*.ipynb")

            app.info("Executing and converting these notebooks to sphinx files: " + str(nbs))

            nbc_app = NbConvertApp()
            nbc_app.initialize(argv=[])

            nbc_app.writer = OrphanizerWriter()

            nbc_app.export_format = "rst"

            pps = RSTExporter().default_preprocessors
            pps.insert(0, AddSysPath)
            pps.append(RemoveSysPath)
            nbc_app.config.RSTExporter.preprocessors = pps

            nbc_app.notebooks = nbs

            nbc_app.start()
        else:
            app.info("No notebook directory found in docs so not converting any notebooks.")
    except:
        e = sys.exc_info()[0]
        app.warn("Failed to convert notebooks to RST (see above): " + str(e))
    finally:
        os.chdir(olddir)
Esempio n. 5
0
def write_nb_to_html(filename):
    nbc = NbConvertApp()
    nbc.exporter = HTMLExporter()
    nbc.writer = FilesWriter()
    nbc.convert_single_notebook(filename)
Esempio n. 6
0
def notebooks_to_rst(app):
    from glob import glob

    try:
        # post "big-split", nbconvert is a separate namespace
        from nbconvert.nbconvertapp import NbConvertApp
        from nbconvert.writers import FilesWriter
        from nbconvert.preprocessors import Preprocessor, ExecutePreprocessor, execute
        from nbconvert.exporters import RSTExporter
        from nbformat import NotebookNode
    except ImportError:
        try:
            from IPython.nbconvert.nbconvertapp import NbConvertApp
            from IPython.nbconvert.writers import FilesWriter
            from IPython.nbconvert.preprocessors import Preprocessor, ExecutePreprocessor, execute
            from IPython.nbconvert.exporters import RSTExporter
            from IPython.nbformat import NotebookNode
        except ImportError:
            raise ImportError(
                'Failed to find Jupyter or IPython. Cannot build '
                'the notebooks embedded in the docs. Proceeding '
                'the rest of the doc build, but additional '
                'warnings are likely.')
            return

    class OrphanizerWriter(FilesWriter):
        def write(self, output, resources, **kwargs):
            output = ':orphan:\n\n' + output
            FilesWriter.write(self, output, resources, **kwargs)

    class AddSysPath(Preprocessor):
        """
        Adds the local system path to the top of the notebook.  This makes sure
        when build_sphinx is invoked that the notebook actually runs with the
        current build.
        """
        def preprocess(self, nb, resources):
            syspathstr = 'sys.path = {} + sys.path'.format(str(sys.path))
            cell = {
                'cell_type': 'code',
                'execution_count': None,
                'metadata': {},
                'outputs': [],
                'source': 'import sys\n' + syspathstr
            }
            nb.cells.insert(0, NotebookNode(cell))
            return nb, resources

    class RemoveSysPath(Preprocessor):
        """
        Removes the sys.path cell added by AddSysPath
        """
        def preprocess(self, nb, resources):
            if 'sys.path' in nb.cells[0].source:
                del nb.cells[0]
            return nb, resources

    class MonkeypatchCellExecutionError(execute.CellExecutionError):
        def __str__(self):
            sstr = super(MonkeypatchCellExecutionError, self).__str__()
            return sstr + ' Traceback:\n' + str(self.traceback)

    execute.CellExecutionError = MonkeypatchCellExecutionError

    olddir = os.path.abspath(os.curdir)
    try:
        srcdir = os.path.abspath(os.path.split(__file__)[0])
        if os.path.isdir('notebooks'):
            os.chdir(os.path.join(srcdir, 'notebooks'))
            nbs = glob('*.ipynb')

            app.info(
                "Executing and converting these notebooks to sphinx files: " +
                str(nbs))

            nbc_app = NbConvertApp()
            nbc_app.initialize(argv=[])

            nbc_app.writer = OrphanizerWriter()

            nbc_app.export_format = 'rst'

            pps = RSTExporter().default_preprocessors
            pps.insert(0, AddSysPath)
            pps.append(RemoveSysPath)
            nbc_app.config.RSTExporter.preprocessors = pps

            nbc_app.notebooks = nbs

            nbc_app.start()
        else:
            app.info(
                'No notebook directory found in docs so not converting any notebooks.'
            )
    except:
        e = sys.exc_info()[0]
        app.warn('Failed to convert notebooks to RST (see above): ' + str(e))
    finally:
        os.chdir(olddir)
Esempio n. 7
0
def convert_notebook(src_path):
    app = NbConvertApp()
    app.output_files_dir = './stage/img'
    app.writer = FilesWriter()
    app.exporter = MarkdownExporter()
    app.convert_single_notebook(src_path)
Esempio n. 8
0
def convert_notebook(src_path, output_img_dir):
    app = NbConvertApp()
    app.output_files_dir = output_img_dir
    app.writer = FilesWriter()
    app.exporter = MarkdownExporter()
    app.convert_single_notebook(src_path)