Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@raethlein
Copy link
Collaborator

@raethlein raethlein commented Mar 24, 2025

Describe your changes

This PR fixes an issue in st.selectbox where when you type and e.g. remove some characters and then click outside of the selectbox, the backend value will stay the same but the frontend selectbox is empty.
Also, it does not trigger a rerun until a value is selected.

Dismiss behavior

Before:

old-selectbox-dismiss-behavior.mov

After:

new-selectbox-dismiss-behavior.mov

GitHub Issue Link (if applicable)

Testing Plan

  • Explanation of why no additional tests are needed
  • Unit Tests (JS and/or Python)
  • E2E Tests
  • Any manual testing needed?

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 24, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-10891/streamlit-1.43.2-py3-none-any.whl
🕹️ Preview app pr-10891.streamlit.app (☁️ Deploy here if not accessible)

@snyk-io
Copy link
Contributor

snyk-io bot commented Mar 24, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

@raethlein raethlein added security-assessment-completed Security assessment has been completed for PR change:bugfix PR contains bug fix implementation impact:users PR changes affect end users labels Mar 24, 2025
@raethlein raethlein force-pushed the feature/preserve-state-on-blur branch 2 times, most recently from 68a050e to d00be9d Compare March 25, 2025 11:32
@raethlein raethlein changed the title Preserve selectbox frontend state when clicking away Preserve selectbox frontend state when clicking away and don't rerun until selecting option Mar 25, 2025
@raethlein raethlein marked this pull request as ready for review March 25, 2025 11:36
@raethlein raethlein force-pushed the feature/preserve-state-on-blur branch from d00be9d to 9a3820c Compare March 25, 2025 11:44
@raethlein raethlein requested a review from lukasmasuch March 25, 2025 11:47
@raethlein raethlein force-pushed the feature/preserve-state-on-blur branch from 9a3820c to 4fe6944 Compare March 25, 2025 12:24
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is only setValue needed here and not onChange?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added following comment:

// We set the value so that BaseWeb updates the element's value while typing.
// We don't want to commit the change yet, so we don't call onChange.

Does this sound good to you?

Copy link
Collaborator

@lukasmasuch lukasmasuch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@raethlein raethlein force-pushed the feature/preserve-state-on-blur branch from 4fe6944 to 56ae3a4 Compare March 25, 2025 15:01
@raethlein raethlein force-pushed the feature/preserve-state-on-blur branch from d4224fc to ebb878f Compare March 25, 2025 15:34
@raethlein raethlein merged commit bf213b4 into develop Mar 25, 2025
33 checks passed
@raethlein raethlein deleted the feature/preserve-state-on-blur branch March 25, 2025 17:03
raethlein added a commit that referenced this pull request Mar 27, 2025
## Describe your changes

This PR adds a new functionality to the `st.selectbox` and
`st.multiselect` commands, which will allow to add new options
dynamically when the `accept_new_options` argument is passed as `True`.

These PRs were merged in context of this work:
#10865,
#10891

Underlying, we change an index-based option selection with a value-based
option selection. Previously, the frontend and backend used the indices
of the selected options, since the frontend and backend had the same set
of options available. Now, the frontend can have a new option being
added dynamically. This option is not available on the backend, so we
cannot use an index. We made the decision to switch the communication to
always use strings to keep it simple.
For that, following changes are made:
- the selected values are sent via `setStringValue` and
`setStringArrayValue` from the Frontend now
- the protos for Selectbox and Multiselect are updated:
- a new `accept_new_options` field to tell the frontend whether adding
an option dynamically should be enabled
- a new `raw_value` field that replaces the old `value` field. The
`value` field was of type `int32` / `int32[]`, respectively, and is now
of type `string` / `string[]`.
- the `value` field is marked as deprecated, but otherwise left in place
so that the code is backwards-compatible.
- the `st.selectbox` and `st.multiselect` have new overloaded versions
with return types `T | str` and `list[T | str]`, respectively. This
accounts for the fact that not only the generic type `T` based on the
passed options can be returned but also strings corresponding to
dynamically created options that are of type string.

The string-based message is ~6times bigger, but note that the
measurement is in bytes. So compared to the overall communication
pattern of Streamlit with reruns and messages based on every
interaction, this number should be neglectible.

The commands `st.selectbox` and `st.multiselect` continue to return the
generic type `T` for known options. However, if `accept_new_options` is
`True`, the returned value can also contain string(s) for dynamically
added options.
Additionally, the coercion of enums corresponding to the passed
`options` should not be relied on when options are added dynamically. So
far, the `maybe_coerce_enum` function returns early when not all items
in the passed list are of type enum. A dynamically added option is of
type `str`. So, the function will always return early when a dynamically
added option of type `str` is part of the list. We will see whether the
coercion of such mixed types will be requested & voted for in the
future.

## GitHub Issue Link (if applicable)

- Closes #5348
- Closes #5744

## Testing Plan

- Explanation of why no additional tests are needed
- Unit Tests (JS and/or Python)
  - Add unit tests for Serde classes
- Add unit tests to ensure that the `accept_new_options` proto field is
present
  - Update existing JS tests
- Add unit test to ensure correct value returned from
`getCurrStateFromProto`
- E2E Tests
  - Add new e2e tests to test adding a new option dynamically
  - Add new e2e test for ensuring preset session state value is used
- Any manual testing needed?

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants