4

我已经建立了一个用于可视化目的的笔记本。

首先,我使用以下命令行界面将其导出为 html nbconvertjupyter nbconvert myNotebook.ipynb --no-input添加--no-input标志以隐藏输入单元格

输出由一个带有嵌入图的 html 文件组成。正如预期的那样

其次,为了在我的报告中添加目录,我还安装了该jupyter_contrib_nbextensions软件包。

我使用目录 (2)包。但是,当我将笔记本导出为 HTML 时 jupyter nbconvert myNotebook.ipynb --no-input --to html_toc ,绘图现在保存在单独的文件夹中

我想让它们嵌入到 html 文件中。有人知道如何解决这个问题吗?

这是一个简单的笔记本,可以用来说明问题

#%%

import numpy as np
import matplotlib.pyplot as plt

#%% md

# Linear plot with noise

#%%

x = np.linspace(0, 10, 100)
noise = np.random.randn(len(x))
y1 = 2 * x + noise
plt.plot(x, y1);


#%% md

# Sine plot with noise

#%%

y2 = 5 * np.sin(x) + noise
plt.plot(x, y2);

4

2 回答 2

1

toc2 使用额外的预处理器 ExtractOutputPreprocessor

创建一个nbconvert_config.py包含以下内容的文件:

c = get_config()

c.NbConvertApp.notebooks = ['mynotebook.ipynb']
c.NbConvertApp.export_format = 'html_toc'
c.Exporter.preprocessors = [
    'nbconvert.preprocessors.TagRemovePreprocessor',
    'nbconvert.preprocessors.RegexRemovePreprocessor',
    'nbconvert.preprocessors.coalesce_streams',
    'nbconvert.preprocessors.CSSHTMLHeaderPreprocessor',
    'nbconvert.preprocessors.HighlightMagicsPreprocessor',
    #'nbconvert.preprocessors.ExtractOutputPreprocessor',
]

并打电话jupyter nbconvert --config nbconvert_config.py。键是列表中必须注释的最后一个条目。

于 2020-04-15T16:36:13.487 回答
0

经过几次迭代,我想出了以下代码:

main_name  = 'your_notebook_name' # or use ipynbname to get it automatically
nb_fname   = main_name + '.ipynb' # Input  file
html_fname = main_name + '.html'  # Output file

# Disable the ExtractOutput preprocessor. This prevents images embedded on the notebook (ie: plots) from being externally linked
config = {'ExtractOutputPreprocessor': {'enabled': False}}

# Find the HTML (with TOC) exporter
import nbconvert.exporters.exporter_locator
HTMLTOCExporter = nbconvert.exporters.exporter_locator.get_exporter('html_toc')
exporter = HTMLTOCExporter(config)

# Add a preprocessor to the exporter to remove the notebook cells with the tag 'remove_cell'
from nbconvert.preprocessors import TagRemovePreprocessor
cell_remover = TagRemovePreprocessor(remove_cell_tags={'remove_cell'})
exporter.register_preprocessor(cell_remover, True)

# Generate HTML and write it to a file
html, resources = exporter.from_filename(nb_fname)
with open(html_fname,'w') as f:
    f.write(html)

加分项:在 Markdown 中嵌入 Markdown 图像![text](path_to_file)

您需要定义一个自定义预处理器并在调用之前注册它exporter.from_filename(...)自定义预处理器

import re
import base64
import os
from nbconvert.preprocessors import Preprocessor

class EmbedExternalImagesPreprocessor(Preprocessor):
    def preprocess_cell(self, cell, resources, cell_index):
        if cell.get('cell_type','') == 'markdown':
            # Find Markdown image pattern: ![alt_text](file)
            embedded_images = re.findall('\!\[(.*?)\]\((.*?)\)',cell['source'])
            
            for alt_text, file in embedded_images:
                # Read each image file and encode it in base64
                with open(file,'br') as f:
                    img_data = f.read()
                    b64_image = base64.b64encode(img_data).decode()
                
                # Generate the HTML tag
                _, file_extension = os.path.splitext(file)
                base64html = f'<img src="data:image/{file_extension};base64,{b64_image}" alt="{alt_text}">'
                
                # Replace Markdown pattern with HTML tag
                cell['source'] = cell['source'].replace(f'![{alt_text}]({file})',base64html)
        return cell, resources

注册新的预处理器

main_name  = 'your_notebook_name' # or use ipynbname to get it automatically
nb_fname   = main_name + '.ipynb' # Input  file
html_fname = main_name + '.html'  # Output file

# Disable the ExtractOutput preprocessor. This prevents images embedded on the notebook (ie: plots) from being externally linked
config = {'ExtractOutputPreprocessor': {'enabled': False}}

# Find the HTML (with TOC) exporter
import nbconvert.exporters.exporter_locator
HTMLTOCExporter = nbconvert.exporters.exporter_locator.get_exporter('html_toc')
exporter = HTMLTOCExporter(config)

# Add a preprocessor to the exporter to remove the notebook cells with the tag 'remove_cell'
from nbconvert.preprocessors import TagRemovePreprocessor, ExtractOutputPreprocessor
cell_remover = TagRemovePreprocessor(remove_cell_tags={'remove_cell'})
exporter.register_preprocessor(cell_remover, True)
exporter.register_preprocessor(EmbedExternalImagesPreprocessor(), True)

# Generate HTML and write it to a file
html, resources = exporter.from_filename(nb_fname)
with open(html_fname,'w') as f:
    f.write(html)
于 2021-05-26T22:11:05.017 回答