1414# limitations under the License.
1515
1616import argparse
17+ from pathlib import Path
1718
1819import nbformat
20+ from tqdm import tqdm
1921
2022from ..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
5772def 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