Code
import pandas as pd
import yaml
from itables import init_notebook_mode, show
import itables.options as opt
from pathlib import Path
import re
# Initialize itables
init_notebook_mode(all_interactive=True)
# Function to make DOIs clickable
def make_doi_clickable(text):
doi_pattern = r'(https://doi\.org/[^\s)]+)'
return re.sub(doi_pattern, r'<a href="\1" target="_blank">\1</a>', text)
# Create relative path to repos file
# Goes up two levels (../../) from current .qmd location to reach root
repo_path = Path('../../data/repos.yml')
# Load YAML data
with open(repo_path, 'r') as f:
data = yaml.safe_load(f)
# Convert to DataFrame
df = pd.DataFrame(data['repos'])
# Make DOIs clickable in reference column
df['reference'] = df['reference'].apply(make_doi_clickable)
# Format the repo column to include HTML links
df['repo'] = df.apply(lambda x: f'<a href="{x["repo"]["url"]}" target="_blank">{x["repo"]["name"]}</a>', axis=1)
# Join topics with semicolons
df['topics'] = df['topics'].apply(lambda x: '; '.join(x))
# Configure itables options
opt.classes = ["display", "cell-border"]
opt.columnDefs = [
{ # Reference column
"width": "50%",
"targets": 0
},
{ # Year column
"width": "5%",
"targets": 1,
"className": "dt-left"
},
{ # Repo column
"width": "15%",
"targets": 2
},
{ # Topics column
"width": "30%",
"targets": 3
}
]
opt.showIndex = False
# By default, show newest to oldest
sorted_df = df.sort_values('year', ascending=False)
# Display the interactive table
show(sorted_df)| reference | year | repo | topics |
|---|---|---|---|
Loading ITables v2.2.4 from the init_notebook_mode cell...
(need help?) |