Skip to content
This repository was archived by the owner on Aug 21, 2023. It is now read-only.

Fixes Broken links#1488

Merged
Eric-Arellano merged 5 commits intoQiskit:masterfrom
divshacker:broken_links
Jul 26, 2023
Merged

Fixes Broken links#1488
Eric-Arellano merged 5 commits intoQiskit:masterfrom
divshacker:broken_links

Conversation

@divshacker
Copy link
Copy Markdown
Contributor

@divshacker divshacker commented Jul 6, 2023

Summary

I have coded a script which (when directory is specified) checks for the broken links in the Jupyter notebooks and tells its exact place and the error it shows.

import os
import nbformat as nbf
import re
import requests
# Code to populate file_list with relevant .ipynb files
file_list = []

for root, dirs, files in os.walk("qiskit-tutorials/tutorials/"):
    for file in files:
        if file.endswith(".ipynb"):
            file_list.append(os.path.join(root, file))  # Including the relative path of the file

INLINE_LINK_RE = re.compile(r'\[([^\]]+)\]\(([^)]+)\)')
for file_path in file_list:
    try:
        with open(file_path) as file:
            nb = nbf.read(file, 4)
    except OSError as e:
        print(f"Error occurred while reading file: {file_path}")
        print("Error message:", str(e))
        print()
        continue

    for cell in nb["cells"]:
        if cell["cell_type"] != "markdown":
            continue
        links = list(INLINE_LINK_RE.findall(cell["source"]))
        for link in links:
            if not link:
                continue
            link_text, link_url = link
            if link_url.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
                continue  # Skip image links
            try:
                response = requests.get(link_url)
                if response.status_code != 200:
                    print(f"Broken link found in file: {file_path}")
                    print(f"Link: {link_text}")
                    print(f"URL: {link_url}")
                    print("HTTP status code:", response.status_code)
                    print()
            except requests.RequestException as e:
                print(f"Error occurred while checking link in file: {file_path}")
                print(f"Link: {link_text}")
                print(f"URL: {link_url}")
                print("Error message:", str(e))
                print()

Details and comments

This code is part of QAMP Project Issues in Qiskit Tutorials #37
which can read many files at once and can find broken links with their exact position and error code it is showing.

I ran this above code throughout the repo and checked each and every Jupyter notebook files, based on the output I have fixed many links. This PR is just the Last PR of the of project, which fixes the remaining broken links. All the other broken links are already fixed in the past PRs which are made throughout this project. #1445 #1446 #1467 #1483 #1484.
This project also fixes #1402

@review-notebook-app
Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Copy link
Copy Markdown
Member

@frankharkins frankharkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Can you reset the unintentional changes in 01_IQPE.ipynb please?

@divshacker
Copy link
Copy Markdown
Contributor Author

divshacker commented Jul 26, 2023

I changed two files in this PR, but as the second file is not needed by maintainers, so they deleted it. I somehow resolved the conflict and now it looks fine. As the file is deleted from main repo so there is no point in improving it so I removed my commit too.
Thanks.

Copy link
Copy Markdown
Member

@frankharkins frankharkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@Eric-Arellano Eric-Arellano merged commit e9990c1 into Qiskit:master Jul 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

Improve An introduction to algorithms in qiskit tutorial if needed

3 participants