Skip to content

Commit 22f0223

Browse files
committed
fix: Introduce a more ergonomic API for express.ui.insert_accordion_panel()
1 parent 5653bdb commit 22f0223

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed

shiny/express/ui/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
input_text,
7575
input_text_area,
7676
panel_title,
77-
insert_accordion_panel,
7877
remove_accordion_panel,
7978
update_accordion,
8079
update_accordion_panel,
@@ -161,6 +160,10 @@
161160
hold,
162161
)
163162

163+
from ._insert import (
164+
insert_accordion_panel,
165+
)
166+
164167
__all__ = (
165168
# Imports from htmltools
166169
"TagList",

shiny/express/ui/_insert.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""
2+
Shims for `ui.insert_*()`, `ui.update_*()`, etc. functions that lead to a more ergonomic
3+
Express API.
4+
5+
These functions tend to have one issue in common: if they were re-exported verbatim from
6+
Core to Express, they would want to take RecallContextManager(s) as input, which leads
7+
to a somewhat awkward API. That's because, you'd have to know to use something like
8+
@ui.hold() pass the UI as a value without displaying it.
9+
"""
10+
11+
from typing import Literal, Optional
12+
13+
from htmltools import TagAttrs, TagChild
14+
15+
from ..._docstring import add_example
16+
from ...session import Session
17+
from ...types import MISSING, MISSING_TYPE
18+
19+
20+
@add_example()
21+
def insert_accordion_panel(
22+
id: str,
23+
panel_title: str,
24+
*panel_contents: TagChild | TagAttrs,
25+
panel_value: str | MISSING_TYPE | None = MISSING,
26+
panel_icon: TagChild = None,
27+
target: Optional[str] = None,
28+
position: Literal["after", "before"] = "after",
29+
session: Optional[Session] = None,
30+
) -> None:
31+
"""
32+
Insert an accordion panel into an existing accordion.
33+
34+
Parameters
35+
----------
36+
id
37+
A string that matches an existing :func:`~shiny.ui.express.accordion`'s `id`.
38+
panel_title
39+
The title to appear in the panel header.
40+
panel_contents
41+
UI elements for the panel's body. Can also be a dict of tag attributes for the
42+
body's HTML container.
43+
panel_value
44+
A character string that uniquely identifies this panel. If `MISSING`, the
45+
`title` will be used.
46+
panel_icon
47+
A :class:`~htmltools.TagChild` which is positioned just before the `title`.
48+
target
49+
The `value` of an existing panel to insert next to.
50+
position
51+
Should `panel` be added before or after the target? When `target=None`,
52+
`"after"` will append after the last panel and `"before"` will prepend before
53+
the first panel.
54+
session
55+
A Shiny session object (the default should almost always be used).
56+
57+
References
58+
----------
59+
[Bootstrap Accordion](https://getbootstrap.com/docs/5.3/components/accordion/)
60+
61+
See Also
62+
--------
63+
* :func:`~shiny.ui.express.accordion`
64+
* :func:`~shiny.ui.express.accordion_panel`
65+
* :func:`~shiny.ui.express.update_accordion`
66+
"""
67+
68+
from ...ui import accordion_panel, insert_accordion_panel
69+
70+
panel = accordion_panel(
71+
panel_title, *panel_contents, value=panel_value, icon=panel_icon
72+
)
73+
74+
insert_accordion_panel(
75+
id=id,
76+
panel=panel,
77+
target=target,
78+
position=position,
79+
session=session,
80+
)

shiny/ui/_accordion.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,17 @@ def accordion_panel(
315315
Parameters
316316
----------
317317
title
318-
A title to appear in the :func:`~shiny.ui.accordion_panel`'s header.
318+
A title to appear in the panel's header.
319319
*args
320-
Contents to the accordion panel body. Or tag attributes that are supplied to the
321-
returned :class:`~htmltools.Tag` object.
320+
UI elements for the panel's body. Can also be a dict of tag attributes for the
321+
body's HTML container.
322322
value
323323
A character string that uniquely identifies this panel. If `MISSING`, the
324324
`title` will be used.
325325
icon
326-
A :class:`~htmltools.Tag` which is positioned just before the `title`.
326+
A :class:`~htmltools.TagChild` which is positioned just before the `title`.
327327
**kwargs
328-
Tag attributes to the `accordion-body` div Tag.
328+
Tag attributes for the body's HTML container.
329329
330330
Returns
331331
-------

0 commit comments

Comments
 (0)