diff --git a/docs/nbdoc/consts.py b/docs/nbdoc/consts.py index 5a53c137abe..57d9e603b38 100644 --- a/docs/nbdoc/consts.py +++ b/docs/nbdoc/consts.py @@ -10,6 +10,8 @@ blacklisted_extensions = ['.xml', '.bin'] notebooks_repo = "https://github.com/openvinotoolkit/openvino_notebooks/blob/main/" notebooks_binder = "https://mybinder.org/v2/gh/openvinotoolkit/openvino_notebooks/HEAD?filepath=" notebooks_colab = "https://colab.research.google.com/github/openvinotoolkit/openvino_notebooks/blob/main/" +file_with_binder_notebooks = Path('../../docs/notebooks/notebooks_with_binder_buttons.txt').resolve(strict=True) +file_with_colab_notebooks = Path('../../docs/notebooks/notebooks_with_colab_buttons.txt').resolve(strict=True) openvino_notebooks_ipynb_list = Path('../../docs/notebooks/all_notebooks_paths.txt').resolve(strict=True) diff --git a/docs/nbdoc/nbdoc.py b/docs/nbdoc/nbdoc.py index 45bcfcada36..a22baf6319b 100644 --- a/docs/nbdoc/nbdoc.py +++ b/docs/nbdoc/nbdoc.py @@ -17,6 +17,8 @@ from consts import ( repo_directory, repo_name, openvino_notebooks_ipynb_list, + file_with_binder_notebooks, + file_with_colab_notebooks, repo_owner, notebooks_repo, notebooks_binder, @@ -37,6 +39,51 @@ import sys matching_notebooks_paths = [] +def fetch_binder_list(binder_list_file) -> list: + """Function that fetches list of notebooks with binder buttons + + :param file_format: Format of file containing list of notebooks with button. Defaults to 'txt' + :type file_format: str + :return: List of notebooks containing binder buttons + :rtype: list + """ + if binder_list_file: + with open(binder_list_file) as file: + list_of_buttons = file.read().splitlines() + return list_of_buttons + +def fetch_colab_list(colab_list_file) -> list: + """Function that fetches list of notebooks with colab buttons + + :param file_format: Format of file containing list of notebooks with button. Defaults to 'lst' + :type file_format: str + :return: List of notebooks containing colab buttons + :rtype: list + """ + if colab_list_file: + with open(colab_list_file) as file: + list_of_cbuttons = file.read().splitlines() + return list_of_cbuttons + + +def add_glob_directive(): + """This function modifies toctrees of the five node articles in tutorials + section. It adds the notebooks found in docs/notebooks directory to the menu. + """ + tutorials_path = Path('../../docs/articles_en/learn_openvino/tutorials').resolve(strict=True) + tutorials_files = [x for x in os.listdir(tutorials_path) if re.match("notebooks_section_[0-9]{1}\.md$", x)] + for tutorials_file in tutorials_files: + file_name = os.path.join(tutorials_path, tutorials_file) + with open(file_name, 'r+', encoding='cp437') as section_file: + section_number = ''.join(c for c in str(tutorials_file) if c.isdigit()) + read_file = section_file.read() + if ':glob:' not in read_file: + add_glob = read_file\ + .replace(":hidden:\n", ":hidden:\n :glob:\n :reversed:\n\n notebooks/" + section_number +"*\n") + section_file.seek(0) + section_file.write(add_glob) + section_file.truncate() + class NbTravisDownloader: @staticmethod def download_from_jenkins(path: str = notebooks_path, artifact_link: str = artifacts_link): @@ -103,38 +150,7 @@ class NbProcessor: for n in matching_notebooks: matching_notebooks_paths.append(n) - def fetch_binder_list(self, file) -> list: - """Function that fetches list of notebooks with binder buttons - - :param file_format: Format of file containing list of notebooks with button. Defaults to 'txt' - :type file_format: str - :return: List of notebooks containing binder buttons - :rtype: list - """ - list_of_buttons = glob(f"{self.nb_path}/{file}") - if list_of_buttons: - with open(list_of_buttons[0]) as file: - list_of_buttons = file.read().splitlines() - return list_of_buttons - return [] - - def fetch_colab_list(self, file) -> list: - """Function that fetches list of notebooks with colab buttons - - :param file_format: Format of file containing list of notebooks with button. Defaults to 'lst' - :type file_format: str - :return: List of notebooks containing colab buttons - :rtype: list - """ - list_of_cbuttons = glob(f"{self.nb_path}/{file}") - if list_of_cbuttons: - with open(list_of_cbuttons[0]) as file: - list_of_cbuttons = file.read().splitlines() - return list_of_cbuttons - return [] - - - def add_binder(self, buttons_list: list, cbuttons_list: list, template_with_colab_and_binder: str = binder_colab_template, template_with_binder: str = binder_template, template_with_colab: str = colab_template, template_without_binder: str = no_binder_template): + def add_binder(self, buttons_list: list, cbuttons_list: list, template_with_colab_and_binder: str = binder_colab_template, template_with_binder: str = binder_template, template_with_colab: str = colab_template, template_without_binder: str = no_binder_template): """Function working as an example how to add binder button to existing rst files :param buttons_list: List of notebooks that work on Binder. @@ -158,7 +174,7 @@ class NbProcessor: "folder": repo_directory, "link_git": notebooks_repo + nb_path, "link_binder": notebooks_binder + nb_path, - "link_colab ": notebooks_colab + nb_path, + "link_colab": notebooks_colab + nb_path, } if notebook_item in buttons_list: @@ -171,25 +187,9 @@ class NbProcessor: raise FileNotFoundError("Unable to modify file") -def add_glob_directive(): - """This function modifies toctrees of the five node articles in tutorials - section. It adds the notebooks found in docs/notebooks directory to the menu. - """ - tutorials_path = Path('../../docs/articles_en/learn_openvino/tutorials').resolve(strict=True) - tutorials_files = [x for x in os.listdir(tutorials_path) if re.match("notebooks_section_[0-9]{1}\.md$", x)] - for tutorials_file in tutorials_files: - file_name = os.path.join(tutorials_path, tutorials_file) - with open(file_name, 'r+', encoding='cp437') as section_file: - section_number = ''.join(c for c in str(tutorials_file) if c.isdigit()) - read_file = section_file.read() - if ':glob:' not in read_file: - add_glob = read_file\ - .replace(":hidden:\n", ":hidden:\n :glob:\n :reversed:\n\n notebooks/" + section_number +"*\n") - section_file.seek(0) - section_file.write(add_glob) - section_file.truncate() - def main(): + buttons_list = fetch_binder_list(file_with_binder_notebooks) + cbuttons_list = fetch_colab_list(file_with_colab_notebooks) parser = argparse.ArgumentParser() parser.add_argument('sourcedir', type=Path) parser.add_argument('outdir', type=Path) @@ -208,8 +208,6 @@ def main(): shutil.copytree(sourcedir, outdir) # Step 3. Run processing on downloaded file nbp = NbProcessor(outdir) - buttons_list = nbp.fetch_binder_list('notebooks_with_binder_buttons.txt') - cbuttons_list = nbp.fetch_colab_list('notebooks_with_colab_buttons.txt') nbp.add_binder(buttons_list, cbuttons_list)