Skip to content

Commit 6eb0802

Browse files
committed
tools: update export_to_ipynb.py
1 parent 3065641 commit 6eb0802

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

tools/export_to_ipynb.py

+26-21
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
input_file = sys.argv[1]
2525
print(f"reading {input_file}")
26-
with open(input_file) as fpin:
26+
with open(input_file, encoding="utf-8") as fpin:
2727
text = fpin.read()
2828

2929
# Compute output file path.
@@ -40,12 +40,15 @@
4040
nbook = v3.reads_py("")
4141
nbook = v4.upgrade(nbook) # Upgrade v3 to v4
4242

43+
METADATA = {"language_info": {"name": "python"}}
44+
nbook["metadata"] = METADATA
45+
4346
print("Adding copyright cell...")
44-
google = "##### Copyright 2024 Google LLC."
45-
nbook["cells"].append(v4.new_markdown_cell(source=google, id="google"))
47+
GOOGLE = "##### Copyright 2024 Google LLC."
48+
nbook["cells"].append(v4.new_markdown_cell(source=GOOGLE, id="google"))
4649

4750
print("Adding license cell...")
48-
apache = """Licensed under the Apache License, Version 2.0 (the "License");
51+
APACHE = """Licensed under the Apache License, Version 2.0 (the "License");
4952
you may not use this file except in compliance with the License.
5053
You may obtain a copy of the License at
5154
@@ -57,43 +60,43 @@
5760
See the License for the specific language governing permissions and
5861
limitations under the License.
5962
"""
60-
nbook["cells"].append(v4.new_markdown_cell(source=apache, id="apache"))
63+
nbook["cells"].append(v4.new_markdown_cell(source=APACHE, id="apache"))
6164

6265
print("Adding Title cell...")
6366
basename = "# " + os.path.basename(input_file).replace(".py", "")
6467
nbook["cells"].append(v4.new_markdown_cell(source=basename, id="basename"))
6568

6669
print("Adding link cell...")
67-
github_logo = (
70+
GITHUB_LOGO = (
6871
"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png"
6972
)
70-
github_path = "https://github.com/google/or-tools/blob/main/" + input_file
73+
GITHUB_PATH = "https://github.com/google/or-tools/blob/main/" + input_file
7174

72-
colab_path = (
75+
COLAB_PATH = (
7376
"https://colab.research.google.com/github/google/or-tools/blob/main/" + output_file
7477
)
75-
colab_logo = (
78+
COLAB_LOGO = (
7679
"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png"
7780
)
7881
link = f"""<table align=\"left\">
7982
<td>
80-
<a href=\"{colab_path}\"><img src=\"{colab_logo}\"/>Run in Google Colab</a>
83+
<a href=\"{COLAB_PATH}\"><img src=\"{COLAB_LOGO}\"/>Run in Google Colab</a>
8184
</td>
8285
<td>
83-
<a href=\"{github_path}\"><img src=\"{github_logo}\"/>View source on GitHub</a>
86+
<a href=\"{GITHUB_PATH}\"><img src=\"{GITHUB_LOGO}\"/>View source on GitHub</a>
8487
</td>
8588
</table>"""
8689
nbook["cells"].append(v4.new_markdown_cell(source=link, id="link"))
8790

8891
print("Adding ortools install cell...")
89-
install_doc = (
92+
INSTALL_DOC = (
9093
"First, you must install "
9194
"[ortools](https://pypi.org/project/ortools/) package in this "
9295
"colab."
9396
)
94-
nbook["cells"].append(v4.new_markdown_cell(source=install_doc, id="doc"))
95-
install_cmd = "%pip install ortools"
96-
nbook["cells"].append(v4.new_code_cell(source=install_cmd, id="install"))
97+
nbook["cells"].append(v4.new_markdown_cell(source=INSTALL_DOC, id="doc"))
98+
INSTALL_CMD = "%pip install ortools"
99+
nbook["cells"].append(v4.new_code_cell(source=INSTALL_CMD, id="install"))
97100

98101
print("Adding code cell...")
99102
all_blocks = ast.parse(text).body
@@ -102,7 +105,7 @@
102105
line_start[0] = 0
103106
lines = text.split("\n")
104107

105-
full_text = ""
108+
FULL_TEXT = ""
106109
for idx, (c_block, s, e) in enumerate(
107110
zip(all_blocks, line_start, line_start[1:] + [len(lines)])
108111
):
@@ -148,7 +151,7 @@
148151
and c_block.names[0].name == "flags"
149152
):
150153
print(f"Rewrite import {c_block.module}.{c_block.names[0].name}...")
151-
full_text += "from ortools.sat.colab import flags\n"
154+
FULL_TEXT += "from ortools.sat.colab import flags\n"
152155
# Unwrap __main__ function
153156
elif isinstance(c_block, ast.If) and c_block.test.comparators[0].s == "__main__":
154157
print("Unwrapping main function...")
@@ -173,7 +176,7 @@
173176
filtered_lines = [
174177
re.sub(r"app.run\((.*)\)$", r"\1()", s) for s in filtered_lines
175178
]
176-
full_text += "\n".join(filtered_lines) + "\n"
179+
FULL_TEXT += "\n".join(filtered_lines) + "\n"
177180
# Others
178181
else:
179182
print("Appending block...")
@@ -186,12 +189,14 @@
186189
filtered_lines = list(
187190
filter(lambda l: not re.search(r"# \[END .*\]$", l), filtered_lines)
188191
)
189-
full_text += "\n".join(filtered_lines) + "\n"
192+
FULL_TEXT += "\n".join(filtered_lines) + "\n"
190193

191-
nbook["cells"].append(v4.new_code_cell(source=full_text, id="code"))
194+
nbook["cells"].append(
195+
v4.new_code_cell(source=FULL_TEXT, id="code")
196+
)
192197

193198
jsonform = v4.writes(nbook) + "\n"
194199

195200
print(f"writing {output_file}")
196-
with open(output_file, "w") as fpout:
201+
with open(output_file, mode="w", encoding="utf-8") as fpout:
197202
fpout.write(jsonform)

tools/generate_all_notebooks.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
# usage: ./tools/generate_all_notebooks.sh
16-
set -e
16+
set -eu
1717

1818
DIR="${BASH_SOURCE%/*}"
1919
if [[ ! -d "${DIR}" ]]; then

0 commit comments

Comments
 (0)