コード例 #1
0
ファイル: autogen.py プロジェクト: jerbly/icevision
def py_to_nb_md(dest_dir):
    for file_path in os.listdir("py/"):
        dir_path = "py"
        file_name = file_path
        py_path = os.path.join(dir_path, file_path)
        file_name_no_ext = os.path.splitext(file_name)[0]
        ext = os.path.splitext(file_name)[1]

        if ext != ".py":
            continue

        nb_path = os.path.join("ipynb", file_name_no_ext + ".ipynb")
        md_path = os.path.join(dest_dir, "tutorial", file_name_no_ext + ".md")

        tutobooks.py_to_md(py_path, nb_path, md_path, "templates/img")

        github_repo_dir = "airctic/icedata/blob/master/docs/"
        with open(md_path, "r") as md_file:
            button_lines = [
                ":material-link: "
                "[**View in Colab**](https://colab.research.google.com/github/"
                + github_repo_dir + "ipynb/" + file_name_no_ext + ".ipynb" +
                ")      "
                # + '<span class="k-dot">•</span>'
                + ":octicons-octoface: "
                "[**GitHub source**](https://github.com/" + github_repo_dir +
                "py/" + file_name_no_ext + ".py)",
                "\n",
            ]
            md_content = "".join(button_lines) + "\n" + md_file.read()

        with open(md_path, "w") as md_file:
            md_file.write(md_content)
コード例 #2
0
    def add_guide(self, name, working_dir=None):
        """e.g. add_guide('functional_api')"""
        if name.endswith(".py"):
            name = name[:-3]
        ipynb_dir = Path(self.guides_dir) / "ipynb"
        if not os.path.exists(ipynb_dir):
            os.makedirs(ipynb_dir)

        md_dir = Path(self.guides_dir) / "md"
        if not os.path.exists(md_dir):
            os.makedirs(md_dir)

        img_dir = Path(self.guides_dir) / "img"
        if not os.path.exists(img_dir):
            os.makedirs(img_dir)

        py_path = Path(self.guides_dir) / (name + ".py")
        md_path = md_dir / (name + ".md")
        nb_path = ipynb_dir / (name + ".ipynb")

        tutobooks.py_to_nb(py_path, nb_path, fill_outputs=False)
        tutobooks.py_to_md(py_path,
                           nb_path,
                           md_path,
                           img_dir,
                           working_dir=working_dir)
        md_content = open(md_path).read()
        github_repo_dir = str(GUIDES_GH_LOCATION)
        site_img_dir = "img/guides/" + name
        md_content = self.preprocess_tutobook_md_source(
            md_content, name + ".py", github_repo_dir, img_dir, site_img_dir)
        open(md_path, "w").write(md_content)
コード例 #3
0
    def add_example(self, path, working_dir=None):
        """e.g. add_example('vision/cats_and_dogs')"""
        assert path.count(os.path.sep) == 1
        folder, name = path.split(os.path.sep)
        if name.endswith(".py"):
            name = name[:-3]

        ipynb_dir = Path(self.examples_dir) / folder / "ipynb"
        if not os.path.exists(ipynb_dir):
            os.makedirs(ipynb_dir)

        md_dir = Path(self.examples_dir) / folder / "md"
        if not os.path.exists(md_dir):
            os.makedirs(md_dir)

        img_dir = Path(self.examples_dir) / folder / "img"
        if not os.path.exists(img_dir):
            os.makedirs(img_dir)

        py_path = Path(self.examples_dir) / folder / (name + ".py")
        md_path = md_dir / (name + ".md")
        nb_path = ipynb_dir / (name + ".ipynb")
        tutobooks.py_to_nb(py_path, nb_path, fill_outputs=False)
        tutobooks.py_to_md(py_path,
                           nb_path,
                           md_path,
                           img_dir,
                           working_dir=working_dir)
        md_content = open(md_path).read()
        github_repo_dir = str(EXAMPLES_GH_LOCATION / folder)
        site_img_dir = os.path.join("img", "examples", folder, name)
        md_content = self.preprocess_tutobook_md_source(
            md_content, name + ".py", github_repo_dir, img_dir, site_img_dir)
        open(md_path, "w").write(md_content)
