使用单独的图像将ipython笔记本转换为html [英] Converting ipython notebook to html with separate images
问题描述
我有一个混合了SVG和PNG图形的ipython笔记本。我可以毫不费力地将它导出到html,但它将图像作为编码文本嵌入 .html
文件的正文中。
I have an ipython notebook with a mixture of SVG and PNG graphs. I can export it to html without any trouble, but it embeds the images as encoded text in the body of the .html
file.
我正在打电话:
ipython nbconvert --to html mynotebook.ipynb
命令行的输出包括:
[NbConvertApp] Converting notebook mynotebook.ipynb to html
[NbConvertApp] Support files will be in mynotebook_files/
但是没有创建这样的目录,并且没有文件。
but no such directory is created, and there are no files in it.
有相关的帖子(1
, 2
, 3
, 4
)但他们要么不解决这个特定问题,要么参考NBconvert是一个单独的库的旧时代。
There are related posts (1 ,2 ,3 ,4 ) but they either don't fix this specific issue, or refer to the olden days when NBconvert was a separate library.
本文档解释了如何以旧的方式解决这个问题。
This document explains how to solve this problem on the old way of doing things too.
我试过使用:
ipython nbconvert --config mycfg.py
使用
c = get_config()
c.NbConvertApp.notebooks = ["mynotebook.ipynb"]
在 .py
文件中,但是这就像我所拥有的一样。
in the .py
file, but that's as fas as I've got.
我正在寻找的是一种使png文件,最好是svg文件进入文件夹的方法。理想情况下尽可能容易!
What I'm looking for is a way to make the png files, and preferably the svg files, go into a folder. Ideally as easily as possible!
推荐答案
感谢 Thomas K 的推动我已经取得了一些成功。在我有机会了解问题的所有细微差别之前,请将此视为原型答案。可能会有错误,但这是我对正在发生的事情的理解。
Thanks to Thomas K's nudge I've had some success in getting this to work. Consider this a proto-answer until I have a chance to get my head around all the nuances of the problem. There will probably be errors, but this is my understanding of what's happening.
覆盖默认
的默认行为 ipython nbconvert - 到html mynotebook.ipynb
命令你需要指定一个配置文件并像这样调用它
ipython nbconvert --config mycfg.py
。其中 mycfg.py
是与笔记本相同目录中的文件。我的看起来像这样:
To override the default behaviour of the default
ipython nbconvert --to html mynotebook.ipynb
command you need to specify a configuration file and call it like this
ipython nbconvert --config mycfg.py
. Where mycfg.py
is a file in the same directory as your notebooks. Mine looks like this:
c = get_config()
c.NbConvertApp.notebooks = ["mynotebook.ipynb"]
c.NbConvertApp.export_format = 'html'
c.Exporter.preprocessors = ['extractoutput.ExtractOutputPreprocessor']
其中 [mynotebook.ipynb]
是我要转换的文件或文件列表。在这种情况下,控制如何笔记本转换的部分是'extractoutput.ExtractOutputPreprocessor'
。
Where ["mynotebook.ipynb"]
is the file, or list of files, that I want to convert. The part that controls how the notebook gets converted is 'extractoutput.ExtractOutputPreprocessor'
in this case.
extractoutput
.ExtractOutputPreprocessor
指的是 extractoutput.py
,它与笔记本电脑位于同一目录中(虽然我认为不需要)。
extractoutput
.ExtractOutputPreprocessor
refers to extractoutput.py
, which is also in the same directory as the notebooks (although I don't think it needs to be).
extractoutput
.ExtractOutputPreprocessor
指的是 extractoutput.py
指定如何处理输出。
extractoutput
.ExtractOutputPreprocessor
refers to a function in extractoutput.py
that specifies how the output will be processed.
在我的情况下,此文件的内容完全取得来自 IPython repo 的小修改。第22行(<。code>来自.base导入预处理器)生成
In my case the content of this file is taken exactly from the IPython repo with a small modification. Line 22 (from .base import Preprocessor
) produces a
ValueError:尝试相对导入在非包裹中
错误,因为它不知道在哪里寻找包。更改为
error because it doesn't know where to look for the package. When changed to
来自
IPython.nbconvert.preprocessors
.base import预处理器
然后它可以运行并且所有图像资产都被放入 mynotebook_files
目录。
then it works and all the image assets are put into the mynotebook_files
directory.
我不需要编辑 HTML输出模板在这种情况下,它知道无论如何要看。
I didn't need to edit the HTML output template in this case, it knew where to look anyway.
这篇关于使用单独的图像将ipython笔记本转换为html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!