forked from danielfrg/mkdocs-jupyter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbconvert2.py
279 lines (231 loc) · 7.64 KB
/
nbconvert2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""
This modules is a wrapper around nbconvert
It provides a cleaner version of the HTML content that can be embedded into
existing HTML pages without breaking existing styles.
"""
import io
import json
import logging
import os
import jupytext
import mistune
from nbconvert.exporters import HTMLExporter, MarkdownExporter
from nbconvert.filters.highlight import _pygments_highlight
from nbconvert.filters.markdown_mistune import (
IPythonRenderer,
MarkdownWithMath,
)
from nbconvert.nbconvertapp import NbConvertApp
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name
from pygments.util import ClassNotFound
from mkdocs_jupyter.config import settings
from mkdocs_jupyter.preprocessors import SubCell
logger = logging.getLogger("mkdocs.mkdocs-jupyter")
# Language of the kernel (used for syntax highlighting)
kernel_lang = "python"
# We use this to tag each div with code with an unique ID for the copy-to-clipboard
cell_id = 0
def nb2html(
nb_path,
execute=False,
kernel_name="",
theme=None,
start=0,
end=None,
allow_errors=True,
show_input: bool = True,
no_input: bool = False,
):
"""
Convert a notebook to HTML
Arguments
---------
nb_path: str
Path to the notebook
execute: bool
Whether to execute the notebook
kernel_name: str
Name of the kernel to use
theme: str
Name of the theme to use (default: light) (options: light or dark)
start: int
Start cell number
end: int
End cell number
allow_errors
Render even if notebook execution fails
show_input: bool
Shows code input (default: True)
no_input: bool
Render notebook without code or output (default: False)
Returns
-------
HTML content
"""
logger.info(f"Converting notebook (execute={execute}): {nb_path}")
global cell_id, kernel_lang
cell_id = 0 # Reset the cell id
app = get_nbconvert_app(
execute=execute,
kernel_name=kernel_name,
start=start,
end=end,
allow_errors=allow_errors,
show_input=show_input,
no_input=no_input,
)
# Use the templates included in this package
template_file = "mkdocs_html/notebook.html.j2"
extra_template_paths = [settings.templates_dir]
# Customize NBConvert App
preprocessors_ = [SubCell]
filters = {
"highlight_code": custom_highlight_code,
# "markdown2html": custom_markdown2html,
}
exporter = HTMLExporter(
config=app.config,
template_file=template_file,
extra_template_paths=extra_template_paths,
preprocessors=preprocessors_,
filters=filters,
theme=theme,
)
_, extension = os.path.splitext(nb_path)
if extension == ".py":
nb = jupytext.read(nb_path)
nb_file = io.StringIO(jupytext.writes(nb, fmt="ipynb"))
content, resources = exporter.from_file(nb_file)
else:
try:
with open(nb_path, "r", encoding="utf-8") as f:
nb_json = json.load(f)
kernel_lang = nb_json["metadata"]["kernelspec"]["language"]
except KeyError:
pass
content, resources = exporter.from_filename(nb_path)
return content
def nb2md(nb_path, start=0, end=None, execute=False, kernel_name=""):
"""Convert a notebook to markdown
We use a template that removed all code cells because if the body
is to big ( with javascript and CSS) it takes to long to read and parse
"""
app = get_nbconvert_app(
start=start, end=end, execute=execute, kernel_name=kernel_name
)
# Use the templates included in this package
template_file = "mkdocs_md/md-no-codecell.md.j2"
extra_template_paths = [settings.templates_dir]
exporter = MarkdownExporter(
config=app.config,
template_file=template_file,
extra_template_paths=extra_template_paths,
)
_, extension = os.path.splitext(nb_path)
if extension == ".py":
nb = jupytext.read(nb_path)
nb_file = io.StringIO(jupytext.writes(nb, fmt="ipynb"))
body, resources = exporter.from_file(nb_file)
else:
body, resources = exporter.from_filename(nb_path)
return body
def get_nbconvert_app(
execute=False,
kernel_name="",
start=0,
end=None,
allow_errors=True,
show_input: bool = True,
no_input: bool = False,
) -> NbConvertApp:
"""Create"""
# Load the user's nbconvert configuration
app = NbConvertApp()
app.load_config_file()
template_exported_conf = {
"TemplateExporter": {
"exclude_input": not show_input,
}
}
if no_input:
template_exported_conf.update(
{
"TemplateExporter": {
"exclude_output_prompt": True,
"exclude_input": True,
"exclude_input_prompt": True,
}
}
)
app.config.update(
{
# This Preprocessor changes the pygments css prefixes
# from .highlight to .highlight-ipynb
"CSSHTMLHeaderPreprocessor": {
"enabled": True,
"highlight_class": ".highlight-ipynb",
},
"SubCell": {"enabled": True, "start": start, "end": end},
"ExecutePreprocessor": {
"enabled": execute,
"store_widget_state": True,
"kernel_name": kernel_name,
"allow_errors": allow_errors,
},
**template_exported_conf,
}
)
return app
def custom_highlight_code(source, language=None, metadata=None):
"""
Change CSS class names from .highlight to .highlight-ipynb.
This are for the <div> that contains the <pre>
This modifies only the HTML not CSS.
On the `notebook.html.js` template we modify the CSS styles.
"""
global cell_id
cell_id = cell_id + 1
if not language:
language = kernel_lang
formatter = HtmlFormatter(cssclass="highlight-ipynb hl-" + language)
output = _pygments_highlight(source, formatter, language, metadata)
clipboard_copy_txt = f"""<div id="cell-{cell_id}" class="clipboard-copy-txt">{source}</div>
"""
return output + clipboard_copy_txt
def custom_markdown2html(source):
"""
This filter uses a Custom Rendered that uses a custom CodeHtmlFormatter
to wrap the language blocks into a <div> and a <pre> tags.
This is done so it's the same HTML structure that for regular non-language
sections.
"""
return MarkdownWithMath(renderer=CustomMarkdownRendered(escape=False)).render(
source
)
class CustomMarkdownRendered(IPythonRenderer):
def block_code(self, code, lang):
lexer = None
if lang:
try:
lexer = get_lexer_by_name(lang, stripall=True)
except ClassNotFound:
code = lang + "\n" + code
lang = None
if not lang:
return "\n<pre><code>%s</code></pre>\n" % mistune.escape(code)
formatter = CodeHtmlFormatter()
return highlight(code, lexer, formatter)
class CodeHtmlFormatter(HtmlFormatter):
def wrap(self, source, outfile):
return self._wrap_code(source)
def _wrap_code(self, source):
yield 0, '<div class="codehilite"><pre><code>'
for i, t in source:
yield i, t
yield 0, "</code></pre></div>"
if __name__ == "__main__":
content = nb2html("tests/mkdocs/docs/demo.ipynb")
with open("./demo.html", "w") as f:
f.write(content)