Skip to content

Commit 1c6d47c

Browse files
[SN-121] Cleaned up readme created python file to auto generate (#1617)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 0312789 commit 1c6d47c

File tree

8 files changed

+482
-72
lines changed

8 files changed

+482
-72
lines changed

examples/README.md

Lines changed: 348 additions & 72 deletions
Large diffs are not rendered by default.

examples/scripts/generate_readme.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import pandas
2+
import glob
3+
from collections import defaultdict
4+
5+
"""
6+
Script used to generate readme programmatically works by taking the links of all the notebooks
7+
then dividing them to different tables based on directory name. Pandas is used to make the tables. Using inline HTML to support our doc page.
8+
"""
9+
10+
SDK_EXAMPLE_HEADER = """
11+
# Labelbox SDK Examples\n
12+
- Learn how to use the SDK by following along\n
13+
- Run in google colab, view the notebooks on github, or clone the repo and run locally\n
14+
"""
15+
16+
COLAB_TEMPLATE = "https://colab.research.google.com/github/Labelbox/labelbox-python/blob/develop/examples/{filename}"
17+
GITHUB_TEMPLATE = (
18+
"https://github.com/Labelbox/labelbox-python/tree/develop/examples/{filename}"
19+
)
20+
21+
22+
def create_header(link: str) -> str:
23+
"""Creates headers of tables with h2 tags to support readme docs
24+
25+
Args:
26+
link (str): file path
27+
28+
Returns:
29+
str: formatted file path for header
30+
"""
31+
# Splits up link uses directory name
32+
split_link = link.split("/")[1].replace("_", " ").split(" ")
33+
header = []
34+
35+
# Capitalize first letter of each word
36+
for word in split_link:
37+
header.append(word.capitalize())
38+
return f"<h2>{' '.join(header)}</h2>"
39+
40+
41+
def create_title(link: str) -> str:
42+
"""Create notebook titles will be name of notebooks with _ replaced with spaces and file extension removed
43+
44+
Args:
45+
link (str): file path
46+
47+
Returns:
48+
str: formatted file path for notebook title
49+
"""
50+
split_link = link.split(".")[-2].split("/")[-1].replace("_", " ").split(" ")
51+
title = []
52+
53+
# List to lower case certain words and list to keep certain acronyms capitalized
54+
lower_case_words = ["to"]
55+
acronyms = ["html", "pdf", "llm", "dicom", "sam"]
56+
57+
for word in split_link:
58+
if word.lower() in acronyms:
59+
title.append(word.upper())
60+
elif word.lower() in lower_case_words:
61+
title.append(word.lower())
62+
else:
63+
title.append(word.capitalize())
64+
return " ".join(title).split(".")[0]
65+
66+
67+
def make_link(link: str, photo: str, link_type: str) -> str:
68+
"""Creates the links for the notebooks as an anchor tag
69+
70+
Args:
71+
link (str): file path
72+
photo (str): photo link
73+
link_type (str): type of link (github, google colab)
74+
75+
Returns:
76+
str: anchor tag with image
77+
"""
78+
return f'<a href="{link}" target="_blank"><img src="{photo}" alt="Open In {link_type}" onclick="(function prevent(e){{e.preventDefault()}}()"></a>'
79+
80+
81+
def make_links_dict(links: str) -> defaultdict[list]:
82+
"""Creates dictionary needed for pandas to generate the table takes all the links and makes each directory its own table
83+
84+
Args:
85+
links (list[str]): list of links to notebooks from glob
86+
87+
Returns:
88+
defaultdict[list]: returns dict that is in pandas dataFrame format
89+
"""
90+
link_dict = defaultdict(list)
91+
for link in links:
92+
split_link = link.split("/")[1]
93+
link_dict[split_link].append(link)
94+
return link_dict
95+
96+
97+
def make_table(base: str = "") -> str:
98+
"""main function to make table
99+
100+
Args:
101+
base (str): Header of file generated. Defaults to an empty string.
102+
103+
Returns:
104+
str: markdown string file
105+
"""
106+
link_dict = make_links_dict(glob.glob("**/examples/**/*.ipynb", recursive=True))
107+
generated_markdown = base
108+
for link_list in link_dict.values():
109+
pandas_dict = {"Notebook": [], "Github": [], "Google Colab": []}
110+
generated_markdown += f"{create_header(link_list[0])}\n\n"
111+
for link in link_list:
112+
pandas_dict["Notebook"].append(create_title(link))
113+
pandas_dict["Github"].append(
114+
make_link(
115+
GITHUB_TEMPLATE.format(filename="/".join(link.split("/")[1:])),
116+
"https://img.shields.io/badge/GitHub-100000?logo=github&logoColor=white",
117+
"Github",
118+
)
119+
)
120+
pandas_dict["Google Colab"].append(
121+
make_link(
122+
COLAB_TEMPLATE.format(filename="/".join(link.split("/")[1:])),
123+
"https://colab.research.google.com/assets/colab-badge.svg",
124+
"Colab",
125+
)
126+
)
127+
df = pandas.DataFrame(pandas_dict)
128+
generated_markdown += f"{df.to_html(col_space={'Notebook':400}, index=False, escape=False, justify='left')}\n\n"
129+
return generated_markdown
130+
131+
132+
if __name__ == "__main__":
133+
with open("./examples/README.md", "w") as readme:
134+
readme.write(f"{make_table(SDK_EXAMPLE_HEADER).rstrip()}\n")

0 commit comments

Comments
 (0)