1
1
import base64
2
+ import json
2
3
import os
3
4
import re
4
- import shutil
5
5
from datetime import datetime
6
6
from pprint import pprint
7
- from typing import Sequence
7
+ from textwrap import wrap
8
+ from typing import Any , Dict , Sequence
8
9
from warnings import warn
9
10
10
11
import fire
80
81
# {height="60px" width="240px"}
81
82
82
83
"""
84
+ TEMPLATE_CARD_ITEM = """
85
+ .. customcarditem::
86
+ :header: %(title)s
87
+ :card_description: %(short_description)s
88
+ :tags: %(tags)s
89
+ """
83
90
84
91
85
92
def default_requirements (path_req : str = PATH_REQ_DEFAULT ) -> list :
@@ -163,6 +170,7 @@ def augment_script(fpath: str):
163
170
dict (local_ipynb = f"{ os .path .dirname (fpath )} .ipynb" ),
164
171
generated = datetime .now ().isoformat (),
165
172
)
173
+
166
174
meta ['description' ] = meta ['description' ].replace (os .linesep , f"{ os .linesep } # " )
167
175
168
176
header = TEMPLATE_HEADER % meta
@@ -311,6 +319,43 @@ def parse_requirements(dir_path: str):
311
319
with open (fname , "w" ) as fp :
312
320
fp .write (" " .join (cmd_args ))
313
321
322
+ @staticmethod
323
+ def _get_card_item_cell (path_ipynb : str ) -> Dict [str , Any ]:
324
+ """Build the card item cell for the given notebook path."""
325
+ fpath_meta = path_ipynb .replace (".ipynb" , ".yaml" )
326
+ meta = yaml .safe_load (open (fpath_meta ))
327
+
328
+ # Clamp description length
329
+ wrapped_description = wrap (
330
+ meta .get ('short_description' , meta ['description' ]).strip ().replace (os .linesep , " " ), 175
331
+ )
332
+ suffix = "..." if len (wrapped_description ) > 1 else ""
333
+ meta ['short_description' ] = wrapped_description [0 ] + suffix
334
+
335
+ # Resolve some default tags based on accelerators and directory name
336
+ meta ['tags' ] = meta .get ('tags' , [])
337
+
338
+ accelerators = meta .get ("accelerator" , ('CPU' , ))
339
+ if ('GPU' in accelerators ) or ('TPU' in accelerators ):
340
+ meta ['tags' ].append ('GPU/TPU' )
341
+
342
+ dirname = os .path .basename (os .path .dirname (path_ipynb ))
343
+ if dirname != ".notebooks" :
344
+ meta ['tags' ].append (dirname )
345
+
346
+ meta ['tags' ] = "," .join (meta ['tags' ])
347
+
348
+ # Build the notebook cell
349
+ rst_cell = TEMPLATE_CARD_ITEM % meta
350
+
351
+ return {
352
+ "cell_type" : "raw" ,
353
+ "metadata" : {
354
+ "raw_mimetype" : "text/restructuredtext"
355
+ },
356
+ "source" : rst_cell .strip ().splitlines (True )
357
+ }
358
+
314
359
@staticmethod
315
360
def copy_notebooks (path_root : str , path_docs_ipynb : str = "docs/source/notebooks" ):
316
361
"""Copy all notebooks from a folder to doc folder.
@@ -331,7 +376,15 @@ def copy_notebooks(path_root: str, path_docs_ipynb: str = "docs/source/notebooks
331
376
new_ipynb = os .path .join (path_docs_ipynb , sub_ipynb )
332
377
os .makedirs (os .path .dirname (new_ipynb ), exist_ok = True )
333
378
print (f'{ path_ipynb } -> { new_ipynb } ' )
334
- shutil .copy (path_ipynb , new_ipynb )
379
+
380
+ with open (path_ipynb ) as f :
381
+ ipynb = json .load (f )
382
+
383
+ ipynb ["cells" ].append (HelperCLI ._get_card_item_cell (path_ipynb ))
384
+
385
+ with open (new_ipynb , 'w' ) as f :
386
+ json .dump (ipynb , f )
387
+
335
388
ipynb_content .append (os .path .join ('notebooks' , sub_ipynb ))
336
389
337
390
@staticmethod
0 commit comments