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.