Skip to content

Commit

Permalink
Merge pull request #7 from blackary/on-change
Browse files Browse the repository at this point in the history
Add on_change keyword
  • Loading branch information
blackary authored Sep 8, 2022
2 parents 3c8ba48 + 92eb145 commit 5b5cd3c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
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.1.7",
version="0.1.8",
author="Zachary Blackwood",
author_email="[email protected]",
description="Text input that renders on keyup",
Expand Down
51 changes: 49 additions & 2 deletions src/st_keyup/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Optional
from typing import Any, Callable, Dict, Optional, Tuple

import streamlit as st
import streamlit.components.v1 as components
Expand All @@ -13,6 +13,9 @@ def st_keyup(
value: str = "",
key: Optional[str] = None,
debounce: int = 0,
on_change: Optional[Callable] = None,
args: Optional[Tuple[Any, ...]] = None,
kwargs: Optional[Dict[str, Any]] = None,
):
"""
Generate a text input that renders on keyup, debouncing the input by the
Expand All @@ -23,7 +26,16 @@ def st_keyup(
when the user is typing. Since the input updating will cause the app to rerun,
if you are having performance issues, you should consider setting a debounce
value.
on_change is a callback function that will be called when the value changes.
args and kwargs are optional arguments which are passed to the on_change callback
function
"""

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

component_value = _component_func(
label=label,
value=value,
Expand All @@ -32,10 +44,26 @@ def st_keyup(
default=value,
)

if on_change is not None:
if "__previous_values__" not in st.session_state:
st.session_state["__previous_values__"] = {}

if component_value != st.session_state["__previous_values__"].get(key, value):
st.session_state["__previous_values__"][key] = component_value

if on_change:
if args is None:
args = ()
if kwargs is None:
kwargs = {}
on_change(*args, **kwargs)

return component_value


def main():
from datetime import datetime

st.write("## Default keyup input")
value = st_keyup("Enter a value")

Expand All @@ -47,15 +75,34 @@ def main():
st.write(value)

"## Keyup input with 500 millesecond debounce"
value = st_keyup("Enter a second value", debounce=500)
value = st_keyup("Enter a second value debounced", debounce=500)

st.write(value)

def on_change():
st.write("Value changed!", datetime.now())

def on_change2(*args, **kwargs):
st.write("Value changed!", args, kwargs)

"## Keyup input with on_change callback"
value = st_keyup("Enter a third value", on_change=on_change)

"## Keyup input with on_change callback and debounce"
value = st_keyup("Enter a third value...", on_change=on_change, debounce=1000)
st.write(value)

"## Keyup input with args"
value = st_keyup("Enter a fourth value...", on_change=on_change2, args=("Hello", "World"), kwargs={"foo": "bar"})
st.write(value)

"## Standard text input for comparison"
value = st.text_input("Enter a value")

st.write(value)

st.write(st.session_state)


if __name__ == "__main__":
main()

0 comments on commit 5b5cd3c

Please sign in to comment.