Skip to main content

TreeViz-native Python helpers for building .treeviz.json sessions

Project description

TreeViz Python

treeviz-phylo builds TreeViz-native .treeviz.json sessions from Python scripts and notebooks. It handles tree input, metadata binding, schema validation, notebook iframe views, basic tree inspection, and static export through an external TreeViz renderer.

The package does not vendor the TreeViz browser app or frontend source. It ships only the Python helpers and the TreeViz session schema.

Install after a release is uploaded to PyPI:

pip install treeviz-phylo

Notebook extra:

pip install "treeviz-phylo[notebook]"

From a source checkout:

pixi run -e py python -m pip install -e packages/python

Minimal use:

from treeviz import (
    binding_diagnostics,
    build_session,
    leaf_names,
    render_tree,
    save_session,
    tree_stats,
    validate_session,
    view_session,
)

tree = "(A:0.1,B:0.2,(C:0.3,D:0.4):0.5);"
metadata = [
    {"id": "A", "group": "alpha", "value": 1.2},
    {"id": "B", "group": "alpha", "value": 0.8},
    {"id": "C", "group": "beta", "value": 2.1},
    {"id": "D", "group": "beta", "value": 1.6},
]
tracks = [
    {"kind": "color_strip", "column_key": "group", "title": "Group"},
    {"kind": "gradient", "column_key": "value", "title": "Value"},
]

session = build_session(tree, metadata=metadata, tracks=tracks, row_key_column="id")
validate_session(session)
save_session(session, "example.treeviz.json")
print(leaf_names(session))
print(tree_stats(session))
print(binding_diagnostics(session))

view = view_session(session, open_browser=False)
print(view.url)

Static export requires a TreeViz renderer command, such as the source checkout CLI. render_tree defaults to bun run treeviz render; pass cwd when the current working directory is not the checkout root.

from pathlib import Path

render_tree(
    tree,
    metadata=metadata,
    tracks=tracks,
    format="pdf",
    output="example.pdf",
    width=1400,
    height=620,
    auto_crop=True,
    crop_padding=24,
    metrics="example.metrics.json",
    cwd=Path("/path/to/treeviz"),
)

Use auto_crop=True to trim exported SVG/PNG/PDF artifacts to the visible tree content. The optional metrics JSON records the content box, crop box, whitespace margins, fill ratios, and crop warnings for automated checks.

Metadata rows must contain one key column whose values exactly match tree leaf labels. Pass it as row_key_column. Values may be strings, numbers, booleans, None, or empty strings; missing values are handled explicitly. Supported track kinds are color_strip, gradient, heatmap, bar, text, and binary_dots.

The source checkout includes a runnable script:

PYTHONPATH=packages/python/src pixi run -e py python packages/python/examples/plot_treeviz_examples.py --out /tmp/treeviz-python-plots

Full documentation is in docs/PYTHON.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

treeviz_phylo-0.1.0.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

treeviz_phylo-0.1.0-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file treeviz_phylo-0.1.0.tar.gz.

File metadata

  • Download URL: treeviz_phylo-0.1.0.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for treeviz_phylo-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1c33521e8c3aac21d46e5fd6cd492a00244a7fc6cfd98c441392057bd319990b
MD5 b34f21db4808dcd0f2223ff5edd0f4b5
BLAKE2b-256 c01abc61a2115fe3ccba9271f6c6c0bdd01d51237468cf8a9635b9c41bde87f3

See more details on using hashes here.

File details

Details for the file treeviz_phylo-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: treeviz_phylo-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for treeviz_phylo-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 059f9e4438fb03f662b373b58a4e37a69c5ae7cf55d9cbdd88dd912e2d0a90b4
MD5 f2dd9cde4fcba55a18e4f1fbfcb77250
BLAKE2b-256 a55851783b8da0d83fc88f6aa117d78fde370ddeab337b3cdd7cff1b911f0310

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page