create a streamlit interface for selecting json files in the /mnt/uat-data/data/refactor/workflow/state directory on the left side panel and displaying their content in the right panel. Sample json file name is US_R1K_sod_processor_ip-172-31-25-209-ec2-internal_20250407.json and consists of multiple parts delimited by underscore character. Provide dropdown selectors on top of the interface for selecting parts of the available filenames with distinct values taken from all available filenames in the directory. Display json content in the right side panel as collapsible view
Drop files here
or click to upload
import streamlit as st
import json
from pathlib import Path
import pandas as pd
# Function to parse filename into parts
def parse_filename(filename):
parts = filename.replace('.json', '').split('_')
# Expected format: REGION_INDEX_TYPE_PROCESSOR_HOST_DATE
if len(parts) >= 6:
return {
'region': parts[0],
'index': parts[1],
'type': parts[2],
'processor': parts[3],
'host': parts[4],
'date': parts[5]
}
return None
# Set page config for wide layout
st.set_page_config(layout='wide')
# Directory path
data_dir = Path('/mnt/uat-data/data/refactor/workflow/state')
# Get all JSON files
json_files = list(data_dir.glob('*.json'))
file_data = []
# Parse all filenames
for file in json_files:
parsed = parse_filename(file.name)
if parsed:
parsed['filename'] = file.name
file_data.append(parsed)
# Convert to DataFrame for easier filtering
df = pd.DataFrame(file_data)
# Create filters in the sidebar
st.sidebar.header('File Filters')
# Create dropdown for each part of the filename
filters = {}
for column in df.columns:
if column != 'filename':
unique_values = sorted(df[column].unique())
filters[column] = st.sidebar.multiselect(
f'Select {column}:',
unique_values,
default=[]
)
# Apply filters
filtered_df = df.copy()
for column, selected_values in filters.items():
if selected_values:
filtered_df = filtered_df[filtered_df[column].isin(selected_values)]
# Display filtered files in the sidebar
st.sidebar.header('Available Files')
selected_file = st.sidebar.selectbox(
'Select a file to view:',
filtered_df['filename'].tolist()
)
# Main content area
st.title('JSON File Explorer')
if selected_file:
try:
# Read and parse JSON file
with open(data_dir / selected_file) as f:
data = json.load(f)
# Display JSON content with collapsible view
st.json(data)
except Exception as e:
st.error(f'Error reading file: {str(e)}')
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?