如何导出和保存链接的Jupyter笔记本? [英] How to export and preserve linked Jupyter notebooks?

查看:248
本文介绍了如何导出和保存链接的Jupyter笔记本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个相互链接的Jupyter笔记本,因此 Notebook1.ipydb 包含指向 Notebook2.ipydb 的链接,降价幅度为[Notebook2](Notebook2.ipynb),反之亦然.

I have multiple Jupyter notebooks that are linked to one another such that Notebook1.ipydb contains a link to Notebook2.ipydb with the markdown [Notebook2](Notebook2.ipynb) and vice versa.

通过nbconvert将所有笔记本导出为HTML时,将保留指向 Notebook2.ipynb 的链接.我想将该链接更改为导出的 Notebook2.html ,以便链接的HTML文件充当静态网站.

When exporting all notebooks to HTML via nbconvert, the link to Notebook2.ipynb is preserved. I would like to change that link to the exported Notebook2.html so the linked HTML files function as a static website.

我试图检测我是否使用get_ipython().__class__.__name__在iPython中运行,但是在 转换为HTML之前,它执行了这段代码.

I tried to detect if I was running in iPython using get_ipython().__class__.__name__, but it executes this code before converting to HTML.

有没有一种方法可以检测静态文件以有条件地呈现正确的降价幅度?还有另一种保存链接笔记本的方法吗?

Is there a way to detect a static file to conditionally render the right markdown? Is there another way to preserve linked notebooks?

推荐答案

实际上只有两个选择.首先是链接到Notebook2.html,另一个是为nbconvert创建自定义预处理器.

There's only really two options. One is to link to Notebook2.html in the first place and the other is to create a custom preprocessor for nbconvert.

from nbconvert.preprocessors import Preprocessor
import re


class CustomPreprocessor(Preprocessor):

    def preprocess_cell(self, cell, resources, index):

        if 'source' in cell and cell.cell_type == "markdown":
            cell.source = re.sub(r"\[(.*)\]\(\1\.ipynb\)",r"[\1](\1.html)",cell.source)

        return cell, resources

将其保存到文件,然后将以下行添加到您的nbconvert配置文件(位于~/.jupyter/jupyter_nbconvert_config.py或可以使用命令jupyter nbconvert --generate-config生成):

Save this to a file, then add to your nbconvert config file (located at ~/.jupyter/jupyter_nbconvert_config.py or can be generated using the command jupyter nbconvert --generate-config) the line:

c.HTMLExporter.preprocessors = ['CustomPreprocessor.CustomPreprocessor']

这假定自定义预处理器文件名为CustomPreprocessor,并且与您尝试转换的文件位于同一目录中.您也可以将其作为模块正确安装.

This assumes that the custom preprocessor file is named CustomPreprocessor and is located in the same directory as the files you're trying to convert. You could also properly install it as a module too.

这篇关于如何导出和保存链接的Jupyter笔记本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