Skip to content

Commit 568fc4d

Browse files
authored
Better support for notebooks-to-md (#469)
* Better support for notebooks-to-md * continue using previous functionality rather than nbconvert * use `<pre>` for print statement outputs * search for all subdirs as well
1 parent c4ed9fd commit 568fc4d

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

.github/workflows/build_main_documentation.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ on:
4343
type: string
4444
default: "src/"
4545
description: "Suffix to add after the version tag (e.g. 1.3.0 or main) in the documentation links."
46+
convert_notebooks:
47+
type: boolean
48+
description: "Convert notebooks to markdown files before building docs."
4649
secrets:
4750
hf_token:
4851
required: true
@@ -148,6 +151,13 @@ jobs:
148151
then
149152
bash -c "${{ inputs.pre_command }}"
150153
fi
154+
155+
- name: Convert notebooks to markdown files
156+
if: inputs.convert_notebooks
157+
run: |
158+
cd doc-builder
159+
doc-builder notebook-to-mdx ${{ env.doc_folder }}"
160+
cd ..
151161
152162
- name: Make documentation
153163
shell: bash

.github/workflows/build_pr_documentation.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ on:
4343
type: string
4444
default: "src/"
4545
description: "Suffix to add after the version tag (e.g. 1.3.0 or main) in the documentation links."
46+
convert_notebooks:
47+
type: boolean
48+
description: "Convert notebooks to markdown files before building docs."
4649
# Debug purposes only!
4750
doc_builder_revision:
4851
type: string
@@ -135,6 +138,13 @@ jobs:
135138
bash -c "${{ inputs.pre_command }}"
136139
fi
137140
141+
- name: Convert notebooks to markdown files
142+
if: inputs.convert_notebooks
143+
run: |
144+
cd doc-builder
145+
doc-builder notebook-to-mdx ${{ env.doc_folder }}"
146+
cd ..
147+
138148
- name: Make documentation
139149
env:
140150
NODE_OPTIONS: --max-old-space-size=6656

src/doc_builder/commands/notebook_to_mdx.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
# limitations under the License.
1515

1616
import argparse
17+
from pathlib import Path
1718

1819
import nbformat
20+
from tqdm import tqdm
1921

2022
from ..style_doc import format_code_example
2123

2224

23-
def notebook_to_mdx_command(args):
24-
notebook = nbformat.read(args.notebook_file, as_version=4)
25+
def notebook_to_mdx(notebook, max_len):
2526
content = []
2627
for cell in notebook["cells"]:
2728
if cell["cell_type"] == "code":
@@ -31,43 +32,62 @@ def notebook_to_mdx_command(args):
3132
]
3233
if len(outputs) > 0:
3334
code_lines = code.split("\n")
34-
# We can add >>> everywhere without worrying as format_code_examples will replace them by ...
35+
# We can add >>> everywhere without worrying as format_code_example will replace them by ...
3536
# when needed.
3637
code_lines = [f">>> {l}" if not len(l) == 0 or l.isspace() else l for l in code_lines]
3738
code = "\n".join(code_lines)
38-
code = format_code_example(code, max_len=args.max_len)[0]
39+
code = format_code_example(code, max_len=max_len)[0]
3940
content.append(f"```python\n{code}\n```")
4041

4142
output = outputs[0]["text"] if "text" in outputs[0] else outputs[0]["text/plain"]
4243
output = output.strip()
43-
content.append(f"```python out\n{output}\n```")
44+
content.append(f"<pre>\n{output}\n</pre>")
4445
else:
45-
code = format_code_example(code, max_len=args.max_len)[0]
46+
code = format_code_example(code, max_len=max_len)[0]
4647
content.append(f"```python\n{code}\n```")
4748
elif cell["cell_type"] == "markdown":
4849
content.append(cell["source"])
4950
else:
5051
content.append(f"```\n{cell['source']}\n```")
5152

52-
dest_file = args.dest_file if args.dest_file is not None else args.notebook_file.replace(".ipynb", ".mdx")
53-
with open(dest_file, "w", encoding="utf-8") as f:
54-
f.write("\n\n".join(content))
53+
mdx_content = "\n\n".join(content)
54+
return mdx_content
55+
56+
57+
def notebook_to_mdx_command(args):
58+
src_path = Path(args.notebook_src).resolve()
59+
src_dir = src_path.parent if src_path.is_file() else src_path
60+
notebook_paths = [src_path] if src_path.is_file() else [*src_dir.glob("**/*.ipynb")]
61+
output_dir = src_dir if args.output_dir is None else Path(args.output_dir).resolve()
62+
63+
for notebook_path in tqdm(notebook_paths, desc="Converting .ipynb files to .md files"):
64+
notebook = nbformat.read(notebook_path, as_version=4)
65+
mdx_content = notebook_to_mdx(notebook, args.max_len)
66+
mdx_file_name = notebook_path.name[: -len(".ipynb")] + ".md"
67+
dest_file_path = output_dir / mdx_file_name
68+
with open(dest_file_path, "w", encoding="utf-8") as f:
69+
f.write(mdx_content)
5570

5671

5772
def notebook_to_mdx_command_parser(subparsers=None):
5873
if subparsers is not None:
5974
parser = subparsers.add_parser("notebook-to-mdx")
6075
else:
61-
parser = argparse.ArgumentParser("Doc Builder convert notebook to MDX command")
76+
parser = argparse.ArgumentParser("Doc Builder convert notebook to MD command")
6277

63-
parser.add_argument("notebook_file", type=str, help="The notebook to convert.")
78+
parser.add_argument("notebook_src", type=str, help="The notebook or directory containing notebook(s) to convert.")
79+
parser.add_argument(
80+
"--output_dir",
81+
type=str,
82+
help="Where the markdown files will be. Defaults to notebook source dir.",
83+
default=None,
84+
)
6485
parser.add_argument(
6586
"--max_len",
6687
type=int,
6788
default=119,
6889
help="The number of maximum characters per line.",
6990
)
70-
parser.add_argument("--dest_file", type=str, default=None, help="Where to save the result.")
7191

7292
if subparsers is not None:
7393
parser.set_defaults(func=notebook_to_mdx_command)

0 commit comments

Comments
 (0)