nithin81 commited on
Commit
d26f874
·
verified ·
1 Parent(s): cc26fab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -37
app.py CHANGED
@@ -70,8 +70,6 @@ if uploaded_file is not None:
70
 
71
  if result.tables:
72
  for table in result.tables:
73
- temp_kv_df = kv_df.copy()
74
-
75
  data = []
76
  for cell in table.cells:
77
  data.append([cell.row_index, cell.column_index, cell.content])
@@ -86,8 +84,6 @@ if uploaded_file is not None:
86
  # Display the results if processing is done
87
  if st.session_state.processed:
88
  if not st.session_state.kv_df.empty:
89
- st.write("Extracted Key-Value Pairs:")
90
- st.dataframe(st.session_state.kv_df)
91
  st.download_button(
92
  label="Download Key-Value Pairs as CSV",
93
  data=st.session_state.kv_df.to_csv(index=False, header=False).encode('utf-8'),
@@ -99,56 +95,74 @@ if st.session_state.processed:
99
 
100
 
101
  if st.session_state.tables:
102
- if not st.session_state.kv_df.empty:
103
- # Allow the user to select multiple columns
104
- first_row_values = st.session_state.kv_df.iloc[0].values
105
- selected_columns = st.multiselect(
106
- "Select columns from Key-Value Pairs to add to tables:", first_row_values
107
- )
108
- for i, table_df in enumerate(st.session_state.tables):
109
- st.write(f"Table {i + 1} with Selected Columns Added:")
110
 
111
- # Find the column names corresponding to the selected first row values
112
- column_indices = [list(first_row_values).index(val) for val in selected_columns]
113
- column_names = st.session_state.kv_df.columns[column_indices]
 
 
 
 
 
 
 
 
 
 
114
 
 
 
 
 
 
 
 
 
 
 
 
115
  # Add the selected columns to the table
116
  if selected_columns:
117
- # Create a new DataFrame for the selected columns
118
- selected_columns_df = st.session_state.kv_df[column_names].copy()
119
-
120
  # Align the length of selected_columns_df with the table_df rows
121
- selected_columns_df = selected_columns_df.iloc[:len(table_df)].reset_index(drop=True)
122
-
123
- rows_to_add = len(table_df) - len(selected_columns_df)
 
124
  if rows_to_add > 0:
125
  last_row = selected_columns_df.iloc[-1]
126
  additional_rows = pd.DataFrame([last_row] * rows_to_add, columns=selected_columns_df.columns)
127
  selected_columns_df = pd.concat([selected_columns_df, additional_rows], ignore_index=True)
128
-
129
- # Concatenate the selected columns DataFrame with the table DataFrame
130
- table_df = pd.concat([selected_columns_df, table_df.reset_index(drop=True)], axis=1, ignore_index=True)
131
 
132
- st.dataframe(table_df)
 
 
 
133
  st.download_button(
134
- label=f"Download Table {i + 1} as CSV",
135
- data=table_df.to_csv(index=False, header=False).encode('utf-8'),
136
- file_name=f"table_with_kv_{i + 1}.csv",
137
- mime='text/csv',
138
  )
139
- else:
140
- for i, table_df in enumerate(st.session_state.tables):
141
- st.write(f"Table {i + 1}:")
142
- st.dataframe(table_df)
143
  st.download_button(
144
- label=f"Download Table {i + 1} as CSV",
145
  data=table_df.to_csv(index=False, header=False).encode('utf-8'),
146
- file_name=f"table{i + 1}.csv",
147
  mime='text/csv',
148
- )
149
  else:
150
  st.write("No tables found in the document.")
151
-
152
 
153
  # Option to remove the file and clear the session
154
  if st.session_state.processed and st.button("Remove File"):
 
70
 
71
  if result.tables:
72
  for table in result.tables:
 
 
73
  data = []
74
  for cell in table.cells:
75
  data.append([cell.row_index, cell.column_index, cell.content])
 
84
  # Display the results if processing is done
85
  if st.session_state.processed:
86
  if not st.session_state.kv_df.empty:
 
 
87
  st.download_button(
88
  label="Download Key-Value Pairs as CSV",
89
  data=st.session_state.kv_df.to_csv(index=False, header=False).encode('utf-8'),
 
95
 
96
 
97
  if st.session_state.tables:
98
+ # Display available tables for selection, with "None" as the first option
99
+ table_options = ["Select"] + [f"Table {i + 1}" for i in range(len(st.session_state.tables))]
100
+ selected_table_option = st.selectbox(
101
+ "Select table to join key-value pairs:", table_options, index=0
102
+ )
 
 
 
103
 
104
+ # Proceed only if a table is selected (i.e., not "None")
105
+ if selected_table_option != "Select":
106
+ selected_table_index = table_options.index(selected_table_option) - 1 # Adjust for "None"
107
+ selected_table_df = st.session_state.tables[selected_table_index]
108
+
109
+ if not st.session_state.kv_df.empty:
110
+ # Allow the user to select multiple columns
111
+ first_row_values = st.session_state.kv_df.iloc[0].values
112
+ second_row_values = st.session_state.kv_df.iloc[1].values
113
+ # Format as "Key (row 0 value): Value (row 1 value)"
114
+ key_value_options = [
115
+ f"{key}: {value}" for key, value in zip(first_row_values, second_row_values)
116
+ ]
117
 
118
+ selected_columns = st.multiselect(
119
+ "Select key-value pairs to add to the selected table:", key_value_options
120
+ )
121
+
122
+ # Extract the column indices of the selected items
123
+ selected_indices = [
124
+ key_value_options.index(selected) for selected in selected_columns
125
+ ]
126
+
127
+ st.write(f"Selected Table {selected_table_index + 1} with Key-Value Pairs Added:")
128
+
129
  # Add the selected columns to the table
130
  if selected_columns:
131
+ # Create a DataFrame for the selected columns using the original kv_df
132
+ selected_columns_df = st.session_state.kv_df.iloc[:, selected_indices].copy()
133
+
134
  # Align the length of selected_columns_df with the table_df rows
135
+ selected_columns_df = selected_columns_df.iloc[:len(selected_table_df)].reset_index(drop=True)
136
+
137
+ # Add more rows if needed to match the table_df size
138
+ rows_to_add = len(selected_table_df) - len(selected_columns_df)
139
  if rows_to_add > 0:
140
  last_row = selected_columns_df.iloc[-1]
141
  additional_rows = pd.DataFrame([last_row] * rows_to_add, columns=selected_columns_df.columns)
142
  selected_columns_df = pd.concat([selected_columns_df, additional_rows], ignore_index=True)
 
 
 
143
 
144
+ # Concatenate the selected columns with the selected table DataFrame
145
+ selected_table_df = pd.concat([selected_columns_df, selected_table_df.reset_index(drop=True)], axis=1, ignore_index=True)
146
+
147
+ st.dataframe(selected_table_df)
148
  st.download_button(
149
+ label=f"Download Table {selected_table_index + 1} as CSV",
150
+ data=selected_table_df.to_csv(index=False, header=False).encode('utf-8'),
151
+ file_name=f"table_with_kv_{selected_table_index + 1}.csv",
152
+ mime='text/csv',
153
  )
154
+ else:
155
+
156
+ st.write(f"Selected Table {selected_table_index + 1}:")
157
+ st.dataframe(selected_table_df)
158
  st.download_button(
159
+ label=f"Download Table {selected_table_index + 1} as CSV",
160
  data=table_df.to_csv(index=False, header=False).encode('utf-8'),
161
+ file_name=f"table{selected_table_index + 1}.csv",
162
  mime='text/csv',
163
+ )
164
  else:
165
  st.write("No tables found in the document.")
 
166
 
167
  # Option to remove the file and clear the session
168
  if st.session_state.processed and st.button("Remove File"):