from __future__ import annotations import html import json import os import re import shlex import shutil import subprocess import sys import time import uuid from datetime import datetime, timezone from pathlib import Path PROJECT_DIR_NAME = ".trackio" LOGBOOK_SUBDIR = "logbook" METADATA_FILE = "metadata.json" SCHEMA_VERSION = 1 ROOT_SLUG = "index" VIEWER_DIR = Path(__file__).parent / "frontend_templates" / "logbook" VIEWER_FILES = [ "index.html", "logbook.css", "logbook.js", "trackio-logo.png", "trackio-logo-light.png", "trackio-wordmark-dark.png", "bucket-icon.svg", ] TOC_HEADING = "## Pages" TOC_HEADER = "| Page |" TOC_SEP = "| --- |" TOC_PLACEHOLDER_TOKENS = ("logbook note", "logbook cell markdown", "logbook page") STATUS_COL_RE = re.compile(r"\b(status|state)\b", re.I) LINK_COL_RE = re.compile(r"\b(page|experiment|name|title)\b", re.I) CELL_TYPES = {"markdown", "code", "figure", "artifact", "dashboard"} ARTIFACT_URI_PREFIX = "trackio-artifact://" LOCAL_DASHBOARD_PREFIX = "trackio-local-dashboard://" SPACE_URL_RE = re.compile(r"https://huggingface\.co/spaces/[^\s<>)\"'`]+") DEFAULT_HEAD = 3 DEFAULT_TAIL = 3 DEFAULT_RAW_LIMIT = 500 FENCE_RE = re.compile(r"(`{3,4}|~{3,4})([^\n]*)\n([\s\S]*?)\n\1") RUN_OUTPUT_LIMIT = 20_000 RUN_OUTPUT_HEAD = 2_000 RUN_OUTPUT_TAIL = RUN_OUTPUT_LIMIT - RUN_OUTPUT_HEAD TRY_NUM_PORTS = int(os.getenv("GRADIO_NUM_PORTS", "100")) CELL_RE = re.compile( r"(^|\n)---\n\n([\s\S]*?)(?=\n---\n" def _shorten(text: str, limit: int = 80) -> str: text = re.sub(r"\s+", " ", text).strip() if len(text) <= limit: return text return text[: limit - 1].rstrip() + "…" def _strip_markdown_title(text: str) -> str: text = re.sub(r"!\[([^\]]*)\]\([^)]+\)", r"\1", text) text = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", text) text = re.sub(r"[*_`>#]+", "", text) return text.strip(" -:\t") def _default_cell_title(cell_type: str, body: str, metadata: dict) -> str: command = metadata.get("command") if command: try: return _shorten(f"Run: {shlex.join(command)}") except TypeError: return "Run" if cell_type == "code": titled = re.search(r"^````\w*\s+title=([^\n]+)", body, re.M) if titled: return _shorten(f"Code: {titled.group(1).strip()}") return "Code cell" if cell_type == "figure": return "Figure" for line in body.splitlines(): line = line.strip() if not line or line.startswith("````") or line.startswith("