-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from posit-dev/feat-shiny
feat: shiny output and renderer for GT
- Loading branch information
Showing
4 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ dev = [ | |
"pytest>=3", | ||
"pytest-cov", | ||
"siuba", | ||
"shiny", | ||
"syrupy" | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |