Skip to content

[BUG] Checkbox counts are incremented on startup #102

@robino16

Description

@robino16

Describe the bug
The counts for all checkboxes are incremented on startup without user interaction. Counts for checkboxes always start at one instead of zero and are incremented without interaction from the user.

Expected behavior
I would expect the counts for all widgets to start at zero the first time the app is run. Once a user checks or unchecks a checkbox, the count for that checkbox should increment by one. Checkboxes that are untouched should remain at count 0.

To Reproduce

  1. Make a Streamlit app with at least one checkbox (and which stores the counts in, e.g., a JSON file).
  2. Run the app.
  3. See that the count for all checboxes start at 1 instead of 0 by examining the JSON file.
  4. Stop and re-start the app.
  5. See that the count for all checkboxes have been incremented by 1 without user interaction.

Software Vesions

  • streamlit_analytics2 : v0.7.7

Additional context
The original streamlit-analytics repo by jrieke also has this bug.

Suggested fix:
We should not increment the count the first time the script is run, only after a user has interacted with the checkbox. The following code attempts to solve this issue:

def _wrap_checkbox(func):
    """
    Wrap st.checkbox.
    """

    def new_func(label, *args, **kwargs):
        checked = func(label, *args, **kwargs)
        label = replace_empty(label)
        if label not in counts["widgets"]:
            counts["widgets"][label] = 0

        # we only increment the count if the user has interacted with the checbox
        if label in st.session_state.state_dict and checked != st.session_state.state_dict[label]:
            counts["widgets"][label] += 1

        st.session_state.state_dict[label] = checked
        return checked

    return new_func

The code and comments here are just suggestions.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions