CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
weijie-chen

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: weijie-chen/Linear-Algebra-With-Python
Path: blob/master/scripts/update_readme.py
Views: 449
1
import os
2
import re
3
from datetime import datetime
4
from urllib.parse import quote
5
6
# Directory where notebooks are stored
7
notebooks_dir = 'notebooks'
8
9
# Read the README.md file
10
with open('README.md', 'r') as file:
11
content = file.read()
12
13
# Update the date
14
updated_date = datetime.now().strftime("<font color='purple' size=2.5><i>Updated on %b %Y</i></font>")
15
content = re.sub(r"<font color='purple' size=2.5><i>Updated on .*</i></font>", updated_date, content)
16
17
# Define the new base URL for nbviewer links
18
new_base_url = "https://www.weijiechen.com/linear-algebra-with-python-book/qmd/"
19
20
# Function to generate new lecture link
21
def generate_lecture_link(filename):
22
lecture_name = filename.replace('.ipynb', '')
23
encoded_filename = quote(filename)
24
return f"[{lecture_name}]({new_base_url}/{encoded_filename})"
25
26
# Get list of notebook files and sort them numerically by chapter
27
notebook_files = sorted(
28
[f for f in os.listdir(notebooks_dir) if f.endswith('.ipynb')],
29
key=lambda x: int(re.search(r'\d+', x).group())
30
)
31
32
# Create a new content section for the lectures
33
lectures_section = "\n".join([generate_lecture_link(file) + "<br>" for file in notebook_files])
34
35
# Update the lectures section in the README content
36
content = re.sub(r'## Contents(.|\n)*?(?=##)', f'## Contents\n\n{lectures_section}\n\n', content, flags=re.DOTALL)
37
38
# Write the updated content back to the README.md file
39
with open('README.md', 'w') as file:
40
file.write(content)
41
42