diff --git a/README.md b/README.md index b38643e..63530c7 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ import streamlit_shortcuts def delete_callback(): st.write("DELETED!") -streamlit_shortcuts.button("delete", on_click=delete_callback, shortcut="Ctrl+Shift+X") +streamlit_shortcuts.button("delete", on_click=delete_callback, shortcut="Ctrl+Shift+X", hint=True) ``` ⭐ NEW in v0.1.5: Support for args and kwargs @@ -33,6 +33,7 @@ streamlit_shortcuts.button( "Delete", shortcut="Ctrl+Shift+X", on_click=delete_callback, + hint=True, args=(42,), kwargs={"user": "admin"}, type="primary" diff --git a/src/streamlit_shortcuts/streamlit_shortcuts.py b/src/streamlit_shortcuts/streamlit_shortcuts.py index 6eaadb8..3507208 100644 --- a/src/streamlit_shortcuts/streamlit_shortcuts.py +++ b/src/streamlit_shortcuts/streamlit_shortcuts.py @@ -41,7 +41,36 @@ def add_keyboard_shortcuts(key_combinations: Dict[str, str]): components.html(js_code, height=0, width=0) -def button(label: str, shortcut: str, on_click: Callable[..., None], *args, **kwargs): - shortcut = {shortcut: label} - add_keyboard_shortcuts(shortcut) - return st.button(label=label, on_click=on_click, args=args, **kwargs) +def button( + label: str, + shortcut: str, + on_click: Callable[..., None], + hint=False, + *args, + **kwargs, +): + """ + Creates a button with an optional keyboard shortcut and hint. + + Args: + label (str): The text to display on the button. + shortcut (str): The keyboard shortcut associated with the button. + on_click (Callable[..., None]): The function to call when the button is clicked. + hint (bool, optional): Whether to show the keyboard shortcut as a hint on the button label. Defaults to False. + *args: Additional positional arguments passed to the button function. + **kwargs: Additional keyword arguments passed to the button function. + + Returns: + The result of the Streamlit button function. + + Notes: + This function integrates with Streamlit's `st.button` to display a button with an optional hint showing the associated + keyboard shortcut. + """ + key_combination = {shortcut: label} + add_keyboard_shortcuts(key_combination) + if hint is False: + return st.button(label=label, on_click=on_click, args=args, **kwargs) + return st.button( + label=label + f"`{shortcut}`", on_click=on_click, args=args, **kwargs + )