[docs]defroot(subfolder:str)->str:"Returns the local folder for all notebooks."this=os.path.dirname(__file__)ifsubfolder=="c_data":returnos.path.abspath(os.path.normpath(os.path.join(this,"..","..","_doc")))returnos.path.abspath(os.path.normpath(os.path.join(this,"..","..","_doc","practice")))
[docs]deflist_notebooks(subfolder:str,name:Optional[str]=None,contains:Optional[str]=None)->List[str]:""" Retourne les notebooks qui contiennent *name* dans leur nom. :param subfolder: sous-répertoire où chercher :param name: préfixe à chercher :param contains: extrait à chercher :return: liste des notebooks (sans répertoire) """nbs=[os.path.join(root(subfolder),subfolder)]nb_=list(filter(os.path.exists,nbs))assertlen(nb_)>0,"Unable to find notebooks in\n{0}".format("\n".join(nbs))nb=nb_[0]name_=nameifnameisnotNone:names=[_for_inos.listdir(nb)if_.startswith(name_)]ifcontainsisnotNone:names=[_for_inos.listdir(nb)ifcontainsin_]assertlen(names)>0,(f"Unable to find any notebook in {nb!r} "f"(this file is {__file__}, "f"root is {root(subfolder)}, "f"sub is {subfolder!r}, name is {name!r}).")returnnames
[docs]deflist_notebooks_rst_links(subfolder:str,name:Optional[str]=None,contains:Optional[str]=None)->List[str]:""" Retourne une liste de notebooks au format :epkg:`rst`. :param subfolder: sous-répertoire où chercher :param name: préfixe à chercher :param contains: extrait à chercher :return: liste des liens """def_name(s):returnos.path.splitext(os.path.split(s)[-1])[0]def_title(sub,s):reg=re.compile("# (.+)")fn=os.path.join(root(sub),sub,s)assertos.path.exists(fn),(f"Unable to find filename {fn!r} (this file is {__file__}, "f"root is {root(sub)!r}, sub is {sub!r}, name is {name!r}).")withopen(fn,"r",encoding="utf-8")asf:content=f.read()f=reg.findall(content)assertf,f"File {fn!r} does not have any title."title=f[0].strip("\\n\n")returntitlenames=list_notebooks(subfolder,name,contains)prefix=""ifsubfolder=="c_data"else"practice-"return[f":ref:`{_title(subfolder,name)} <nbl-{prefix}{subfolder}-{_name(name)}>`"fornameinnames]