Date w/ Timestamp Slider or user input question

Hi all:

I have a file that looks like the following:

Previously, tried creating a slider based on datetime, but could not get the slider to work down to the millisecond. My work around was to abstract the timestamp to a line number and provide a slider so they can filter lines down to the approximate timestamps they are looking for within the logcat.

dtSelection=st.sidebar.slider("Filter Line Number",0,1,(minLine,maxLine))
st.dataframe(df[(df.priority.isin(prioritySelection)) & (df.lineNum.between(dtSelection[0], dtSelection[1])) & ( & (df.message.str.contains(keyword))])

Users do not really like this experience–which I agree with from their perspective.

Is there a way to have the slider use timestamps rather than line numbers?
Is there a way to use a user input field to search for a timestamp range instead?

Hi @rzw3ch

I haven’t been able to get st.slider to comprehend dates. Instead, I have used st.date_input. Here’s a solution that might work for you:

import streamlit as st
import pandas as pd
from datetime import datetime

# data.csv has columns ['date_time', 'value']
# Example:
#   date_time,value
#   2015-05-15 10:05:03,95.93390214
#   2015-05-15 10:05:43,81.03359351
#   2015-05-15 10:05:47,71.66595487
#   2015-05-15 10:05:12,76.99855579
#       :
#       :  
df = pd.read_csv('data.csv')
df['date_time'] = pd.to_datetime(df['date_time'])
start_dt = st.sidebar.date_input('Start date', value=df['date_time'].min())
end_dt = st.sidebar.date_input('End date', value=df['date_time'].max())
if start_dt <= end_dt:
    df = df[df['date_time'] > datetime(start_dt.year, start_dt.month,]
    df = df[df['date_time'] < datetime(end_dt.year, end_dt.month,]
    st.error('Start date must be > End date')


I havent tested this but will it work with timestamps down to milliseconds?

No - the date control is calendar date resolution only. You’d have to add one or more number input widgets to capture the sub-day time resolution you want, and combine all values to filter your data set.

How can I submit a feature request to allow for the inclusion of timestamp values? I will try to encode the timestamp as an integer next and see if that would work.

EDIT: Here is my work around for now:

def renderDf(df):    
    #Set up the tag filter for log message
    keyword = streamlitTags.st_tags_sidebar(label='Search Log Message',text='Add tags to Search Log Messages',suggestions=['five'],maxtags = -1)
    keyword = '|'.join(keyword)
    #Instantiate the priorities filter
    priorities = df['priority'].unique().tolist()
    prioritySelection = st.sidebar.multiselect('Select Log Line Priority',priorities,default=priorities)
    #Instantiate the proxy filter since Streamlit is not super flexible with filtering
    dtSelection=st.sidebar.slider("Filter Line Number",0,1,(minLine,maxLine))
    #Set up the tag filter for apps Tags
    apps = df['app'].unique().tolist()
    keywordTags = streamlitTags.st_tags_sidebar(label='Search Apps',text='Add tags to Search Apps',suggestions=apps,maxtags = -1)
    keywordT = '|'.join(keywordTags)
    #create the dataframe and nest all of the filters together so they work 'somewhat' in tandem. Streamlit doesnt have a great way of chaining filters like a PowerBI or Tableau
    df = df[(df.priority.isin(prioritySelection)) & (df.lineNum.between(dtSelection[0], dtSelection[1])) & ( & (df.message.str.contains(keyword))]
    startDate = min(df.index)
    enDate = max(df.index)'Start: **%s** End: **%s**' % (startDate,enDate))
    st.markdown('This script has already written the parsed log files to a csv called "parsedLogs.csv" in the current working directory')


it is not a perfect solution by any means, but it at least gives a better visual indicator of what the line number equates to a timestamp range