Add state versioning and safety guards to DynamicFilters classes to prevent stale session issues#23
Open
leiofrivia wants to merge 1 commit intoarsentievalex:mainfrom
Open
Add state versioning and safety guards to DynamicFilters classes to prevent stale session issues#23leiofrivia wants to merge 1 commit intoarsentievalex:mainfrom
leiofrivia wants to merge 1 commit intoarsentievalex:mainfrom
Conversation
…revent stale session issues This commit introduces a lightweight versioning mechanism for all DynamicFilters classes (DynamicFilters, DynamicFiltersHierarchical, and DynamicFiltersWithGroupby) to address persistent session state conflicts and KeyErrors when the underlying DataFrame or filter set changes between reruns. Problem Previously, when the user changed filter configurations or when the DataFrame columns changed (for example, switching to a different study or dataset), Streamlit’s session state retained old widget keys. This caused: KeyErrors such as “KeyError: 'Beginning Slot Penalty'” Stale filter values lingering from previous runs Widgets referencing non-existent columns Unpredictable behavior when filters were removed or renamed Solution This patch introduces a short hash-based version token derived from the current filter list. All session state keys and widget keys are now versioned using this token, ensuring that each change in filters results in a clean, isolated namespace. Key enhancements: Added _make_token() helper to compute a short hash token from filter names. Namespaced filters_name and aggregation_name (for GroupBy variant) with the token. Added _purge_old_namespaces() to safely remove previous state namespaces. Added _rebuild_namespace_if_mismatch() to auto-repair session state when filters differ. Added _widget_key() and _agg_key() to provide stable, versioned widget identifiers. Added guards to skip missing DataFrame columns gracefully (if key in df.columns). Ensured consistent handling across DynamicFilters, DynamicFiltersHierarchical, and DynamicFiltersWithGroupby. Impact Backward compatible: no change to public API, signatures, or behavior. Prevents stale state and KeyErrors when switching datasets or filter sets. Streamlit widgets now refresh correctly on filter schema changes. This change improves reliability and user experience in Streamlit apps that dynamically change available filters or datasets.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit introduces a lightweight versioning mechanism for all DynamicFilters classes
(DynamicFilters, DynamicFiltersHierarchical, and DynamicFiltersWithGroupby) to address
persistent session state conflicts and KeyErrors when the underlying DataFrame or
filter set changes between reruns.
Problem
Previously, when the user changed filter configurations or when the DataFrame columns
changed (for example, switching to a different study or dataset), Streamlit’s session
state retained old widget keys. This caused:
Solution
This patch introduces a short hash-based version token derived from the current filter list.
All session state keys and widget keys are now versioned using this token, ensuring
that each change in filters results in a clean, isolated namespace.
Key enhancements:
_make_token()helper to compute a short hash token from filter names.filters_nameandaggregation_name(for GroupBy variant) with the token._purge_old_namespaces()to safely remove previous state namespaces._rebuild_namespace_if_mismatch()to auto-repair session state when filters differ._widget_key()and_agg_key()to provide stable, versioned widget identifiers.if key in df.columns).and DynamicFiltersWithGroupby.
Impact
This change improves reliability and user experience in Streamlit apps that dynamically
change available filters or datasets.