
Product
Introducing Rust Support in Socket
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
Add keyboard shortcuts to your Streamlit buttons! 🚀
[!NOTE] v1.2.0 - Multiple Shortcuts per Button
- Added support for multiple shortcuts per button/widget (#34)
- Use lists to assign multiple shortcuts:
shortcut_button("Save", ["ctrl+s", "cmd+s"])
- Works with both
shortcut_button
andadd_shortcuts
st.button
pattern# Before using native st.button:
if st.button("Save", type="primary", use_container_width=True):
save()
# After (just change function name & add shortcut):
if shortcut_button("Save", "ctrl+s", type="primary", use_container_width=True):
save()
# Multiple shortcuts for one button
if shortcut_button("Previous", ["arrowleft", "h"]):
go_previous()
name = st.text_input("Name", key="name_input")
# Add shortcuts to any widget with a key
add_shortcuts(
name_input="ctrl+n", # Focus name field
)
Try the live demo or check out the example code
pip install streamlit-shortcuts
shortcut_button(label, shortcut, **kwargs)
Drop-in replacement for st.button
with keyboard shortcut support.
Parameters:
label
(str): Button textshortcut
(str | list[str]): Single shortcut or list of shortcuts (e.g., "ctrl+s", ["ctrl+s", "cmd+s"])key
(str, optional): Unique key for the buttonhint
(bool, optional): Show shortcut hint in button label (default: True)**kwargs
: All other st.button parameters (help, on_click, args, type, icon, disabled, use_container_width)Returns: bool - True if clicked
add_shortcuts(**shortcuts)
Add keyboard shortcuts to any Streamlit widgets.
Parameters:
**shortcuts
: Keyword arguments where key is the widget's key and value is the shortcut (str | list[str])Example:
add_shortcuts(
save_btn="ctrl+s",
search_input="ctrl+f",
submit_form="ctrl+enter"
)
# Multiple shortcuts per widget
add_shortcuts(
prev_btn=["arrowleft", "a"],
next_btn=["arrowright", "d"]
)
clear_shortcuts()
(New in v1.1)Remove all keyboard shortcuts and event listeners. Useful for:
Example:
# Disable shortcuts conditionally
if not shortcuts_enabled:
clear_shortcuts()
# Clean up when switching pages
if st.sidebar.button("Go to Settings"):
clear_shortcuts()
st.switch_page("settings")
ctrl
, alt
, shift
, meta
(cmd on Mac)enter
, escape
, space
, tab
, delete
a
-z
0
-9
f1
-f12
arrowleft
, arrowright
, arrowup
, arrowdown
ctrl+s
- Ctrl + Sctrl+shift+d
- Ctrl + Shift + Dalt+enter
- Alt + Enterf1
- F1 keyctrl
works as expected (not cmd)meta
(Windows key on PC, Cmd on Mac)When using shortcut_button
inside columns with vertical_alignment
, the button may not align properly. This happens because the shortcut injection creates an invisible element that affects layout.
Workaround: Use st.button
with add_shortcuts
separately:
# ❌ Broken alignment
col1, col2 = st.columns([1, 1], vertical_alignment="bottom")
with col1:
shortcut_button("Save", "ctrl+s")
with col2:
st.selectbox("Options", ["A", "B", "C"])
# ✅ Correct alignment
col1, col2 = st.columns([1, 1], vertical_alignment="bottom")
with col1:
st.button("Save", key="save_btn")
with col2:
st.selectbox("Options", ["A", "B", "C"])
# Add shortcuts after columns
add_shortcuts(save_btn="ctrl+s")
# v0.x - Hijacked the API, confusing and unpythonic
button("Save", "ctrl+s", lambda: save()) # What is this? Not st.button!
# v1.0 - Respects Streamlit patterns, works like st.button
if shortcut_button("Save", "ctrl+s"): # Familiar pattern!
save()
# Or use native st.button unchanged
if st.button("Save", key="save_btn"):
save()
add_shortcuts(save_btn="ctrl+s")
If upgrading from v0.x:
# Old v0.x API
button("Click me", "ctrl+k", lambda: st.write("Hi"))
# New v1.0 API
if shortcut_button("Click me", "ctrl+k"):
st.write("Hi")
# Or use st.button + add_shortcuts
if st.button("Click me", key="btn"):
st.write("Hi")
add_shortcuts(btn="ctrl+k")
Built by the Streamlit community! 🎈
Special thanks to:
Inspired by Streamlit discussion #1291
FAQs
Streamlit keyboard shortcuts for your buttons.
We found that streamlit-shortcuts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.