Converting ipython notebook to html with separate images

前端 未结 1 418
执笔经年
执笔经年 2021-02-08 20: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 .

1条回答
  •  星月不相逢
    2021-02-08 20:52

    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.

    0 讨论(0)
提交回复
热议问题