Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add keyboard hint #13

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
Loading