Skip to content

Commit

Permalink
Merge pull request #13 from quantum-ernest/main
Browse files Browse the repository at this point in the history
feat: add keyboard hint
  • Loading branch information
adriangalilea authored Aug 16, 2024
2 parents 3f907b3 + d01842b commit 0673a56
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
37 changes: 33 additions & 4 deletions src/streamlit_shortcuts/streamlit_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit 0673a56

Please sign in to comment.