Spaces:
Runtime error
Runtime error
Fix: Use session state to persist data between button clicks
Browse files- src/streamlit_app.py +16 -5
src/streamlit_app.py
CHANGED
|
@@ -36,22 +36,26 @@ st.title("Entity Resolution on CSV (Network Graph)")
|
|
| 36 |
st.sidebar.header("Upload CSV for Entity Resolution")
|
| 37 |
uploaded_file = st.sidebar.file_uploader("Choose a CSV file", type=["csv"])
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# Generate sample data option
|
| 40 |
st.sidebar.markdown("**Or use sample data:**")
|
| 41 |
if st.sidebar.button("Use Sample Data"):
|
| 42 |
# Create simple sample data for testing
|
| 43 |
-
|
| 44 |
'first_name': ['John', 'Jon', 'Jane', 'Jain', 'Mike', 'Michael'],
|
| 45 |
'last_name': ['Smith', 'Smith', 'Doe', 'Doe', 'Johnson', 'Johnson'],
|
| 46 |
'email_address': ['john.smith@email.com', 'j.smith@gmail.com', 'jane.doe@company.com', 'jdoe@company.com', 'mike.j@work.com', 'michael.johnson@work.com'],
|
| 47 |
'phone_number': ['555-0123', '555-0123', '555-0456', '(555) 456-0000', '555-0789', '5550789']
|
| 48 |
})
|
| 49 |
-
csv_buffer = io.StringIO()
|
| 50 |
-
sample_data.to_csv(csv_buffer, index=False)
|
| 51 |
-
csv_buffer.seek(0)
|
| 52 |
-
uploaded_file = csv_buffer
|
| 53 |
st.sidebar.success("Sample data loaded!")
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
similarity_threshold = st.sidebar.slider(
|
| 56 |
"Similarity Threshold",
|
| 57 |
min_value=0.0,
|
|
@@ -164,10 +168,17 @@ def find_connected_components_manual(nodes, edges):
|
|
| 164 |
# ----------------------
|
| 165 |
# LOAD CSV & PROCESS
|
| 166 |
# ----------------------
|
|
|
|
| 167 |
if uploaded_file is not None:
|
| 168 |
st.markdown("### Preview of Uploaded CSV Data")
|
| 169 |
df = pd.read_csv(uploaded_file)
|
| 170 |
st.dataframe(df.head(10))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
# Provide a "Run Entity Resolution" button
|
| 173 |
if st.button("Run Entity Resolution"):
|
|
|
|
| 36 |
st.sidebar.header("Upload CSV for Entity Resolution")
|
| 37 |
uploaded_file = st.sidebar.file_uploader("Choose a CSV file", type=["csv"])
|
| 38 |
|
| 39 |
+
# Initialize session state for sample data
|
| 40 |
+
if 'sample_data_df' not in st.session_state:
|
| 41 |
+
st.session_state.sample_data_df = None
|
| 42 |
+
|
| 43 |
# Generate sample data option
|
| 44 |
st.sidebar.markdown("**Or use sample data:**")
|
| 45 |
if st.sidebar.button("Use Sample Data"):
|
| 46 |
# Create simple sample data for testing
|
| 47 |
+
st.session_state.sample_data_df = pd.DataFrame({
|
| 48 |
'first_name': ['John', 'Jon', 'Jane', 'Jain', 'Mike', 'Michael'],
|
| 49 |
'last_name': ['Smith', 'Smith', 'Doe', 'Doe', 'Johnson', 'Johnson'],
|
| 50 |
'email_address': ['john.smith@email.com', 'j.smith@gmail.com', 'jane.doe@company.com', 'jdoe@company.com', 'mike.j@work.com', 'michael.johnson@work.com'],
|
| 51 |
'phone_number': ['555-0123', '555-0123', '555-0456', '(555) 456-0000', '555-0789', '5550789']
|
| 52 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
st.sidebar.success("Sample data loaded!")
|
| 54 |
|
| 55 |
+
# Clear sample data if a file is uploaded
|
| 56 |
+
if uploaded_file is not None:
|
| 57 |
+
st.session_state.sample_data_df = None
|
| 58 |
+
|
| 59 |
similarity_threshold = st.sidebar.slider(
|
| 60 |
"Similarity Threshold",
|
| 61 |
min_value=0.0,
|
|
|
|
| 168 |
# ----------------------
|
| 169 |
# LOAD CSV & PROCESS
|
| 170 |
# ----------------------
|
| 171 |
+
df = None
|
| 172 |
if uploaded_file is not None:
|
| 173 |
st.markdown("### Preview of Uploaded CSV Data")
|
| 174 |
df = pd.read_csv(uploaded_file)
|
| 175 |
st.dataframe(df.head(10))
|
| 176 |
+
elif st.session_state.sample_data_df is not None:
|
| 177 |
+
st.markdown("### Preview of Sample Data")
|
| 178 |
+
df = st.session_state.sample_data_df
|
| 179 |
+
st.dataframe(df.head(10))
|
| 180 |
+
|
| 181 |
+
if df is not None:
|
| 182 |
|
| 183 |
# Provide a "Run Entity Resolution" button
|
| 184 |
if st.button("Run Entity Resolution"):
|