Skip to content

Conversation

@katosh
Copy link
Collaborator

@katosh katosh commented Dec 3, 2025

Add ObjectFormatter for full HTML repr customization

Builds on #2236

Summary

This PR introduces ObjectFormatter, a new extension point that allows packages with fundamentally different structures (like SpatialData) to fully customize the HTML representation while reusing anndata's styling and infrastructure.

Live Demo - See the SpatialData example (test case 20) alongside MuData and other examples.

Motivation

While SectionFormatter works well for packages that extend AnnData (like MuData with its .mod section), packages like SpatialData have completely different architectures:

  • No central X matrix
  • Different sections (images, labels, points, shapes, tables)
  • No obs_names/var_names (uses coordinate_systems instead)
  • Different version in footer

Changes

New Extension Point: ObjectFormatter

Added to src/anndata/_repr/registry.py:

@register_formatter
class SpatialDataFormatter(ObjectFormatter):
    def can_format(self, obj) -> bool:
        return type(obj).__name__ == "SpatialData"

    def get_header_config(self, obj, context) -> HeaderConfig:
        return HeaderConfig(
            type_name="SpatialData",
            shape_str=None,  # No central shape
            badges=[...],
        )

    def get_index_preview_config(self, obj, context) -> IndexPreviewConfig:
        # Show coordinate_systems instead of obs/var names
        return IndexPreviewConfig(items=[("coordinate_systems:", preview)])

    def should_render_x(self, obj) -> bool:
        return False  # No X matrix

    def get_footer_version(self, obj) -> str:
        return f"spatialdata v{version('spatialdata')}"

New Configuration Classes

  • HeaderConfig - Customize type name, shape, badges, file path, README icon
  • IndexPreviewConfig - Customize index preview section content

Updated generate_repr_html

The function now checks for registered ObjectFormatters and uses them to control:

  • Header rendering (via HeaderConfig)
  • Index preview (via IndexPreviewConfig)
  • Whether to render X section
  • Which standard sections to render
  • Footer version string

Visual Test: SpatialData Example

Added a comprehensive SpatialData test case demonstrating:

  • SpatialDataObjectFormatter for full customization
  • SectionFormatters for each element type (images, labels, points, shapes, tables)
  • Expandable nested AnnData in the tables section
  • Documentation links for each section

Extension Points Summary

Extension Point Use Case
TypeFormatter Custom visualization of specific types within sections
SectionFormatter Add new sections to AnnData-like objects
ObjectFormatter NEW - Complete control for non-AnnData objects

@settylab settylab deleted a comment from coderabbitai bot Dec 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants