11import base64
2+ import json
23import os
34import re
4- import shutil
55from datetime import datetime
66from pprint import pprint
7- from typing import Sequence
7+ from textwrap import wrap
8+ from typing import Any , Dict , Sequence
89from warnings import warn
910
1011import fire
8081# {height="60px" width="240px"}
8182
8283"""
84+ TEMPLATE_CARD_ITEM = """
85+ .. customcarditem::
86+ :header: %(title)s
87+ :card_description: %(short_description)s
88+ :tags: %(tags)s
89+ """
8390
8491
8592def default_requirements (path_req : str = PATH_REQ_DEFAULT ) -> list :
@@ -163,6 +170,7 @@ def augment_script(fpath: str):
163170 dict (local_ipynb = f"{ os .path .dirname (fpath )} .ipynb" ),
164171 generated = datetime .now ().isoformat (),
165172 )
173+
166174 meta ['description' ] = meta ['description' ].replace (os .linesep , f"{ os .linesep } # " )
167175
168176 header = TEMPLATE_HEADER % meta
@@ -311,6 +319,43 @@ def parse_requirements(dir_path: str):
311319 with open (fname , "w" ) as fp :
312320 fp .write (" " .join (cmd_args ))
313321
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+
314359 @staticmethod
315360 def copy_notebooks (path_root : str , path_docs_ipynb : str = "docs/source/notebooks" ):
316361 """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
331376 new_ipynb = os .path .join (path_docs_ipynb , sub_ipynb )
332377 os .makedirs (os .path .dirname (new_ipynb ), exist_ok = True )
333378 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+
335388 ipynb_content .append (os .path .join ('notebooks' , sub_ipynb ))
336389
337390 @staticmethod
0 commit comments