Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,88 +30,168 @@ with col2:
|
|
| 30 |
with col3:
|
| 31 |
header("Claims Count",'h3')
|
| 32 |
|
| 33 |
-
# Second row with three file upload buttons
|
| 34 |
-
upload_col1, upload_col2, upload_col3 = st.columns(3)
|
| 35 |
-
with upload_col1:
|
| 36 |
-
|
| 37 |
-
with upload_col2:
|
| 38 |
-
|
| 39 |
-
with upload_col3:
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
# Third row with three buttons, enabled only if the corresponding file is uploaded
|
| 43 |
-
button_col1, button_col2, button_col3 = st.columns(3)
|
| 44 |
-
premiumlabel = "Select Premiums columns"
|
| 45 |
-
incurredlabel = "Select Incurred claims columns"
|
| 46 |
-
recoverlabel = "Select Recoveries columns"
|
| 47 |
-
claimscount = "Select Claims count columns"
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def process_premiums_file(uploaded_file):
|
| 50 |
-
# Read the CSV file into a DataFrame
|
| 51 |
df = pd.read_csv(uploaded_file)
|
| 52 |
-
|
| 53 |
-
# Store the DataFrame in the session state
|
| 54 |
st.session_state['premiums_df'] = df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
# Indicate that the CSV has been processed and we're ready to select columns
|
| 57 |
-
st.session_state['select_columns'] = True
|
| 58 |
-
|
| 59 |
-
# Button to select premiums, only displayed and active if a file has been uploaded
|
| 60 |
-
with button_col1:
|
| 61 |
-
# Check if the file has been uploaded
|
| 62 |
-
file_uploaded = uploaded_file_premiums is not None
|
| 63 |
-
# The button is enabled if a file has been uploaded, and disabled otherwise
|
| 64 |
-
if st.button(premiumlabel, disabled=not file_uploaded, key="premiums_button"):
|
| 65 |
-
# If the button is clicked (active), call the callback function
|
| 66 |
-
process_premiums_file(uploaded_file_premiums)
|
| 67 |
-
|
| 68 |
-
with button_col2:
|
| 69 |
-
if uploaded_file_claims_recoveries is not None:
|
| 70 |
-
st.button(incurredlabel)
|
| 71 |
-
st.button(recoverlabel)
|
| 72 |
-
else:
|
| 73 |
-
st.button(incurredlabel, disabled=True)
|
| 74 |
-
st.button(recoverlabel, disabled=True)
|
| 75 |
-
|
| 76 |
-
with button_col3:
|
| 77 |
-
if uploaded_file_claims_count is not None:
|
| 78 |
-
st.button(claimscount)
|
| 79 |
-
else:
|
| 80 |
-
st.button(claimscount, disabled=True)
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# If the CSV has been processed, display the column selection UI
|
| 84 |
-
if st.session_state.get('select_columns', False):
|
| 85 |
-
st.empty()
|
| 86 |
-
st.write("Please choose the 'lob' column and the values to filter by:")
|
| 87 |
-
|
| 88 |
-
# Determine columns with only string values and where no value is mostly numeric
|
| 89 |
string_columns = [
|
| 90 |
-
col for col in st.session_state[
|
| 91 |
-
if not any(not isinstance(x, str) or is_mostly_numeric(x) for x in st.session_state[
|
| 92 |
]
|
| 93 |
|
| 94 |
-
# Create two columns: one for the 'lob' column dropdown, one for the checkboxes
|
| 95 |
col1, col2 = st.columns(2)
|
| 96 |
-
|
| 97 |
-
# Dropdown to select the 'lob' column, filtered to only string columns
|
| 98 |
with col1:
|
| 99 |
-
lob_column = st.selectbox('Choose the column for "lob":', string_columns, key='
|
| 100 |
-
# Initialize an empty list to hold the selected values
|
| 101 |
selected_lob_values = []
|
| 102 |
|
| 103 |
-
# Generate checkboxes for each unique value in the 'lob' column
|
| 104 |
with col2:
|
| 105 |
st.write("Select values for filtering:")
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
# Retrieve unique values from the 'lob' column
|
| 109 |
-
unique_values = st.session_state['premiums_df'][st.session_state['lob_column']].unique()
|
| 110 |
-
# Sort the values for consistent checkbox ordering
|
| 111 |
unique_values.sort()
|
| 112 |
for value in unique_values:
|
| 113 |
-
|
| 114 |
-
if st.checkbox(f"{value}", key=f"checkbox_{value}"):
|
| 115 |
selected_lob_values.append(value)
|
| 116 |
|
| 117 |
# Filter the DataFrame based on selected 'lob' values
|
|
@@ -130,4 +210,47 @@ if st.session_state.get('select_columns', False):
|
|
| 130 |
'valuation_as_at': valuation_as_at,
|
| 131 |
'quarter_bracket': quarter_bracket,
|
| 132 |
# ... include other selections as well
|
| 133 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
with col3:
|
| 31 |
header("Claims Count",'h3')
|
| 32 |
|
| 33 |
+
# # Second row with three file upload buttons
|
| 34 |
+
# upload_col1, upload_col2, upload_col3 = st.columns(3)
|
| 35 |
+
# with upload_col1:
|
| 36 |
+
# uploaded_file_premiums = st.file_uploader("", key="premiums")
|
| 37 |
+
# with upload_col2:
|
| 38 |
+
# uploaded_file_claims_recoveries = st.file_uploader("", key="claims_recoveries")
|
| 39 |
+
# with upload_col3:
|
| 40 |
+
# uploaded_file_claims_count = st.file_uploader("", key="claims_count")
|
| 41 |
+
|
| 42 |
+
# # Third row with three buttons, enabled only if the corresponding file is uploaded
|
| 43 |
+
# button_col1, button_col2, button_col3 = st.columns(3)
|
| 44 |
+
# premiumlabel = "Select Premiums columns"
|
| 45 |
+
# incurredlabel = "Select Incurred claims columns"
|
| 46 |
+
# recoverlabel = "Select Recoveries columns"
|
| 47 |
+
# claimscount = "Select Claims count columns"
|
| 48 |
+
|
| 49 |
+
# def process_premiums_file(uploaded_file):
|
| 50 |
+
# # Read the CSV file into a DataFrame
|
| 51 |
+
# df = pd.read_csv(uploaded_file)
|
| 52 |
+
|
| 53 |
+
# # Store the DataFrame in the session state
|
| 54 |
+
# st.session_state['premiums_df'] = df
|
| 55 |
+
|
| 56 |
+
# # Indicate that the CSV has been processed and we're ready to select columns
|
| 57 |
+
# st.session_state['select_columns'] = True
|
| 58 |
+
|
| 59 |
+
# # Button to select premiums, only displayed and active if a file has been uploaded
|
| 60 |
+
# with button_col1:
|
| 61 |
+
# # Check if the file has been uploaded
|
| 62 |
+
# file_uploaded = uploaded_file_premiums is not None
|
| 63 |
+
# # The button is enabled if a file has been uploaded, and disabled otherwise
|
| 64 |
+
# if st.button(premiumlabel, disabled=not file_uploaded, key="premiums_button"):
|
| 65 |
+
# # If the button is clicked (active), call the callback function
|
| 66 |
+
# process_premiums_file(uploaded_file_premiums)
|
| 67 |
+
|
| 68 |
+
# with button_col2:
|
| 69 |
+
# if uploaded_file_claims_recoveries is not None:
|
| 70 |
+
# st.button(incurredlabel)
|
| 71 |
+
# st.button(recoverlabel)
|
| 72 |
+
# else:
|
| 73 |
+
# st.button(incurredlabel, disabled=True)
|
| 74 |
+
# st.button(recoverlabel, disabled=True)
|
| 75 |
+
|
| 76 |
+
# with button_col3:
|
| 77 |
+
# if uploaded_file_claims_count is not None:
|
| 78 |
+
# st.button(claimscount)
|
| 79 |
+
# else:
|
| 80 |
+
# st.button(claimscount, disabled=True)
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# # If the CSV has been processed, display the column selection UI
|
| 84 |
+
# if st.session_state.get('select_columns', False):
|
| 85 |
+
# st.empty()
|
| 86 |
+
# st.write("Please choose the 'lob' column and the values to filter by:")
|
| 87 |
+
|
| 88 |
+
# # Determine columns with only string values and where no value is mostly numeric
|
| 89 |
+
# string_columns = [
|
| 90 |
+
# col for col in st.session_state['premiums_df'].columns
|
| 91 |
+
# if not any(not isinstance(x, str) or is_mostly_numeric(x) for x in st.session_state['premiums_df'][col].dropna())
|
| 92 |
+
# ]
|
| 93 |
+
|
| 94 |
+
# # Create two columns: one for the 'lob' column dropdown, one for the checkboxes
|
| 95 |
+
# col1, col2 = st.columns(2)
|
| 96 |
+
|
| 97 |
+
# # Dropdown to select the 'lob' column, filtered to only string columns
|
| 98 |
+
# with col1:
|
| 99 |
+
# lob_column = st.selectbox('Choose the column for "lob":', string_columns, key='lob_column')
|
| 100 |
+
# # Initialize an empty list to hold the selected values
|
| 101 |
+
# selected_lob_values = []
|
| 102 |
+
|
| 103 |
+
# # Generate checkboxes for each unique value in the 'lob' column
|
| 104 |
+
# with col2:
|
| 105 |
+
# st.write("Select values for filtering:")
|
| 106 |
+
# # Ensure that 'lob_column' is in the session state and it's in the DataFrame columns
|
| 107 |
+
# if 'lob_column' in st.session_state and st.session_state['lob_column'] in st.session_state['premiums_df'].columns:
|
| 108 |
+
# # Retrieve unique values from the 'lob' column
|
| 109 |
+
# unique_values = st.session_state['premiums_df'][st.session_state['lob_column']].unique()
|
| 110 |
+
# # Sort the values for consistent checkbox ordering
|
| 111 |
+
# unique_values.sort()
|
| 112 |
+
# for value in unique_values:
|
| 113 |
+
# # Create a checkbox for each unique value in the 'lob' column
|
| 114 |
+
# if st.checkbox(f"{value}", key=f"checkbox_{value}"):
|
| 115 |
+
# selected_lob_values.append(value)
|
| 116 |
+
|
| 117 |
+
# # Filter the DataFrame based on selected 'lob' values
|
| 118 |
+
# if selected_lob_values:
|
| 119 |
+
# filtered_df = st.session_state['premiums_df'][st.session_state['premiums_df'][lob_column].isin(selected_lob_values)]
|
| 120 |
+
# st.session_state['filtered_premiums_df'] = filtered_df
|
| 121 |
+
|
| 122 |
+
# # Display other column selections after 'lob' has been selected and DataFrame has been filtered
|
| 123 |
+
# st.write("Now, select the columns for other parameters:")
|
| 124 |
+
# valuation_as_at = st.selectbox('Valuation As At:', filtered_df.columns, key='valuation_as_at')
|
| 125 |
+
# quarter_bracket = st.selectbox('Quarter Bracket:', filtered_df.columns, key='quarter_bracket')
|
| 126 |
+
# # Continue with other selections as required...
|
| 127 |
+
|
| 128 |
+
# # Store the column selections in the session state
|
| 129 |
+
# st.session_state['column_selections'] = {
|
| 130 |
+
# 'valuation_as_at': valuation_as_at,
|
| 131 |
+
# 'quarter_bracket': quarter_bracket,
|
| 132 |
+
# # ... include other selections as well
|
| 133 |
+
# }
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
# Set initial session states
|
| 141 |
+
if 'active_page' not in st.session_state:
|
| 142 |
+
st.session_state.active_page = None
|
| 143 |
+
if 'premiums_selection' not in st.session_state:
|
| 144 |
+
st.session_state.premiums_selection = False
|
| 145 |
+
if 'claims_recoveries_selection' not in st.session_state:
|
| 146 |
+
st.session_state.claims_recoveries_selection = False
|
| 147 |
+
if 'claims_count_selection' not in st.session_state:
|
| 148 |
+
st.session_state.claims_count_selection = False
|
| 149 |
+
|
| 150 |
+
# Helper function to check if a string has more than 50% numeric characters
|
| 151 |
+
def is_mostly_numeric(s):
|
| 152 |
+
if isinstance(s, str):
|
| 153 |
+
num_numeric = sum(c.isdigit() for c in s)
|
| 154 |
+
percent_numeric = num_numeric / len(s) if s else 0
|
| 155 |
+
return percent_numeric > 0.5
|
| 156 |
+
return False
|
| 157 |
+
|
| 158 |
+
# Callback functions for processing each uploaded file
|
| 159 |
def process_premiums_file(uploaded_file):
|
|
|
|
| 160 |
df = pd.read_csv(uploaded_file)
|
|
|
|
|
|
|
| 161 |
st.session_state['premiums_df'] = df
|
| 162 |
+
st.session_state.active_page = 'premiums_selection'
|
| 163 |
+
|
| 164 |
+
def process_claims_recoveries_file(uploaded_file):
|
| 165 |
+
df = pd.read_csv(uploaded_file)
|
| 166 |
+
st.session_state['claims_recoveries_df'] = df
|
| 167 |
+
st.session_state.active_page = 'claims_recoveries_selection'
|
| 168 |
+
|
| 169 |
+
def process_claims_count_file(uploaded_file):
|
| 170 |
+
df = pd.read_csv(uploaded_file)
|
| 171 |
+
st.session_state['claims_count_df'] = df
|
| 172 |
+
st.session_state.active_page = 'claims_count_selection'
|
| 173 |
+
|
| 174 |
+
# Function to display the column selection UI
|
| 175 |
+
def column_selection_ui(dataframe_key, selection_key):
|
| 176 |
+
st.write(f"Please choose the 'lob' column and the values to filter by for {selection_key}:")
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
string_columns = [
|
| 179 |
+
col for col in st.session_state[dataframe_key].columns
|
| 180 |
+
if not any(not isinstance(x, str) or is_mostly_numeric(x) for x in st.session_state[dataframe_key][col].dropna())
|
| 181 |
]
|
| 182 |
|
|
|
|
| 183 |
col1, col2 = st.columns(2)
|
|
|
|
|
|
|
| 184 |
with col1:
|
| 185 |
+
lob_column = st.selectbox('Choose the column for "lob":', string_columns, key=f'lob_column_{selection_key}')
|
|
|
|
| 186 |
selected_lob_values = []
|
| 187 |
|
|
|
|
| 188 |
with col2:
|
| 189 |
st.write("Select values for filtering:")
|
| 190 |
+
if lob_column in st.session_state[dataframe_key].columns:
|
| 191 |
+
unique_values = st.session_state[dataframe_key][lob_column].unique()
|
|
|
|
|
|
|
|
|
|
| 192 |
unique_values.sort()
|
| 193 |
for value in unique_values:
|
| 194 |
+
if st.checkbox(f"{value}", key=f"checkbox_{value}_{selection_key}"):
|
|
|
|
| 195 |
selected_lob_values.append(value)
|
| 196 |
|
| 197 |
# Filter the DataFrame based on selected 'lob' values
|
|
|
|
| 210 |
'valuation_as_at': valuation_as_at,
|
| 211 |
'quarter_bracket': quarter_bracket,
|
| 212 |
# ... include other selections as well
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
# File uploader and buttons
|
| 217 |
+
upload_col1, upload_col2, upload_col3 = st.columns(3)
|
| 218 |
+
with upload_col1:
|
| 219 |
+
uploaded_file_premiums = st.file_uploader("Upload Premiums Data", key="premiums")
|
| 220 |
+
with upload_col2:
|
| 221 |
+
uploaded_file_claims_recoveries = st.file_uploader("Upload Claims Recoveries Data", key="claims_recoveries")
|
| 222 |
+
with upload_col3:
|
| 223 |
+
uploaded_file_claims_count = st.file_uploader("Upload Claims Count Data", key="claims_count")
|
| 224 |
+
|
| 225 |
+
button_col1, button_col2, button_col3 = st.columns(3)
|
| 226 |
+
premiumlabel = "Select Premiums columns"
|
| 227 |
+
incurredlabel = "Select Incurred claims columns"
|
| 228 |
+
recoverlabel = "Select Recoveries columns"
|
| 229 |
+
claimscount = "Select Claims count columns"
|
| 230 |
+
|
| 231 |
+
# Display buttons if the corresponding file has been uploaded
|
| 232 |
+
if st.session_state.active_page is None:
|
| 233 |
+
with button_col1:
|
| 234 |
+
if uploaded_file_premiums is not None:
|
| 235 |
+
if st.button(premiumlabel):
|
| 236 |
+
process_premiums_file(uploaded_file_premiums)
|
| 237 |
+
|
| 238 |
+
with button_col2:
|
| 239 |
+
if uploaded_file_claims_recoveries is not None:
|
| 240 |
+
if st.button(incurredlabel):
|
| 241 |
+
process_claims_recoveries_file(uploaded_file_claims_recoveries)
|
| 242 |
+
if st.button(recoverlabel):
|
| 243 |
+
process_claims_recoveries_file(uploaded_file_claims_recoveries) # Modify as needed for different functionality
|
| 244 |
+
|
| 245 |
+
with button_col3:
|
| 246 |
+
if uploaded_file_claims_count is not None:
|
| 247 |
+
if st.button(claimscount):
|
| 248 |
+
process_claims_count_file(uploaded_file_claims_count)
|
| 249 |
+
|
| 250 |
+
# Conditional rendering based on the active page
|
| 251 |
+
if st.session_state.active_page == 'premiums_selection' and st.session_state.premiums_selection:
|
| 252 |
+
column_selection_ui('premiums_df', 'premiums_selection')
|
| 253 |
+
elif st.session_state.active_page == 'claims_recoveries_selection' and st.session_state.claims_recoveries_selection:
|
| 254 |
+
column_selection_ui('claims_recoveries_df', 'claims_recoveries_selection')
|
| 255 |
+
elif st.session_state.active_page == 'claims_count_selection' and st.session_state.claims_count_selection:
|
| 256 |
+
column_selection_ui('claims_count_df', 'claims_count_selection')
|