main page allowing user to upload a csv timeseries data of sensor readings and filter out times based on the users input
Drop files here
or click to upload
import streamlit as st
import pandas as pd
import numpy as np
from datetime import datetime, time
st.title("Timeseries Data Filter")
# File upload
uploaded_file = st.file_uploader("Upload your CSV file", type="csv")
if uploaded_file is not None:
try:
# Read the CSV file
df = pd.read_csv(uploaded_file)
# Check if there's a timestamp column
timestamp_cols = df.select_dtypes(include=['datetime64']).columns
if len(timestamp_cols) == 0:
# Try to identify potential timestamp columns
potential_timestamp_cols = [col for col in df.columns if 'time' in col.lower() or 'date' in col.lower()]
if potential_timestamp_cols:
timestamp_col = st.selectbox(
"Select the timestamp column:",
potential_timestamp_cols
)
else:
timestamp_col = st.selectbox(
"Select the timestamp column:",
df.columns
)
# Convert selected column to datetime
df[timestamp_col] = pd.to_datetime(df[timestamp_col])
else:
timestamp_col = timestamp_cols[0]
# Show basic information about the dataset
st.write("Dataset Info:")
st.write(f"- Total records: {len(df)}")
st.write(f"- Date range: {df[timestamp_col].min()} to {df[timestamp_col].max()}")
# Time range filter
st.subheader("Filter by Time of Day")
col1, col2 = st.columns(2)
with col1:
start_time = st.time_input('Start time', time(0, 0))
with col2:
end_time = st.time_input('End time', time(23, 59))
# Apply time filter
mask = df[timestamp_col].dt.time.between(start_time, end_time)
filtered_df = df[mask]
# Show results
st.subheader("Filtered Dataset")
st.write(f"Records after filtering: {len(filtered_df)}")
# Display the filtered data
st.dataframe(filtered_df)
# Download filtered data
if st.button("Download Filtered Data"):
csv = filtered_df.to_csv(index=False)
st.download_button(
label="Click to Download",
data=csv,
file_name="filtered_data.csv",
mime="text/csv"
)
except Exception as e:
st.error(f"Error processing the file: {str(e)}")
else:
st.info("Please upload a CSV file to begin. The file should contain a timestamp column and sensor readings.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?