|
5 | 5 | from yapf.yapflib.yapf_api import FormatCode |
6 | 6 |
|
7 | 7 | BANNER_CELL = { |
8 | | - "cell_type": |
9 | | - "markdown", |
10 | | - "id": |
11 | | - "db768cda", |
| 8 | + "cell_type": "markdown", |
| 9 | + "id": "db768cda", |
12 | 10 | "metadata": {}, |
13 | 11 | "source": [ |
14 | | - "<td>\n", |
15 | | - " <a target=\"_blank\" href=\"https://labelbox.com\" ><img src=\"https://labelbox.com/blog/content/images/2021/02/logo-v4.svg\" width=256/></a>\n", |
16 | | - "</td>" |
17 | | - ] |
| 12 | + "<td>", |
| 13 | + ' <a target="_blank" href="https://labelbox.com" ><img src="https://labelbox.com/blog/content/images/2021/02/logo-v4.svg" width=256/></a>', |
| 14 | + "</td>", |
| 15 | + ], |
18 | 16 | } |
19 | 17 |
|
20 | 18 | LINK_CELL = { |
21 | | - "cell_type": |
22 | | - "markdown", |
23 | | - "id": |
24 | | - "cb5611d0", |
| 19 | + "cell_type": "markdown", |
| 20 | + "id": "cb5611d0", |
25 | 21 | "metadata": {}, |
26 | 22 | "source": [ |
27 | | - "<td>\n", "<a href=\"{colab}\" target=\"_blank\"><img\n", |
28 | | - "src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n", |
29 | | - "</td>\n", "\n", "<td>\n", |
30 | | - "<a href=\"{github}\" target=\"_blank\"><img\n", |
31 | | - "src=\"https://img.shields.io/badge/GitHub-100000?logo=github&logoColor=white\" alt=\"GitHub\"></a>\n", |
32 | | - "</td>" |
33 | | - ] |
| 23 | + "<td>", |
| 24 | + '<a href="{colab}" target="_blank"><img', |
| 25 | + 'src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"></a>', |
| 26 | + "</td>", |
| 27 | + "\n", |
| 28 | + "<td>", |
| 29 | + '<a href="{github}" target="_blank"><img', |
| 30 | + 'src="https://img.shields.io/badge/GitHub-100000?logo=github&logoColor=white" alt="GitHub"></a>', |
| 31 | + "</td>", |
| 32 | + ], |
34 | 33 | } |
35 | 34 |
|
36 | | -COLAB_TEMPLATE = "https://colab.research.google.com/github/Labelbox/labelbox-python/blob/master/examples/{filename}" |
37 | | -GITHUB_TEMPLATE = "https://github.com/Labelbox/labelbox-python/tree/master/examples/{filename}" |
| 35 | +COLAB_TEMPLATE = "https://colab.research.google.com/github/Labelbox/labelbox-python/blob/develop/examples/{filename}" |
| 36 | +GITHUB_TEMPLATE = ( |
| 37 | + "https://github.com/Labelbox/labelbox-python/tree/develop/examples/{filename}" |
| 38 | +) |
38 | 39 |
|
39 | 40 |
|
40 | 41 | def format_cell(source): |
41 | | - for line in source.split('\n'): |
42 | | - if line.strip().startswith(('!', '%')): |
| 42 | + for line in source.split("\n"): |
| 43 | + if line.strip().startswith(("!", "%")): |
43 | 44 | return source |
44 | 45 | return FormatCode(source, style_config="google")[0] |
45 | 46 |
|
46 | 47 |
|
47 | 48 | def add_headers(file_name): |
48 | | - with open(file_name, 'r') as file: |
| 49 | + with open(file_name, "r") as file: |
49 | 50 | data = json.load(file) |
50 | 51 |
|
51 | 52 | colab_path = COLAB_TEMPLATE.format(filename=file_name) |
52 | 53 | github_path = GITHUB_TEMPLATE.format(filename=file_name) |
53 | 54 |
|
54 | 55 | link_cell = deepcopy(LINK_CELL) |
55 | 56 |
|
56 | | - link_cell['source'][1] = link_cell['source'][1].format(colab=colab_path) |
57 | | - link_cell['source'][6] = link_cell['source'][6].format(github=github_path) |
| 57 | + link_cell["source"][1] = link_cell["source"][1].format(colab=colab_path) |
| 58 | + link_cell["source"][6] = link_cell["source"][6].format(github=github_path) |
58 | 59 |
|
59 | | - data['cells'] = [BANNER_CELL, link_cell] + data['cells'] |
| 60 | + data["cells"] = [BANNER_CELL, link_cell] + data["cells"] |
60 | 61 |
|
61 | | - with open(file_name, 'w') as file: |
| 62 | + with open(file_name, "w") as file: |
62 | 63 | file.write(json.dumps(data, indent=4)) |
63 | 64 |
|
64 | 65 | print("Formatted", file_name) |
65 | 66 |
|
66 | 67 |
|
67 | 68 | def format_file(file_name): |
68 | | - with open(file_name, 'r') as file: |
| 69 | + with open(file_name, "r") as file: |
69 | 70 | data = json.load(file) |
70 | 71 |
|
71 | 72 | idx = 1 |
72 | | - for cell in data['cells']: |
73 | | - if cell['cell_type'] == 'code': |
74 | | - cell['execution_count'] = idx |
75 | | - if isinstance(cell['source'], list): |
76 | | - cell['source'] = ''.join(cell['source']) |
77 | | - cell['source'] = format_cell(cell['source']) |
| 73 | + for cell in data["cells"]: |
| 74 | + if cell["cell_type"] == "code": |
| 75 | + cell["execution_count"] = idx |
| 76 | + if isinstance(cell["source"], list): |
| 77 | + cell["source"] = "".join(cell["source"]) |
| 78 | + cell["source"] = format_cell(cell["source"]) |
78 | 79 | idx += 1 |
79 | | - if cell['source'].endswith('\n'): |
80 | | - cell['source'] = cell['source'][:-1] |
| 80 | + if cell["source"].endswith("\n"): |
| 81 | + cell["source"] = cell["source"][:-1] |
81 | 82 |
|
82 | | - with open(file_name, 'w') as file: |
| 83 | + with open(file_name, "w") as file: |
83 | 84 | file.write(json.dumps(data, indent=4)) |
84 | 85 | print("Formatted", file_name) |
85 | 86 |
|
86 | 87 |
|
87 | | -if __name__ == '__main__': |
88 | | - for file in glob.glob("*/*.ipynb"): |
| 88 | +if __name__ == "__main__": |
| 89 | + for file in glob.glob("**/*.ipynb", recursive=True): |
89 | 90 | format_file(file) |
0 commit comments