Skip to content

Commit

Permalink
Use key to force update when dynamic properties are changed
Browse files Browse the repository at this point in the history
  • Loading branch information
blackary committed Jan 2, 2025
1 parent 670382f commit f8cc418
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
34 changes: 17 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.3
hooks:
- id: ruff
args:
- --fix
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.13.0"
hooks:
- id: mypy
args:
- --ignore-missing-imports
- --follow-imports=silent
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.3
hooks:
- id: ruff
args:
- --fix
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.13.0"
hooks:
- id: mypy
args:
- --ignore-missing-imports
- --follow-imports=silent

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0 # Use the ref you want to point at
hooks:
- id: trailing-whitespace
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0 # Use the ref you want to point at
hooks:
- id: trailing-whitespace
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name="streamlit-keyup",
version="0.2.4",
version="0.3.0",
author="Zachary Blackwood",
author_email="[email protected]",
description="Text input that renders on keyup",
Expand Down
23 changes: 20 additions & 3 deletions src/st_keyup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,24 @@ def st_keyup(
function
"""

if key is None:
key = "st_keyup_" + label
key_parts = [
key,
disabled,
label_visibility,
debounce,
max_chars,
type,
placeholder,
]

computed_key = "st_keyup_" + "__".join(
str(part) for part in key_parts if part is not None
)

component_value = _component_func(
label=label,
value=value,
key=key,
key=computed_key,
debounce=debounce,
default=value,
max_chars=max_chars,
Expand All @@ -55,6 +66,12 @@ def st_keyup(
label_visibility=label_visibility,
)

if key is not None:
st.session_state[key] = component_value

if key is None:
key = "st_keyup_" + label

if on_change is not None:
if "__previous_values__" not in st.session_state:
st.session_state["__previous_values__"] = {}
Expand Down
6 changes: 5 additions & 1 deletion streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def get_cities() -> pd.DataFrame:

debounce = st.checkbox("Add 0.5s debounce?")

name = st_keyup("Enter city name", debounce=500 if debounce else None)
disabled = st.checkbox("Disable input?")

name = st_keyup(
"Enter city name", debounce=500 if debounce else None, disabled=disabled
)

if name:
filtered = cities[cities.City.str.lower().str.contains(name.lower(), na=False)]
Expand Down

0 comments on commit f8cc418

Please sign in to comment.