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’.
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. The most important part of the bug report is the bottom part about which version of everything you’re using.
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?
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:
Hey, @Adrien_Treuille thanks for the workaround.
When I am changing the start date, the end date should also change, right?
But that is not the case here, when I start streamlit for the first time then it’s working as expected but when I am making the changes in the start date from the UI, the end date is not changing.
is there a way to reformat the calendar by weekdays? I wish it would be start with a monday on the first column (left) and end up with a lazy sunday on the last column (right)
import streamlit as st
import datetime as dt
import pandas as pd
from dateutil.relativedelta import relativedelta # to add days or years
## Range selector
cols1,_ = st.beta_columns((1,2)) # To make it narrower
format = 'MMM DD, YYYY' # format output
start_date = dt.date(year=2021,month=1,day=1)-relativedelta(years=2) # I need some range in the past
end_date = dt.datetime.now().date()-relativedelta(years=2)
max_days = end_date-start_date
slider = cols1.slider('Select date', min_value=start_date, value=end_date ,max_value=end_date, format=format)
## Sanity check
st.table(pd.DataFrame([[start_date, slider, end_date]],
columns=['start',
'selected',
'end'],
index=['date']))
df = ld.load_data_projet()
start_date = st.date_input('Date de début :')
end_date = st.date_input('Date de fin :')
if start_date < end_date:
pass
else:
st.error('Error: Date de fin doit être choisi après la dete de début.')
mask = (df['CREATEDDATE'] > start_date) & (df['CREATEDDATE'] <= end_date)
df = df.loc[mask]
# And display the result!
st.dataframe(df)
start_date, end_date = st.date_input('start date - end date :', [])
if start_date < end_date:
pass
else:
st.error('Error: End date must fall after start date.')
mask = (df['CREATEDDATE'] > start_date) & (df['CREATEDDATE'] <= end_date)
An i get the dates input (start date and end date) in the same select box :
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.