Add comprehensive upload debugging and 4-method file reading
Browse files- app.py +47 -14
- debug_upload.py +93 -0
app.py
CHANGED
|
@@ -55,7 +55,7 @@ if 'uploaded_data_df' not in st.session_state:
|
|
| 55 |
if 'last_uploaded_file' not in st.session_state:
|
| 56 |
st.session_state.last_uploaded_file = None
|
| 57 |
|
| 58 |
-
# Enhanced file upload processing
|
| 59 |
if uploaded_file is not None:
|
| 60 |
# Check if this is a new file
|
| 61 |
file_id = f"{uploaded_file.name}_{uploaded_file.size}"
|
|
@@ -65,29 +65,62 @@ if uploaded_file is not None:
|
|
| 65 |
|
| 66 |
# Only process if it's a new file
|
| 67 |
if st.session_state.current_file_id != file_id:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
try:
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
#
|
| 72 |
try:
|
| 73 |
-
#
|
| 74 |
-
df = pd.read_csv(uploaded_file)
|
| 75 |
-
except:
|
| 76 |
-
# Method 2: Read as bytes then convert
|
| 77 |
bytes_data = uploaded_file.getvalue()
|
| 78 |
-
df = pd.read_csv(
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
st.session_state.uploaded_data_df = df
|
| 82 |
st.session_state.last_uploaded_file = uploaded_file.name
|
| 83 |
st.session_state.current_file_id = file_id
|
| 84 |
|
| 85 |
st.sidebar.success(f"β
**File loaded successfully!**")
|
| 86 |
st.sidebar.success(f"π **{uploaded_file.name}** ({len(df)} rows, {len(df.columns)} columns)")
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
st.sidebar.error(
|
| 90 |
-
st.sidebar.info("π‘ Try the
|
| 91 |
st.session_state.uploaded_data_df = None
|
| 92 |
else:
|
| 93 |
# File already processed
|
|
|
|
| 55 |
if 'last_uploaded_file' not in st.session_state:
|
| 56 |
st.session_state.last_uploaded_file = None
|
| 57 |
|
| 58 |
+
# Enhanced file upload processing with comprehensive error handling
|
| 59 |
if uploaded_file is not None:
|
| 60 |
# Check if this is a new file
|
| 61 |
file_id = f"{uploaded_file.name}_{uploaded_file.size}"
|
|
|
|
| 65 |
|
| 66 |
# Only process if it's a new file
|
| 67 |
if st.session_state.current_file_id != file_id:
|
| 68 |
+
st.sidebar.info("π Processing uploaded file...")
|
| 69 |
+
|
| 70 |
+
# Try multiple methods to read the file
|
| 71 |
+
df = None
|
| 72 |
+
success_method = None
|
| 73 |
+
|
| 74 |
+
# Method 1: Direct pandas read
|
| 75 |
try:
|
| 76 |
+
df = pd.read_csv(uploaded_file)
|
| 77 |
+
success_method = "Direct Read"
|
| 78 |
+
except Exception as e1:
|
| 79 |
+
st.sidebar.warning(f"Method 1 failed: {str(e1)[:50]}...")
|
| 80 |
|
| 81 |
+
# Method 2: Read as bytes
|
| 82 |
try:
|
| 83 |
+
uploaded_file.seek(0) # Reset file pointer
|
|
|
|
|
|
|
|
|
|
| 84 |
bytes_data = uploaded_file.getvalue()
|
| 85 |
+
df = pd.read_csv(BytesIO(bytes_data))
|
| 86 |
+
success_method = "Bytes Read"
|
| 87 |
+
except Exception as e2:
|
| 88 |
+
st.sidebar.warning(f"Method 2 failed: {str(e2)[:50]}...")
|
| 89 |
+
|
| 90 |
+
# Method 3: Read as string
|
| 91 |
+
try:
|
| 92 |
+
uploaded_file.seek(0) # Reset file pointer
|
| 93 |
+
string_data = uploaded_file.getvalue().decode("utf-8")
|
| 94 |
+
df = pd.read_csv(io.StringIO(string_data))
|
| 95 |
+
success_method = "String Read"
|
| 96 |
+
except Exception as e3:
|
| 97 |
+
st.sidebar.error(f"Method 3 failed: {str(e3)[:50]}...")
|
| 98 |
+
|
| 99 |
+
# Method 4: Force UTF-8 encoding
|
| 100 |
+
try:
|
| 101 |
+
uploaded_file.seek(0)
|
| 102 |
+
raw_data = uploaded_file.read()
|
| 103 |
+
if isinstance(raw_data, bytes):
|
| 104 |
+
string_data = raw_data.decode('utf-8')
|
| 105 |
+
else:
|
| 106 |
+
string_data = raw_data
|
| 107 |
+
df = pd.read_csv(io.StringIO(string_data))
|
| 108 |
+
success_method = "Force UTF-8"
|
| 109 |
+
except Exception as e4:
|
| 110 |
+
st.sidebar.error(f"All methods failed. Last error: {str(e4)}")
|
| 111 |
+
|
| 112 |
+
# If successful, store the data
|
| 113 |
+
if df is not None:
|
| 114 |
st.session_state.uploaded_data_df = df
|
| 115 |
st.session_state.last_uploaded_file = uploaded_file.name
|
| 116 |
st.session_state.current_file_id = file_id
|
| 117 |
|
| 118 |
st.sidebar.success(f"β
**File loaded successfully!**")
|
| 119 |
st.sidebar.success(f"π **{uploaded_file.name}** ({len(df)} rows, {len(df.columns)} columns)")
|
| 120 |
+
st.sidebar.info(f"π§ Used method: {success_method}")
|
| 121 |
+
else:
|
| 122 |
+
st.sidebar.error("β **All reading methods failed!**")
|
| 123 |
+
st.sidebar.info("π‘ Try the manual processing button below or paste your data")
|
| 124 |
st.session_state.uploaded_data_df = None
|
| 125 |
else:
|
| 126 |
# File already processed
|
debug_upload.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import io
|
| 4 |
+
import traceback
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="Upload Debug Tool", layout="wide")
|
| 7 |
+
st.title("π§ File Upload Debug Tool")
|
| 8 |
+
|
| 9 |
+
st.markdown("""
|
| 10 |
+
This tool will help us diagnose the exact upload issue.
|
| 11 |
+
""")
|
| 12 |
+
|
| 13 |
+
# Initialize session state
|
| 14 |
+
if 'debug_info' not in st.session_state:
|
| 15 |
+
st.session_state.debug_info = []
|
| 16 |
+
|
| 17 |
+
def log_debug(message):
|
| 18 |
+
st.session_state.debug_info.append(message)
|
| 19 |
+
st.write(f"DEBUG: {message}")
|
| 20 |
+
|
| 21 |
+
st.sidebar.header("Upload Test")
|
| 22 |
+
|
| 23 |
+
# Simple file uploader
|
| 24 |
+
uploaded_file = st.sidebar.file_uploader(
|
| 25 |
+
"Test File Upload",
|
| 26 |
+
type=["csv"],
|
| 27 |
+
key="debug_uploader"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
if uploaded_file is not None:
|
| 31 |
+
log_debug(f"File detected: {uploaded_file.name}")
|
| 32 |
+
log_debug(f"File size: {uploaded_file.size} bytes")
|
| 33 |
+
log_debug(f"File type: {uploaded_file.type}")
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
# Test method 1: Direct read
|
| 37 |
+
log_debug("Attempting Method 1: Direct pd.read_csv()")
|
| 38 |
+
df1 = pd.read_csv(uploaded_file)
|
| 39 |
+
log_debug(f"SUCCESS Method 1: {len(df1)} rows, {len(df1.columns)} columns")
|
| 40 |
+
st.success("β
Method 1 (Direct read) WORKED!")
|
| 41 |
+
st.dataframe(df1.head())
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
log_debug(f"FAILED Method 1: {str(e)}")
|
| 45 |
+
st.error(f"β Method 1 failed: {str(e)}")
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
# Test method 2: Reset and read as bytes
|
| 49 |
+
log_debug("Attempting Method 2: Read as bytes")
|
| 50 |
+
uploaded_file.seek(0) # Reset file pointer
|
| 51 |
+
bytes_data = uploaded_file.getvalue()
|
| 52 |
+
log_debug(f"Bytes data length: {len(bytes_data)}")
|
| 53 |
+
df2 = pd.read_csv(io.BytesIO(bytes_data))
|
| 54 |
+
log_debug(f"SUCCESS Method 2: {len(df2)} rows, {len(df2.columns)} columns")
|
| 55 |
+
st.success("β
Method 2 (Bytes read) WORKED!")
|
| 56 |
+
st.dataframe(df2.head())
|
| 57 |
+
|
| 58 |
+
except Exception as e2:
|
| 59 |
+
log_debug(f"FAILED Method 2: {str(e2)}")
|
| 60 |
+
st.error(f"β Method 2 failed: {str(e2)}")
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
# Test method 3: Read as string
|
| 64 |
+
log_debug("Attempting Method 3: Read as string")
|
| 65 |
+
uploaded_file.seek(0)
|
| 66 |
+
string_data = uploaded_file.getvalue().decode("utf-8")
|
| 67 |
+
log_debug(f"String data length: {len(string_data)}")
|
| 68 |
+
df3 = pd.read_csv(io.StringIO(string_data))
|
| 69 |
+
log_debug(f"SUCCESS Method 3: {len(df3)} rows, {len(df3.columns)} columns")
|
| 70 |
+
st.success("β
Method 3 (String read) WORKED!")
|
| 71 |
+
st.dataframe(df3.head())
|
| 72 |
+
|
| 73 |
+
except Exception as e3:
|
| 74 |
+
log_debug(f"FAILED Method 3: {str(e3)}")
|
| 75 |
+
st.error(f"β Method 3 failed: {str(e3)}")
|
| 76 |
+
st.error("β ALL METHODS FAILED!")
|
| 77 |
+
|
| 78 |
+
# Show full traceback
|
| 79 |
+
st.code(traceback.format_exc())
|
| 80 |
+
|
| 81 |
+
else:
|
| 82 |
+
st.info("π Upload a CSV file to test different reading methods")
|
| 83 |
+
|
| 84 |
+
# Show debug log
|
| 85 |
+
if st.session_state.debug_info:
|
| 86 |
+
st.markdown("### Debug Log")
|
| 87 |
+
for i, msg in enumerate(st.session_state.debug_info):
|
| 88 |
+
st.text(f"{i+1}. {msg}")
|
| 89 |
+
|
| 90 |
+
# Clear debug log
|
| 91 |
+
if st.button("Clear Debug Log"):
|
| 92 |
+
st.session_state.debug_info = []
|
| 93 |
+
st.rerun()
|