File size: 3,250 Bytes
63aefc7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | import streamlit as st
import pandas as pd
import io
import traceback
st.set_page_config(page_title="Upload Debug Tool", layout="wide")
st.title("π§ File Upload Debug Tool")
st.markdown("""
This tool will help us diagnose the exact upload issue.
""")
# Initialize session state
if 'debug_info' not in st.session_state:
st.session_state.debug_info = []
def log_debug(message):
st.session_state.debug_info.append(message)
st.write(f"DEBUG: {message}")
st.sidebar.header("Upload Test")
# Simple file uploader
uploaded_file = st.sidebar.file_uploader(
"Test File Upload",
type=["csv"],
key="debug_uploader"
)
if uploaded_file is not None:
log_debug(f"File detected: {uploaded_file.name}")
log_debug(f"File size: {uploaded_file.size} bytes")
log_debug(f"File type: {uploaded_file.type}")
try:
# Test method 1: Direct read
log_debug("Attempting Method 1: Direct pd.read_csv()")
df1 = pd.read_csv(uploaded_file)
log_debug(f"SUCCESS Method 1: {len(df1)} rows, {len(df1.columns)} columns")
st.success("β
Method 1 (Direct read) WORKED!")
st.dataframe(df1.head())
except Exception as e:
log_debug(f"FAILED Method 1: {str(e)}")
st.error(f"β Method 1 failed: {str(e)}")
try:
# Test method 2: Reset and read as bytes
log_debug("Attempting Method 2: Read as bytes")
uploaded_file.seek(0) # Reset file pointer
bytes_data = uploaded_file.getvalue()
log_debug(f"Bytes data length: {len(bytes_data)}")
df2 = pd.read_csv(io.BytesIO(bytes_data))
log_debug(f"SUCCESS Method 2: {len(df2)} rows, {len(df2.columns)} columns")
st.success("β
Method 2 (Bytes read) WORKED!")
st.dataframe(df2.head())
except Exception as e2:
log_debug(f"FAILED Method 2: {str(e2)}")
st.error(f"β Method 2 failed: {str(e2)}")
try:
# Test method 3: Read as string
log_debug("Attempting Method 3: Read as string")
uploaded_file.seek(0)
string_data = uploaded_file.getvalue().decode("utf-8")
log_debug(f"String data length: {len(string_data)}")
df3 = pd.read_csv(io.StringIO(string_data))
log_debug(f"SUCCESS Method 3: {len(df3)} rows, {len(df3.columns)} columns")
st.success("β
Method 3 (String read) WORKED!")
st.dataframe(df3.head())
except Exception as e3:
log_debug(f"FAILED Method 3: {str(e3)}")
st.error(f"β Method 3 failed: {str(e3)}")
st.error("β ALL METHODS FAILED!")
# Show full traceback
st.code(traceback.format_exc())
else:
st.info("π Upload a CSV file to test different reading methods")
# Show debug log
if st.session_state.debug_info:
st.markdown("### Debug Log")
for i, msg in enumerate(st.session_state.debug_info):
st.text(f"{i+1}. {msg}")
# Clear debug log
if st.button("Clear Debug Log"):
st.session_state.debug_info = []
st.rerun()
|