|
# /// script |
|
# requires-python = ">=3.12" |
|
# dependencies = [ |
|
# "httpx", |
|
# "rich", |
|
# "typer", |
|
# ] |
|
# /// |
|
""" |
|
$ uv run django-top-100.py |
|
""" |
|
|
|
import httpx |
|
import json |
|
import typer |
|
|
|
from pathlib import Path |
|
from rich import box |
|
from rich.console import Console |
|
from rich.table import Table |
|
|
|
|
|
def fetch_and_cache( |
|
*, |
|
url: str, |
|
cache_file: str, |
|
timeout: float = 10.0, |
|
): |
|
filename = Path(cache_file) |
|
if filename.exists(): |
|
return filename.read_text() |
|
|
|
response = httpx.get(url, timeout=timeout) |
|
response.raise_for_status() |
|
|
|
contents = response.text |
|
|
|
Path(cache_file).write_text(contents) |
|
|
|
return contents |
|
|
|
|
|
def main(limit: int = 100): |
|
content = fetch_and_cache( |
|
url="https://hugovk.github.io/top-pypi-packages/top-pypi-packages-30-days.json", |
|
cache_file="top-pypi-packages-30-days.json", |
|
) |
|
|
|
django_count = 0 |
|
django_index = 0 |
|
|
|
projects = json.loads(content) |
|
|
|
table = Table(title=f"Django Packages Stats: Top {limit}", box=box.MARKDOWN) |
|
table.add_column("Rank", justify="right") |
|
table.add_column("Project", justify="left") |
|
table.add_column("Downloads", justify="right") |
|
table.add_column("Percent", justify="right") |
|
|
|
for project in projects["rows"]: |
|
name = project["project"] |
|
download_count = project["download_count"] |
|
|
|
# Process only packages with 'django' in their name |
|
if any(sub in name for sub in ("django", "dj-", "wagtail", "whitenoise")): |
|
if django_index <= limit: |
|
if name == "django": |
|
# For the Django project itself, no percent is calculated. |
|
django_count = download_count |
|
|
|
# Avoid division by zero if django_count hasn't been set |
|
percent = download_count / django_count if django_count else 0 |
|
percent_str = "{:.2%}".format(percent) |
|
package_str = ( |
|
f"[link=https://pypistats.org/packages/{name}]{name}[/link]" |
|
) |
|
|
|
# If the percentage exceeds the threshold, color the row yellow. |
|
if percent >= 0.10: |
|
table.add_row( |
|
f"{django_index}" if django_index > 0 else "", |
|
f"[yellow]{package_str}[/yellow]", |
|
f"{download_count:,}", |
|
percent_str, |
|
) |
|
else: |
|
table.add_row( |
|
f"{django_index}", |
|
package_str, |
|
f"{download_count:,}", |
|
percent_str, |
|
) |
|
|
|
django_index += 1 |
|
|
|
console = Console() |
|
console.print(table) |
|
|
|
|
|
if __name__ == "__main__": |
|
typer.run(main) |