jupyter导出存什么文件,如何导出和保存链接的Jupyter笔记本?

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.

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.

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?

解决方案

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

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值