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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import io
|
| 4 |
+
from utils import load_csv, get_file_info, filter_dataframe
|
| 5 |
+
|
| 6 |
+
# Set page config
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="CSV Viewer",
|
| 9 |
+
page_icon="π",
|
| 10 |
+
layout="wide"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Custom CSS for better styling
|
| 14 |
+
st.markdown("""
|
| 15 |
+
<style>
|
| 16 |
+
.main-header {
|
| 17 |
+
font-size: 2.5rem;
|
| 18 |
+
font-weight: bold;
|
| 19 |
+
color: #1f77b4;
|
| 20 |
+
text-align: center;
|
| 21 |
+
margin-bottom: 1rem;
|
| 22 |
+
}
|
| 23 |
+
.sub-header {
|
| 24 |
+
font-size: 1.2rem;
|
| 25 |
+
color: #666;
|
| 26 |
+
text-align: center;
|
| 27 |
+
margin-bottom: 2rem;
|
| 28 |
+
}
|
| 29 |
+
.file-info {
|
| 30 |
+
background-color: #f0f2f6;
|
| 31 |
+
padding: 1rem;
|
| 32 |
+
border-radius: 0.5rem;
|
| 33 |
+
margin-bottom: 1rem;
|
| 34 |
+
}
|
| 35 |
+
.anycoder-link {
|
| 36 |
+
position: fixed;
|
| 37 |
+
bottom: 10px;
|
| 38 |
+
right: 10px;
|
| 39 |
+
font-size: 0.8rem;
|
| 40 |
+
color: #666;
|
| 41 |
+
}
|
| 42 |
+
</style>
|
| 43 |
+
""", unsafe_allow_html=True)
|
| 44 |
+
|
| 45 |
+
def main():
|
| 46 |
+
# Header
|
| 47 |
+
st.markdown('<div class="main-header">CSV Viewer</div>', unsafe_allow_html=True)
|
| 48 |
+
st.markdown('<div class="sub-header">Upload and explore your CSV files with ease</div>', unsafe_allow_html=True)
|
| 49 |
+
|
| 50 |
+
# Anycoder attribution
|
| 51 |
+
st.markdown('<div class="anycoder-link">Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">anycoder</a></div>', unsafe_allow_html=True)
|
| 52 |
+
|
| 53 |
+
# File uploader
|
| 54 |
+
uploaded_file = st.file_uploader(
|
| 55 |
+
"Choose a CSV file",
|
| 56 |
+
type=["csv"],
|
| 57 |
+
help="Upload a CSV file to view and analyze its contents"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if uploaded_file is not None:
|
| 61 |
+
try:
|
| 62 |
+
# Load the CSV file
|
| 63 |
+
df = load_csv(uploaded_file)
|
| 64 |
+
|
| 65 |
+
# Display file info
|
| 66 |
+
file_info = get_file_info(df, uploaded_file.name)
|
| 67 |
+
st.markdown(f"""
|
| 68 |
+
<div class="file-info">
|
| 69 |
+
<strong>File:</strong> {file_info['filename']}<br>
|
| 70 |
+
<strong>Rows:</strong> {file_info['rows']}<br>
|
| 71 |
+
<strong>Columns:</strong> {file_info['columns']}<br>
|
| 72 |
+
<strong>Size:</strong> {file_info['size']}
|
| 73 |
+
</div>
|
| 74 |
+
""", unsafe_allow_html=True)
|
| 75 |
+
|
| 76 |
+
# Create tabs for different views
|
| 77 |
+
tab1, tab2, tab3 = st.tabs(["π Data View", "π Statistics", "π Filter"])
|
| 78 |
+
|
| 79 |
+
with tab1:
|
| 80 |
+
st.subheader("Data Preview")
|
| 81 |
+
# Display options
|
| 82 |
+
col1, col2 = st.columns(2)
|
| 83 |
+
with col1:
|
| 84 |
+
rows_to_show = st.slider(
|
| 85 |
+
"Number of rows to display",
|
| 86 |
+
min_value=5,
|
| 87 |
+
max_value=min(100, len(df)),
|
| 88 |
+
value=min(10, len(df))
|
| 89 |
+
)
|
| 90 |
+
with col2:
|
| 91 |
+
show_columns = st.multiselect(
|
| 92 |
+
"Select columns to display",
|
| 93 |
+
options=df.columns.tolist(),
|
| 94 |
+
default=df.columns.tolist()
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Display the dataframe
|
| 98 |
+
if show_columns:
|
| 99 |
+
st.dataframe(
|
| 100 |
+
df[show_columns].head(rows_to_show),
|
| 101 |
+
use_container_width=True,
|
| 102 |
+
height=400
|
| 103 |
+
)
|
| 104 |
+
else:
|
| 105 |
+
st.warning("Please select at least one column to display")
|
| 106 |
+
|
| 107 |
+
# Download button
|
| 108 |
+
csv = df.to_csv(index=False).encode('utf-8')
|
| 109 |
+
st.download_button(
|
| 110 |
+
label="Download CSV",
|
| 111 |
+
data=csv,
|
| 112 |
+
file_name="filtered_data.csv",
|
| 113 |
+
mime="text/csv"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
with tab2:
|
| 117 |
+
st.subheader("Data Statistics")
|
| 118 |
+
if st.checkbox("Show summary statistics"):
|
| 119 |
+
st.dataframe(df.describe(), use_container_width=True)
|
| 120 |
+
|
| 121 |
+
if st.checkbox("Show data types"):
|
| 122 |
+
st.write(df.dtypes)
|
| 123 |
+
|
| 124 |
+
if st.checkbox("Show missing values"):
|
| 125 |
+
missing_values = df.isnull().sum()
|
| 126 |
+
st.write(missing_values[missing_values > 0])
|
| 127 |
+
|
| 128 |
+
with tab3:
|
| 129 |
+
st.subheader("Filter Data")
|
| 130 |
+
st.write("Filter your data based on column values")
|
| 131 |
+
|
| 132 |
+
# Column selection for filtering
|
| 133 |
+
filter_column = st.selectbox(
|
| 134 |
+
"Select column to filter",
|
| 135 |
+
options=df.columns.tolist()
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
if filter_column:
|
| 139 |
+
# Get unique values for the selected column
|
| 140 |
+
unique_values = df[filter_column].unique()
|
| 141 |
+
|
| 142 |
+
if len(unique_values) > 20:
|
| 143 |
+
st.warning("This column has many unique values. Consider using a different filter method.")
|
| 144 |
+
filter_method = st.radio(
|
| 145 |
+
"Filter method",
|
| 146 |
+
["Range", "Contains"]
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
if filter_method == "Range":
|
| 150 |
+
min_val = st.number_input(
|
| 151 |
+
"Minimum value",
|
| 152 |
+
value=float(df[filter_column].min())
|
| 153 |
+
)
|
| 154 |
+
max_val = st.number_input(
|
| 155 |
+
"Maximum value",
|
| 156 |
+
value=float(df[filter_column].max())
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
if st.button("Apply Filter"):
|
| 160 |
+
filtered_df = filter_dataframe(df, filter_column, min_val, max_val)
|
| 161 |
+
st.dataframe(filtered_df, use_container_width=True)
|
| 162 |
+
else:
|
| 163 |
+
search_term = st.text_input("Contains text")
|
| 164 |
+
if st.button("Apply Filter"):
|
| 165 |
+
filtered_df = df[df[filter_column].astype(str).str.contains(search_term, case=False, na=False)]
|
| 166 |
+
st.dataframe(filtered_df, use_container_width=True)
|
| 167 |
+
else:
|
| 168 |
+
selected_values = st.multiselect(
|
| 169 |
+
"Select values to include",
|
| 170 |
+
options=unique_values
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
if st.button("Apply Filter"):
|
| 174 |
+
filtered_df = df[df[filter_column].isin(selected_values)]
|
| 175 |
+
st.dataframe(filtered_df, use_container_width=True)
|
| 176 |
+
|
| 177 |
+
except Exception as e:
|
| 178 |
+
st.error(f"Error processing file: {str(e)}")
|
| 179 |
+
|
| 180 |
+
if __name__ == "__main__":
|
| 181 |
+
main()
|