Skip to content

Comments

Add state versioning and safety guards to DynamicFilters classes to prevent stale session issues#23

Open
leiofrivia wants to merge 1 commit intoarsentievalex:mainfrom
leiofrivia:patch-1
Open

Add state versioning and safety guards to DynamicFilters classes to prevent stale session issues#23
leiofrivia wants to merge 1 commit intoarsentievalex:mainfrom
leiofrivia:patch-1

Conversation

@leiofrivia
Copy link

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.
  • Fully compatible with existing applications using version 0.1.9 of the package.

This change improves reliability and user experience in Streamlit apps that dynamically
change available filters or datasets.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant