Skip to content

Commit 12082b1

Browse files
committed
Add support for google in notebook metadata.
These fields will be used to support document-specific metadata that needs to be pushed to the publishing system.
1 parent b12399e commit 12082b1

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

tools/tensorflow_docs/tools/nbfmt/__main__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ 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"])
103103

104104
metadata = data.get("metadata", {})
105-
colab = metadata.get("colab", {})
106105

107106
# Set top-level notebook defaults.
108107
data["nbformat"] = 4
109108
data["nbformat_minor"] = 0
110109

111110
# Colab metadata
111+
colab = metadata.get("colab", {})
112112
notebook_utils.del_entries_except(
113113
colab, keep=["collapsed_sections", "name", "toc_visible"])
114114
colab["name"] = os.path.basename(filepath)
@@ -128,6 +128,11 @@ def clean_root(data: Dict[str, Any], filepath: pathlib.Path) -> None:
128128
kernelspec["display_name"] = supported_kernels[kernel_name]
129129
metadata["kernelspec"] = kernelspec
130130

131+
# Google metadata
132+
google = metadata.get("google", {})
133+
notebook_utils.del_entries_except(google, keep=["keywords", "image_path"])
134+
metadata["google"] = google
135+
131136
data["metadata"] = metadata
132137

133138

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)