Skip to content

Commit 42550ed

Browse files
Merge pull request #2294 from markmcd:nbfmt_devsite
PiperOrigin-RevId: 604399188
2 parents d13c500 + 12082b1 commit 42550ed

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

tools/tensorflow_docs/tools/nbfmt/__main__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,17 @@ def clean_root(data: Dict[str, Any], filepath: pathlib.Path) -> None:
9999
data, keep=["cells", "metadata", "nbformat_minor", "nbformat"])
100100
# All metadata is optional according to spec, but we use some of it.
101101
notebook_utils.del_entries_except(
102-
data["metadata"], keep=["accelerator", "colab", "kernelspec"])
102+
data["metadata"], keep=["accelerator", "colab", "kernelspec", "google"]
103+
)
103104

104105
metadata = data.get("metadata", {})
105-
colab = metadata.get("colab", {})
106106

107107
# Set top-level notebook defaults.
108108
data["nbformat"] = 4
109109
data["nbformat_minor"] = 0
110110

111111
# Colab metadata
112+
colab = metadata.get("colab", {})
112113
notebook_utils.del_entries_except(
113114
colab, keep=["collapsed_sections", "name", "toc_visible"])
114115
colab["name"] = os.path.basename(filepath)
@@ -128,6 +129,15 @@ def clean_root(data: Dict[str, Any], filepath: pathlib.Path) -> None:
128129
kernelspec["display_name"] = supported_kernels[kernel_name]
129130
metadata["kernelspec"] = kernelspec
130131

132+
# Google metadata
133+
google = metadata.get("google", {})
134+
notebook_utils.del_entries_except(google, keep=["keywords", "image_path"])
135+
# Don't add the field if it's empty.
136+
if google:
137+
metadata["google"] = google
138+
else:
139+
metadata.pop("google", None)
140+
131141
data["metadata"] = metadata
132142

133143

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2024 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Unit tests for nbfmt."""
15+
import pathlib
16+
import unittest
17+
from nbformat import notebooknode
18+
from tensorflow_docs.tools.nbfmt import __main__ as nbfmt
19+
20+
21+
class NotebookFormatTest(unittest.TestCase):
22+
23+
def test_metadata_cleansing(self):
24+
subject_notebook = notebooknode.NotebookNode({
25+
"cells": [],
26+
"metadata": {
27+
"unknown": ["delete", "me"],
28+
"accelerator": "GPU",
29+
"colab": {
30+
"name": "/this/is/clobbered.ipynb",
31+
"collapsed_sections": [],
32+
"deleteme": "pls",
33+
},
34+
"kernelspec": {
35+
"display_name": "Python 2 foreverrrr",
36+
"name": "python2",
37+
"deleteme": "deldeldel",
38+
},
39+
"google": {
40+
"keywords": ["one", "two"],
41+
"image_path": "/foo/img.png",
42+
"more_stuff": "delete me",
43+
},
44+
},
45+
})
46+
47+
expected_notebook = notebooknode.NotebookNode({
48+
"cells": [],
49+
"metadata": {
50+
"accelerator": "GPU",
51+
"colab": {
52+
"name": "test.ipynb",
53+
"collapsed_sections": [],
54+
"toc_visible": True,
55+
},
56+
"kernelspec": {
57+
"display_name": "Python 3",
58+
"name": "python3",
59+
},
60+
"google": {
61+
"keywords": ["one", "two"],
62+
"image_path": "/foo/img.png",
63+
},
64+
},
65+
"nbformat": 4,
66+
"nbformat_minor": 0,
67+
})
68+
69+
nbfmt.clean_root(subject_notebook, pathlib.Path("/path/test.ipynb"))
70+
self.assertEqual(subject_notebook, expected_notebook)
71+
72+
73+
if __name__ == "__main__":
74+
unittest.main()

0 commit comments

Comments
 (0)