Datetime slider

Dear,

I would like to create a slider where you can choose a period between two dates. First of all, from what I read on the website, a slider is only available to display integers? Is this correct?

Second, there seems to be a bug with the st.date_input API. If I type this “st.sidebar.date_input(‘start date’, datetime.date(2011,1,1))”, the code returns an error: descriptor ‘date’ requires a ‘datetime.datetime’ object but received a ‘int’.

Can someone help me out? :slight_smile:

Thanks!

Dear @madbaets! Welcome to the community! :blush:

I’m looking into this now and will get back to you!

Dear Adrien. Thanks for getting back to me so quickly. Appreciate your help! Looking forward to your solution :slight_smile:

Hi @madbaets:

I wasn’t able to reproduce your issue on my computer. This worked for me on both Python 2.7.10 and Python 3.6.3:

import streamlit as st
import datetime

date = st.sidebar.date_input('start date', datetime.date(2011,1,1))
st.write(date)

My suspicion is that this has something to do with your Python version. In order for us to help you further, could you please fill out a bug report. You don’t need to write too much. You can just refer to this post. :slight_smile: The most important part of the bug report is the bottom part about which version of everything you’re using.

Thanks for using Streamlit! :heart:

Hi Adrien!

I got it to work. I made a mistake when I imported the module datetime.
What I did was the following: From datetime import date, datetime.
=> When you import date as well, python is confused and messes up.

The answer thus lies into importing only the module datetime as a whole.

However, I’m still not able to make a slider with a range of dates, since the widget allows us to only use integers or floats as values for the slider. Do you happen to know if a datetime slider is on the roadmap?

Thanks!

Hi @madbaets.

I’m happy you got it to work! :slight_smile:

Unfortunately, there’s currently no “date range widget,” but please feel free to submit a feature request. The more detail you can provide the better!

In the meantime, may I please suggest this workaround:

import streamlit as st
import datetime

today = datetime.date.today()
tomorrow = today + datetime.timedelta(days=1)
start_date = st.date_input('Start date', today)
end_date = st.date_input('End date', tomorrow)
if start_date < end_date:
    st.success('Start date: `%s`\n\nEnd date:`%s`' % (start_date, end_date))
else:
    st.error('Error: End date must fall after start date.')


Hi All,
I had started to explore Streamlit but these date pickers are there but how to choose date range and get the data fetched from the data-frame.

Hi @Anas_Riz -

From Datetime slider above, Adrien shows assigning the values start_date and end_date to variables. If you want to limit the data fetched from a dataframe, you can pass start_date and end_date as part of a pandas indexing operation. This StackOverflow example covers how to accomplish various date and datetime operations:

Best,
Randy

1 Like