Streamlit Toggle Switch
streamlit-toggle-switch is a Streamlit custom component that provides a customizable toggle switch widget, offering options for color and placement. The current version is 1.0.2. The library has not seen updates since late 2022, and its functionality has largely been superseded by a native Streamlit widget.
Common errors
-
AttributeError: module 'streamlit_toggle' has no attribute 'st_toggle_switch'
cause This error often occurs due to an incorrect import statement, typically trying to import `st_toggle_switch` directly from a module name that doesn't expose it, or using an incompatible Python version.fixEnsure you have installed `streamlit-toggle-switch` correctly. The correct import path is `from streamlit_toggle import st_toggle_switch`. Some users reported success with Python 3.8.5 or 3.8.10 when encountering similar issues. -
ModuleNotFoundError: No module named 'streamlit_toggle'
cause Despite installing `streamlit-toggle-switch`, the import for `streamlit_toggle` fails because the underlying package might not be correctly installed or recognized, or a different package name was used during installation.fixVerify installation with `pip show streamlit-toggle-switch`. Ensure your Python environment is correctly activated. If you previously installed `streamlit-toggle` (a different package), uninstall it and install `streamlit-toggle-switch`.
Warnings
- breaking This custom component is officially deprecated by its author in favor of Streamlit's native `st.toggle` widget, which offers similar functionality and is better integrated into the Streamlit ecosystem.
- gotcha The package name for installation is `streamlit-toggle-switch`, but the Python module to import is `streamlit_toggle`. This can lead to `ModuleNotFoundError` or `AttributeError` if the wrong module name is used in `import` statements.
- gotcha The `streamlit-toggle-switch` component, like many custom components, may not offer direct `on_change` callbacks for immediate state updates as robustly as native Streamlit widgets. This can lead to 'two-click' issues or require manual session state management.
Install
-
pip install streamlit-toggle-switch
Imports
- st_toggle_switch
from streamlit_toggle_switch import st_toggle_switch
from streamlit_toggle import st_toggle_switch
- st.toggle
import streamlit as st on = st.toggle('Activate feature')
Quickstart
import streamlit as st
from streamlit_toggle import st_toggle_switch
st.title('Custom Toggle Switch Demo')
# Example using the custom component
is_enabled = st_toggle_switch(
label='Enable Feature X',
key='feature_x_toggle',
default_value=False,
label_after=True,
inactive_color='#D3D3D3', # Optional
active_color='#11567f', # Optional
track_color='#29B5E8' # Optional
)
if is_enabled:
st.write('Feature X is enabled!')
else:
st.write('Feature X is disabled.')
st.subheader('Native Streamlit Toggle (Recommended)')
native_toggle_on = st.toggle('Activate native feature')
if native_toggle_on:
st.write('Native feature activated!')