Converting ipython notebook to html with separate

2019-03-27 14:25发布

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.

I'm calling:

ipython nbconvert --to html mynotebook.ipynb

The output at the command line includes:

[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.

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.

I've tried to use:

ipython nbconvert --config mycfg.py

With

c = get_config()
c.NbConvertApp.notebooks = ["mynotebook.ipynb"]

in the .py file, but that's as fas as I've got.

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!

1条回答
疯言疯语
2楼-- · 2019-03-27 14:58

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.

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']

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 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 refers to a function in extractoutput.py that specifies how the output will be processed.

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: Attempted relative import in non-package

error because it doesn't know where to look for the package. When changed to

fromIPython.nbconvert.preprocessors.base import Preprocessor

then it works and all the image assets are put into the mynotebook_files directory.

I didn't need to edit the HTML output template in this case, it knew where to look anyway.

查看更多
登录 后发表回答