in notebooks/notebook_template_review.py [0:0]
def validate(self, notebook: Notebook) -> bool:
"""
Parse the links in the links cell
"""
self.git_link = None
self.colab_link = None
self.colab_enterprise_link = None
self.workbench_link = None
source = ''
ret = True
cell = notebook.get()
for ix in range(len(cell['source'])):
line = cell['source'][ix]
source += line
if '<a href="https://github.com' in line:
self.git_link = line.strip()[9:-2].replace('" target="_blank', '').replace('" target=\'_blank', '')
derived_link = os.path.join('https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/', notebook.path)
if self.git_link != derived_link:
if notebook.report_fix(FixCode.FIX_BAD_LINK, f"fixed GitHub link: {derived_link}"):
fix_link = f"<a href=\"{derived_link}\" target='_blank'>\n"
cell['source'][ix] = fix_link
else:
ret = notebook.report_error(ErrorCode.ERROR_LINK_GIT_BAD, f"bad GitHub link: {self.git_link}")
if '<a href="https://colab.research.google.com/' in line:
self.colab_link = 'https://colab.research.google.com/github/' + line.strip()[50:-2].replace('" target="_blank', '').replace('" target=\'_blank', '')
derived_link = os.path.join('https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks', notebook.path)
if self.colab_link != derived_link:
if notebook.report_fix(FixCode.FIX_BAD_LINK, f"fixed Colab link: {derived_link}"):
fix_link = f"<a href=\"{derived_link}\" target='_blank'>\n"
cell['source'][ix] = fix_link
else:
ret = notebook.report_error(ErrorCode.ERROR_LINK_COLAB_BAD, f"bad Colab link: {self.colab_link}")
if '<a href="https://console.cloud.google.com/vertex-ai/colab/' in line:
self.colab_enterprise_link = line.strip()[9:-2].replace('" target="_blank', '').replace('" target=\'_blank', '')
modified_notebook_path = notebook.path.replace("/", "%2F")
derived_link = os.path.join('https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2F', modified_notebook_path)
if self.workbench_link != derived_link:
if notebook.report_fix(FixCode.FIX_BAD_LINK, f"fixed Colab Enterprise link: {derived_link}"):
fix_link = f"<a href=\"{derived_link}\" target='_blank'>\n"
cell['source'][ix] = fix_link
else:
ret = notebook.report_error(ErrorCode.ERROR_LINK_COLAB_ENTERPRISE_BAD, f"bad Colab Enterprise link: {self.colab_enterprise_link}")
if '<a href="https://console.cloud.google.com/vertex-ai/workbench/' in line:
self.workbench_link = line.strip()[9:-2].replace('" target="_blank', '').replace('" target=\'_blank', '')
derived_link = os.path.join('https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/', notebook.path)
if self.workbench_link != derived_link:
if notebook.report_fix(FixCode.FIX_BAD_LINK, f"fixed Workbench link: {derived_link}"):
fix_link = f"<a href=\"{derived_link}\" target='_blank'>\n"
cell['source'][ix] = fix_link
else:
ret = notebook.report_error(ErrorCode.ERROR_LINK_WORKBENCH_BAD, f"bad Workbench link: {self.workbench_link}")
if 'View on GitHub' not in source or not self.git_link:
ret = notebook.report_error(ErrorCode.ERROR_LINK_GIT_MISSING, 'Missing link for GitHub')
if 'Run in Colab' not in source or not self.colab_link:
ret = notebook.report_error(ErrorCode.ERROR_LINK_COLAB_MISSING, 'Missing link for Colab')
if 'Open in Vertex AI Workbench' not in source or not self.workbench_link:
ret = notebook.report_error(ErrorCode.ERROR_LINK_WORKBENCH_MISSING, 'Missing link for Workbench')
return ret