posit-dev / posit-dev/py-shiny

[Bug]: update_checkbox_group requires different selected than input_checkbox_group

Open
#2,272 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

ai-triage:done bug needs-triage Priority: Low
Dominant language
Python
Stars
1.8k
Forks
135
Avg merge
2d 18h
Merged PRs (30d)
21

Description

Component

UI (ui.*)

Severity

P3 - Low (minor inconvenience)

Shiny Version

1.61

Python Version

3.14.2

Minimal Reproducible Example
from shiny import App, reactive, ui

DICT = {"Foo": {0: "a", 1: "b", 2: "c"}, "Bar": {0: "alpha", 1: "beta", 2: "gamma"}}

problem_h2 = """
Bug: update_checkbox_group() requires different 'selected' setup for choices 
with integer keys than input_checkbox_group()
"""

problem_str = """
input_checkbox_group accepts selected keys as ints, but update_checkbox_group does not.
The intention is to have all the options checked by default. There is no problem when 
initiating the checkbox group, but there is a problem when it updates.
\n Bonus idea:\n
It would be great if you could just pass a bool as an argument to check all boxes passed.
"""

checkbox_init = {
    "label": "Select multiple options:",
    "choices": DICT["Foo"],
    "selected": [k for k in DICT["Foo"].keys()],
}

app_ui = ui.page_fluid(
    ui.h2(problem_h2),
    ui.p(problem_str),
    ui.input_radio_buttons(
        "select_key", "Select a dictionary:", list(DICT.keys()), selected="Foo"
    ),
    ui.card(
        ui.card_header("Bugged checkbox_group"),
        ui.p(
            "After selecting a dict above, the options will be unchecked. They should be checked."
        ),
        ui.input_checkbox_group("check_group_bad", **checkbox_init),
    ),
    ui.card(
        ui.card_header("This checkbox_group works properly"),
        ui.p("But it required an extra step of converting keys to strings"),
        ui.input_checkbox_group("check_group_good", **checkbox_init),
    ),
)


def server(input, output, session):
    @reactive.effect
    @reactive.event(input.select_key, ignore_init=True)
    def _():
        choices = DICT[input.select_key()]
        ui.update_checkbox_group(
            "check_group_bad", choices=choices, selected=[k for k in choices.keys()]
        )
        ui.update_checkbox_group(
            "check_group_good",
            choices=choices,
            selected=[str(k) for k in choices.keys()],
        )


app = App(app_ui, server)
Behavior

Current: update_checkbox_group() with selected=some_dict where the type of some_dict is dict[int, str], does not check the boxes. This is unexpected, because input_checkbox_group() accepts dictionaries with int keys and quietly converts them to strings.
Expected: update_checkbox_group() should quietly convert dictionary integers to string the same way input_checkbox_group() does for the selected parameter.
Current workaound: converting int keys from dict to string works just fine. e.g selected=[str(k) for k in choices_dict.keys()]

Error Messages (if any)
No errors, shiny silently doesn't check any boxes when none of the `selected=` hit.
Environment
OS: Windows 11
Browser: Chrome
Dependencies: None

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal reproducible example and trace the ui.input_checkbox_group and ui.update_checkbox_group entry points, comparing how each handles integer dictionary keys in selected. Done means update_checkbox_group checks the same boxes as input_checkbox_group without requiring callers to convert keys to strings.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.