Пример #1
0
    def from_notebook_node(self, nb, resources=None, **kw):
        # track conversion time.
        start_time = time()

        # Preprocess the notebook
        nb, resources = self._preprocess(nb, resources)

        # nbconvert options embedded in the metadata as a dict or a list.
        convert = nb.metadata.pop('nbconvert', {})
        if isinstance(convert, dict):
            convert = convert.items()

        for exporter, config in convert:
            app = NbConvertApp(config=Config(config))
            app.initialize([])
            app.exporter = get_exporter(exporter)(config=app.config)
            app.convert_single_notebook(self.filename)

        # How long did the conversions take?
        if convert:
            app.log.info(
                "Conversions completed in {} seconds.".format(time() -
                                                              start_time))

        return
def notebook(request):
    """
    Example on how Jupyter Notebook can be converted to HTML and returned
    from a Django view
    """
    exporter = HTMLExporter(exclude_input=True, exclude_output_prompt=True)
    writer = StringWriter()
    app = NbConvertApp(writer=writer, exporter=exporter)
    app.convert_single_notebook('./notebooks/sample.ipynb')
    return HttpResponse(writer.content)
Пример #3
0
            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')

try:
Пример #4
0
def write_nb_to_html(filename):
    nbc = NbConvertApp()
    nbc.exporter = HTMLExporter()
    nbc.writer = FilesWriter()
    nbc.convert_single_notebook(filename)
Пример #5
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)
Пример #6
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)