Skip to content

Commit

Permalink
Merge pull request #59 from posit-dev/feat-shiny
Browse files Browse the repository at this point in the history
feat: shiny output and renderer for GT
  • Loading branch information
machow authored Dec 7, 2023
2 parents d3576be + f3fdff0 commit b30c60b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion great_tables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ def __getattr__(k: str):
f"\n\nfrom great_tables.data import {k}"
)
else:
raise ImportError(f"cannot import name {k} from great_tables ({__file__})")
raise AttributeError(f"cannot get attribute {k} from great_tables ({__file__})")
63 changes: 63 additions & 0 deletions great_tables/shiny.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from __future__ import annotations

__all__ = (
"output_gt",
"render_gt",
)

from great_tables import GT
from htmltools import Tag, div, HTML

try:
from shiny.render.transformer import (
output_transformer,
TransformerMetadata,
ValueFn,
resolve_value_fn,
)
from shiny._namespaces import resolve_id
except ImportError:
raise ImportError(
"The great_tables.shiny module requires the shiny package to be installed."
" Please install it with this command:"
"\n\n pip install shiny"
)

from typing import TYPE_CHECKING, overload

if TYPE_CHECKING:
from shiny.session._utils import RenderedDeps


def output_gt(id: str, placeholder: bool = False) -> Tag:
"""Output UI for a great_tables table."""
return div({"class": "shiny-html-output"}, id=resolve_id(id))


@output_transformer(default_ui=output_gt)
async def GtTransformer(_meta: TransformerMetadata, _fn: ValueFn[GT | None]) -> RenderedDeps | None:
value = await resolve_value_fn(_fn)
if value is None:
return None
elif isinstance(value, GT):
return _meta.session._process_ui(HTML(value._repr_html_()))

raise TypeError(f"Expected a great_tables.GT object, got {type(value)}")


@overload
def render_gt() -> GtTransformer.OutputRendererDecorator:
...


@overload
def render_gt(_fn: GtTransformer.ValueFn) -> GtTransformer.OutputRenderer:
...


def render_gt(
_fn: GtTransformer.ValueFn | None = None,
) -> GtTransformer.OutputRenderer | GtTransformer.OutputRendererDecorator:
"""Render a great_tables table."""

return GtTransformer(_fn)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dev = [
"pytest>=3",
"pytest-cov",
"siuba",
"shiny",
"syrupy"
]

Expand Down
5 changes: 5 additions & 0 deletions tests/test_shiny.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from great_tables.shiny import render_gt, output_gt

# TODO: this module currently just imports the shiny submodule
# so the tests will fail if it's not importable. We should add
# tests for render_gt, and output_gt

0 comments on commit b30c60b

Please sign in to comment.