コード例 #4
0
    def make_tutobook_sources_for_directory(self, src_dir, target_dir, img_dir,
                                            site_img_dir, github_repo_dir):
        # e.g.
        # make_tutobook_sources_for_directory(
        #    "examples/nlp", "examples/nlp/md", "examples/nlp/img", "img/examples/nlp")
        print("Making tutobook sources for", src_dir)

        working_ipynb_dir = Path(src_dir) / "ipynb"
        if not os.path.exists(working_ipynb_dir):
            os.makedirs(working_ipynb_dir)

        for fname in os.listdir(src_dir):
            if fname.endswith(".py"):
                print("...Processing", fname)
                name = fname[:-3]
                py_path = Path(src_dir) / fname
                nb_path = working_ipynb_dir / (name + ".ipynb")
                md_path = Path(target_dir) / (name + ".md")
                print("...Write to", md_path)
                tutobooks.py_to_md(py_path, nb_path, md_path, img_dir)
                md_content = open(md_path).read()
                md_content = self.preprocess_tutobook_md_source(
                    md_content, fname, github_repo_dir, img_dir, site_img_dir)
                open(md_path, "w").write(md_content)
        shutil.rmtree(working_ipynb_dir)
コード例 #5
0
def py_to_nb_md(dest_dir):
    for file_path in os.listdir('py/'):
        dir_path = 'py'
        file_name = file_path
        py_path = os.path.join(dir_path, file_path)
        file_name_no_ext = os.path.splitext(file_name)[0]
        ext = os.path.splitext(file_name)[1]

        if ext != '.py':
            continue

        nb_path = os.path.join('ipynb', file_name_no_ext + '.ipynb')
        md_path = os.path.join(dest_dir, 'tutorial', file_name_no_ext + '.md')

        tutobooks.py_to_md(py_path, nb_path, md_path, 'templates/img')

        github_repo_dir = 'keras-team/autokeras/blob/master/docs/'
        with open(md_path, 'r') as md_file:
            button_lines = [
                ':material-link: '
                "[**View in Colab**](https://colab.research.google.com/github/"
                + github_repo_dir + "ipynb/" + file_name_no_ext + ".ipynb" +
                ")   &nbsp; &nbsp;"
                # + '<span class="k-dot">•</span>'
                + ':octicons-octoface-16: '
                "[**GitHub source**](https://github.com/" + github_repo_dir +
                "py/" + file_name_no_ext + ".py)",
                "\n",
            ]
            md_content = ''.join(button_lines) + '\n' + md_file.read()

        with open(md_path, 'w') as md_file:
            md_file.write(md_content)
コード例 #6
0
ファイル: autogen.py プロジェクト: Maximilianpy/ai2business
def py_to_nb_md(dest_dir, dir_path="tutorials"):
    for file_path in os.listdir(f"{dir_path}/"):

        file_name = file_path
        py_path = os.path.join(dir_path, file_path)
        file_name_no_ext = os.path.splitext(file_name)[0]
        ext = os.path.splitext(file_name)[1]

        if ext != ".py":
            continue

        nb_path = os.path.join("ipynb", file_name_no_ext + ".ipynb")
        md_path = os.path.join(dest_dir, "tutorials", file_name_no_ext + ".md")

        Path(nb_path).parent.mkdir(exist_ok=True)
        Path(md_path).parent.mkdir(exist_ok=True)

        tutobooks.py_to_nb(py_path, nb_path, fill_outputs=True)
        tutobooks.py_to_md(py_path, nb_path, md_path, "templates/img")
        github_repo_dir = "ai2business/ai2business/blob/main/docs/"
        with open(md_path, "r") as md_file:
            button_lines = [
                ":material-link: "
                "[**View in Colab**](https://colab.research.google.com/github/"
                + github_repo_dir + "ipynb/" + file_name_no_ext + ".ipynb" +
                ")   &nbsp; &nbsp;" + '<span class="k-dot">•</span>' +
                ":material-github: "
                "[**GitHub source**](https://github.com/" + github_repo_dir +
                "tutorials/" + file_name_no_ext + ".py)",
                "\n",
            ]
            md_content = "".join(button_lines) + "\n" + md_file.read()

        with open(md_path, "w+") as md_file:
            md_file.write(md_content)