Python Tooling & Dependencies
Relevant source files
The following files were used as context for generating this wiki page:
The Python ecosystem within the whk project serves as the backbone for its static site generation, document processing, and mathematical rendering workflows. While the project has migrated toward a Pandoc-based architecture using mkpandocs, it retains a robust Python-based automation layer for managing complex build tasks, environment synchronization, and content transformation.
The tooling is centered around uv for modern package management and a series of specialized scripts and filters that handle everything from Jinja2 template rendering to server-side KaTeX processing. These tools ensure that high-school science content—including complex chemical equations and mathematical formulas—is rendered consistently across web and PDF formats.
Sources: README.md:27-31, README.md:120-130, pyproject.toml:1-18
Package Management with uv
The project utilizes uv as its primary Python package manager, providing high-speed dependency resolution and environment locking. The configuration is defined in pyproject.toml, which specifies a minimum Python requirement of version 3.14.
Dependency Groups
The project differentiates between core production dependencies and development-specific tools:
| Category | Description | Key Packages |
|---|---|---|
| Core | Required for site building and rendering. | mkpandocs, jinja2, pyyaml, pillow, mkdocs-materialx |
| Media/Social | Used for SVG processing and image generation. | cairosvg, pillow |
| Dev Group | Tools for local development and UI. | pyside6, rich |
| Overrides | Prevents specific platforms from installing certain packages. | mkdocs, properdocs (sys_platform == 'never') |
Sources: pyproject.toml:7-30, README.md:135-140
The diagram illustrates the relationship between uv's locking mechanism and the dependency definitions in pyproject.toml.
Build Automation: generate.py
The scripts/generate.py script acts as the primary orchestrator for the build process. It manages the transition from project configuration to a final mkpandocs.yml file, which Pandoc then uses for the actual site generation.
Key Logic and Data Flow
The script performs several critical functions:
- Dependency Validation: It checks for the presence of
cairosvgandpillow. If these are missing or cannot be loaded (e.g., due to missing system DLLs likelibcairo-2.dll), it automatically disables "social" plugins to prevent build crashes. - Jinja2 Template Rendering: It uses a template (e.g., from
scripts/templates/build.yml.jinja2) to generate the final configuration. - Navigation Processing: It loads navigation structures from
docs/nav.jsonand sidebars to construct the site hierarchy. - CLI Interface: It provides arguments for
--build(static site generation) and--serve(local preview).
Sources: scripts/generate.py:38-125
Logic flow within generate.py illustrating the pre-build dependency check and configuration generation.
Mathematical Rendering: KaTeX SSR
Mathematical and chemical notation is handled via a specialized Python-based Pandoc filter located at filters/katex_ssr.py. This filter implements Server-Side Rendering (SSR) for KaTeX to ensure formulas are rendered as HTML before reaching the browser, improving performance and SEO.
Filter Architecture
The filter operates on the Pandoc JSON AST (Abstract Syntax Tree):
- AST Walking: It traverses the JSON structure to find all "Math" elements (InlineMath and DisplayMath).
- Bun Integration: It uses the
bunruntime (viabunx) to execute a JavaScript-based KaTeX renderer. This provides access to the full KaTeX ecosystem, including themhchemextension for chemistry. - Replacement: The original Math elements are replaced with
RawInlineHTML containing the pre-rendered KaTeX spans.
Sources: filters/katex_ssr.py:1-45, filters/katex_ssr.py:126-175
Sequence of events for server-side mathematical rendering using the Python Pandoc filter.
Configuration and Template System
The project uses YAML templates to manage different environments (e.g., local serving vs. production builds).
Configuration Elements
The scripts/template.serve.yml file defines the Material for MkDocs theme settings and extension configurations. Notable settings include:
- KaTeX Macros: Defines standard mathematical symbols (e.g.,
\RRfor $\mathbb{R}$) and specialized chemistry notations (e.g.,\polyfor polymers). - Markdown Extensions: Enables advanced features like
admonition,pymdownx.arithmatex(for math), andpymdownx.superfences. - Custom Logic: The
katex-ssrplugin configuration allows for macro definitions and CSS management.
Sources: scripts/template.serve.yml:32-110, scripts/generate.py:128-145
Summary of Tooling Components
| Tool/Script | Language | Primary Responsibility |
|---|---|---|
uv | Python/Rust | Dependency management and environment isolation. |
generate.py | Python | Orchestrating build workflows and template rendering. |
katex_ssr.py | Python | Pandoc filter for server-side math rendering. |
mkpandocs | Python/Go | Final static site generator wrapper. |
Sources: README.md:75-85, pyproject.toml:19-24, filters/katex_ssr.py:15-20
Conclusion
Python tooling in the whk project is designed for robustness and automation. By combining modern package management via uv with specialized Pandoc filters and orchestration scripts, the project manages complex cross-platform dependencies and provides a high-fidelity rendering environment for scientific documentation. This infrastructure allows contributors to focus on content while the Python layer handles the technical complexities of SSR math, chemical notation, and multi-platform build consistency.