Skip to content

Commit 42931a9

Browse files
committed
Fix syntax highlighting via nbsphinx
1 parent cddcccc commit 42931a9

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

docs/conf.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import glob
55
from os.path import join
6+
import json
67

78
import vitessce
89

@@ -123,9 +124,39 @@ def find_source():
123124
# -- Strip notebook output -------------------------------------------------
124125

125126

126-
for filename in glob.glob(join('notebooks', '*.ipynb'), recursive=True):
127+
for filename in glob.glob(join('notebooks', "__ipynb__", '*.ipynb'), recursive=True):
127128
ntbk = nbclean.NotebookCleaner(filename)
128129
ntbk.clear('stderr')
129130
ntbk.clear('output')
130131
ntbk.remove_cells(empty=True)
132+
ntbk.remove_cells(search_text="import marimo as mo")
131133
ntbk.save(filename)
134+
135+
# Add missing metadata, to enable the code to be interpreted as Python code
136+
# for syntax highlighting when rendered by nbsphinx.
137+
with open(filename, 'r') as f:
138+
ntbk_json = json.load(f)
139+
with open(filename, 'w') as f:
140+
if len(ntbk_json['metadata']) == 0:
141+
# If the metadata is empty, we add the default metadata.
142+
# This is needed for nbsphinx to render the notebook correctly.
143+
ntbk_json['metadata'] = {
144+
"kernelspec": {
145+
"display_name": "Python 3 (ipykernel)",
146+
"language": "python",
147+
"name": "python3"
148+
},
149+
"language_info": {
150+
"codemirror_mode": {
151+
"name": "ipython",
152+
"version": 3
153+
},
154+
"file_extension": ".py",
155+
"mimetype": "text/x-python",
156+
"name": "python",
157+
"nbconvert_exporter": "python",
158+
"pygments_lexer": "ipython3",
159+
"version": "3.10.14"
160+
}
161+
}
162+
json.dump(ntbk_json, f, indent=2)

0 commit comments

Comments
 (0)