String formatting for slider - example

Hi,

I am looking to use a slider for an app to provide lower and upper dates on a parameter. Since st.slider only supports floats, I first convert my dates to timestamp, with the idea of using the format parameter in the st.slider to show something understandable to the user.

However, I cannot seem to find an example of how this is done. For now, I have the following code:

st.slider("Plotting window", 
    value=(min(list_of_days).timestamp(), max(list_of_days).timestamp()), 
    format='%s'.format(datetime.utcfromtimestamp().strftime('%d/%m/%y')))

where list_of_days is, well, a list of days. This does not run since the utcfromtimestamp() needs an input parameter, i.e. the value that is to be converted. How can I get that?

I think it might be good to include an example in the API reference guide (or even better, enable support for datetime? :slight_smile: ).

Thanks
Richard

Hi Richard,

Would something like this work for you?

import streamlit as st
start_date = st.date_input(“Start Date”)
end_date = st.date_input(“End Date”)

Thanks!

Hi Negmat,

thanks for your interest!

What you suggested works, however, I would prefer:

Thanks
Richard

Hi @RichardOberdieck,

This is not currently possible as the format parameter accepts the following

        format : str or None
            Printf/Python format string controlling how the interface should
            display numbers. This does not impact the return value.

Feel free to file a github issue for this functionality!

Can we please get an example of a st.slider function with the format argument filled in with something?

Hey @RichardOberdieck ,

I’ve created a new feature request here .

Hey @JoshZastrow!,

Here you have an small snippet:

age = st.slider('How old are you?', 0, 130, 25, format="%d years old")
st.write("I'm ", age, 'years old')

A possible solution is not ideal, but it can be useful.

import streamlit as st
from datetime import datetime, timedelta
from pandas import DataFrame


if st.checkbox('Hide number of days'):
    format = ''
else:
    format = '%d days'
start_date = datetime(2020, 1, 1)
max_days = 30
end_date = (start_date + timedelta(days=max_days)).date()
slider = st.slider('Select date', 0, max_days, format=format)
date = (start_date + timedelta(days=slider)).date()

st.table(DataFrame([[start_date, date, end_date]],
                      columns=['start',
                               'selected',
                               'end'],
                      index=['date']))

Thanks for putting this together. I think I’ll stick with my current solution for now though: do two drop down menus, one for start and one for the end date.