Compare commits

...
Author SHA1 Message Date
Morgan Du 32b57bdd54 feat: check and update notebook links 2021-11-15 14:35:49 -08:00
2 changed files with 131 additions and 0 deletions
+86
View File
@@ -0,0 +1,86 @@
from nbconvert.preprocessors import Preprocessor
import argparse
import nbformat
import os
import re
class UpdateNotebookLinks(Preprocessor):
def __init__(self, repo_name):
self._repo_name = repo_name
self._regex = {
"colab": "https://colab\\.research\\.google\\.com/github/GoogleCloudPlatform/.*\\.ipynb",
"github": "https://github\\.com/GoogleCloudPlatform/.*\\.ipynb"
}
self._prefix = {
"colab": "https://colab.research.google.com/github/GoogleCloudPlatform/",
"github": "https://github.com/GoogleCloudPlatform/"
}
def generate_link(self, notebook_file_path, type):
return self._prefix[type] + self._repo_name + "/blob/master" + notebook_file_path
def search_link(self, text, type):
return re.search(self._regex[type], text)
def replace_link(self, text, notebook_file_path, type):
return re.sub(self._regex[type], self.generate_link(notebook_file_path, type), text)
def update_text(self, original_text, notebook_file_path):
updated_text = self.replace_link(original_text, notebook_file_path, 'colab')
updated_text = self.replace_link(updated_text, notebook_file_path, 'github')
return updated_text
def preprocess(self, notebook, notebook_file_path):
updated = False
for cell in notebook.cells:
if cell.cell_type == 'markdown' and (
self.search_link(cell.source, 'colab') or self.search_link(cell.source, 'github')):
cell.source = self.update_text(cell.source, notebook_file_path)
updated = True
break
return notebook, updated
def main(args):
notebook_file_paths = list()
for root, dirs, files in os.walk(args.repo_path):
for file in files:
if file.endswith(".ipynb"):
notebook_file_paths.append(os.path.join(root.replace(args.repo_path, ''), file))
for notebook_file_path in notebook_file_paths:
with open(args.repo_path + notebook_file_path) as f:
nb = nbformat.read(f, as_version=4)
update_notebook_links_preprocessor = UpdateNotebookLinks(repo_name=args.repo_name)
nb, updated = update_notebook_links_preprocessor.preprocess(nb, notebook_file_path)
if updated:
with open(args.repo_path + notebook_file_path, mode="w", encoding="utf-8") as f:
nbformat.write(nb, f)
return
parser = argparse.ArgumentParser(description="Run changed notebooks.")
parser.add_argument(
"--repo-path",
type=str,
default=os.getcwd(),
help="The local path containing the folders of notebooks that should be updated.",
)
parser.add_argument(
"--repo-name",
type=str,
default='vertex-ai-samples',
help="The repository name used to generate the notebook links.",
)
args = parser.parse_args()
if __name__ == '__main__':
main(args)
Executable
+45
View File
@@ -0,0 +1,45 @@
#!/bin/bash
export search_dir=community-content
notebook_filenames=$(find $search_dir -type f -name "*.ipynb")
export repo_name=vertex-ai-samples
export search_links=https:.*\.ipynb
export search_console=https://console\.cloud\.google\.com/ai/platform/notebooks/deploy-notebook?download_url=.*\.ipynb
export search_colab=https://colab\.research\.google\.com/github/GoogleCloudPlatform/.*/blob/master/.*\.ipynb
export search_github=https://github\.com/GoogleCloudPlatform/.*/blob/master/.*\.ipynb
export console_prefix=https://console.cloud.google.com/ai/platform/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform
export colab_prefix=https://colab.research.google.com/github/GoogleCloudPlatform
export github_prefix=https://github.com/GoogleCloudPlatform
for notebook_filename in $notebook_filenames
do
echo "Notebook: $notebook_filename"
echo " Existing Linkes"
grep $search_links $notebook_filename
grep $search_console $notebook_filename
grep $search_colab $notebook_filename
grep $search_github $notebook_filename
export replace_console="$console_prefix/$repo_name/raw/master/$notebook_filename"
export replace_colab="$colab_prefix/$repo_name/blob/master/$notebook_filename"
export replace_github="$github_prefix/$repo_name/blob/master/$notebook_filename"
echo " New Linkes"
echo "replace_console: $replace_console"
echo "replace_colab: $replace_colab"
echo "replace_github: $replace_github"
sed -i -e s,$search_console,$replace_console,g $notebook_filename
sed -i -e s,$search_colab,$replace_colab,g $notebook_filename
sed -i -e s,$search_github,$replace_github,g $notebook_filename
done
find $search_dir -name '*.ipynb-e' -delete