How to create a Streamlit datetime slider.
Creating an interactive datetime Slider with Python Streamlit. As easy as pie
Yesterday, I was asked to configure a Streamlit datetime slider easily. Previously, it required some effort and tweaking of the
st.slider() function. However, now it is possible to achieve this smoothly, as the slider function works nearly flawlessly with dates.
import datetime import streamlit as st MIN_MAX_RANGE = (datetime.datetime(2022,1,1), datetime.datetime(2023,7,1)) PRE_SELECTED_DATES = (datetime.datetime(2023,1,1), datetime.datetime(2023,7,1)) selected_min, selected_ax = st.slider( "Datetime slider", value=PRE_SELECTED_DATES, min_value=MIN_MAX_RANGE, max_value=MIN_MAX_RANGE, )
For configure the slider steps when the user interacts with it, you could do as follows. Keep in mind that the intermediate values will not be selectable by default and may require an additional step. Additionally, there is an option to customize the date format for display.
Another functionality of the sliders is to include a helper, which we use quite often. However, please be aware that in certain versions of Streamlit, using the
help argument in the datetime slider may disrupt the display of the slider title.
You can see related Streamlit tips on TypeThePipe
- New Streamlit Chat. Conversational app with st.chat_message and st.chat_input
- Pydantic discriminated unions. Some examples to simplify data structures and ensure type safety
- Ursina vector rotation. The right and easy way to deal with this 3d Python module!
- Mock Celery task in Pytest
- Pydantic, Enums and IntEnums. A Story of Validation