Path: blob/master/scripts/update_readme.py
869 views
import os1import re2from datetime import datetime3from urllib.parse import quote45# Directory where notebooks are stored6notebooks_dir = 'notebooks'78# Read the README.md file9with open('README.md', 'r') as file:10content = file.read()1112# Update the date13updated_date = datetime.now().strftime("<font color='purple' size=2.5><i>Updated on %b %Y</i></font>")14content = re.sub(r"<font color='purple' size=2.5><i>Updated on .*</i></font>", updated_date, content)1516# Define the new base URL for nbviewer links17new_base_url = "https://www.weijiechen.com/linear-algebra-with-python-book/qmd/"1819# Function to generate new lecture link20def generate_lecture_link(filename):21lecture_name = filename.replace('.ipynb', '')22encoded_filename = quote(filename)23return f"[{lecture_name}]({new_base_url}/{encoded_filename})"2425# Get list of notebook files and sort them numerically by chapter26notebook_files = sorted(27[f for f in os.listdir(notebooks_dir) if f.endswith('.ipynb')],28key=lambda x: int(re.search(r'\d+', x).group())29)3031# Create a new content section for the lectures32lectures_section = "\n".join([generate_lecture_link(file) + "<br>" for file in notebook_files])3334# Update the lectures section in the README content35content = re.sub(r'## Contents(.|\n)*?(?=##)', f'## Contents\n\n{lectures_section}\n\n', content, flags=re.DOTALL)3637# Write the updated content back to the README.md file38with open('README.md', 'w') as file:39file.write(content)404142