Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -52,137 +52,60 @@ max_attempts = 5
|
|
52 |
|
53 |
upload_file = st.file_uploader("Upload your file. Accepted file formats include: .csv, .xlsx", type=['csv', 'xlsx'])
|
54 |
|
55 |
-
|
56 |
if upload_file is not None:
|
57 |
file_extension = upload_file.name.split('.')[-1].lower()
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
new_columns = [f'column_{i+1}' for i in range(len(df.columns))]
|
66 |
-
df.columns = new_columns
|
67 |
-
|
68 |
-
all_columns = df.columns.tolist()
|
69 |
-
st.subheader("Select columns for the Tree Map", divider="blue")
|
70 |
-
parent_column = st.selectbox("Select the parent column:", all_columns)
|
71 |
-
value_column = st.selectbox("Select the value column:", all_columns)
|
72 |
-
|
73 |
-
if parent_column and value_column:
|
74 |
-
if parent_column == value_column:
|
75 |
-
st.warning("Warning: You have selected the same column for both the parent and value column. Please select two different columns from your data.")
|
76 |
-
elif parent_column and value_column:
|
77 |
-
path_columns = [px.Constant("all"), parent_column, value_column]
|
78 |
-
fig = px.treemap(df,
|
79 |
-
path=path_columns)
|
80 |
-
|
81 |
-
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
|
82 |
-
st.subheader("Tree map", divider="red")
|
83 |
-
st.plotly_chart(fig)
|
84 |
-
|
85 |
-
|
86 |
-
st.session_state.df = df
|
87 |
-
except pd.errors.ParserError:
|
88 |
-
st.error("Error: The CSV file is not readable or is incorrectly formatted.")
|
89 |
-
st.stop()
|
90 |
-
except UnicodeDecodeError:
|
91 |
-
st.error("Error: The CSV file could not be decoded.")
|
92 |
st.stop()
|
93 |
-
|
94 |
-
|
|
|
95 |
st.stop()
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
new_columns = [f'column_{i+1}' for i in range(len(df.columns))]
|
105 |
-
df.columns = new_columns
|
106 |
-
|
107 |
-
all_columns = df.columns.tolist()
|
108 |
-
st.subheader("Select columns for the Tree Map", divider="blue")
|
109 |
-
parent_column = st.selectbox("Select the parent column:", all_columns)
|
110 |
-
value_column = st.selectbox("Select the value column:", all_columns)
|
111 |
-
|
112 |
if parent_column and value_column:
|
113 |
if parent_column == value_column:
|
114 |
st.warning("Warning: You have selected the same column for both the parent and value column. Please select two different columns from your data.")
|
115 |
-
|
|
|
116 |
path_columns = [px.Constant("all"), parent_column, value_column]
|
117 |
-
fig = px.treemap(
|
118 |
-
|
119 |
-
|
120 |
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
|
121 |
st.subheader("Tree map", divider="red")
|
122 |
st.plotly_chart(fig)
|
123 |
-
|
124 |
-
|
125 |
-
st.session_state.df = df
|
126 |
-
|
127 |
-
except ValueError:
|
128 |
-
st.error("Error: The Excel file is not readable or is incorrectly formatted.")
|
129 |
-
st.stop()
|
130 |
-
except Exception as e:
|
131 |
-
st.error(f"An unexpected error occurred while reading Excel: {e}")
|
132 |
-
st.stop()
|
133 |
-
else:
|
134 |
-
st.warning("Unsupported file type.")
|
135 |
-
st.stop()
|
136 |
-
|
137 |
|
|
|
|
|
|
|
|
|
138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
-
|
141 |
st.divider()
|
142 |
-
|
143 |
-
if upload_file is not None:
|
144 |
-
file_extension = upload_file.name.split('.')[-1].lower()
|
145 |
-
if file_extension == 'csv':
|
146 |
-
try:
|
147 |
-
df = pd.read_csv(upload_file, na_filter=False)
|
148 |
-
if df.isnull().values.any():
|
149 |
-
st.error("Error: The CSV file contains missing values.")
|
150 |
-
st.stop()
|
151 |
-
else:
|
152 |
-
st.dataframe(df, key="csv_dataframe")
|
153 |
-
st.write("_number of rows_", df.shape[0])
|
154 |
-
st.write("_number of columns_", df.shape[1])
|
155 |
-
st.session_state.df = df
|
156 |
-
except pd.errors.ParserError:
|
157 |
-
st.error("Error: The CSV file is not readable or is incorrectly formatted.")
|
158 |
-
st.stop()
|
159 |
-
except UnicodeDecodeError:
|
160 |
-
st.error("Error: The CSV file could not be decoded.")
|
161 |
-
st.stop()
|
162 |
-
except Exception as e:
|
163 |
-
st.error(f"An unexpected error occurred while reading CSV: {e}")
|
164 |
-
st.stop()
|
165 |
-
elif file_extension == 'xlsx':
|
166 |
-
try:
|
167 |
-
df = pd.read_excel(upload_file, na_filter=False)
|
168 |
-
|
169 |
-
if df.isnull().values.any():
|
170 |
-
st.error("Error: The Excel file contains missing values.")
|
171 |
-
st.stop()
|
172 |
-
else:
|
173 |
-
st.dataframe(df, key="excel_dataframe")
|
174 |
-
st.write("_number of rows_", df.shape[0])
|
175 |
-
st.write("_number of columns_", df.shape[1])
|
176 |
-
st.session_state.df = df
|
177 |
-
except ValueError:
|
178 |
-
st.error("Error: The Excel file is not readable or is incorrectly formatted.")
|
179 |
-
st.stop()
|
180 |
-
except Exception as e:
|
181 |
-
st.error(f"An unexpected error occurred while reading Excel: {e}")
|
182 |
-
st.stop()
|
183 |
-
else:
|
184 |
-
st.warning("Unsupported file type.")
|
185 |
-
st.stop()
|
186 |
|
187 |
def clear_question():
|
188 |
st.session_state["question"] = ""
|
|
|
52 |
|
53 |
upload_file = st.file_uploader("Upload your file. Accepted file formats include: .csv, .xlsx", type=['csv', 'xlsx'])
|
54 |
|
|
|
55 |
if upload_file is not None:
|
56 |
file_extension = upload_file.name.split('.')[-1].lower()
|
57 |
+
try:
|
58 |
+
if file_extension == 'csv':
|
59 |
+
df_original = pd.read_csv(upload_file, na_filter=False)
|
60 |
+
elif file_extension == 'xlsx':
|
61 |
+
df_original = pd.read_excel(upload_file, na_filter=False)
|
62 |
+
else:
|
63 |
+
st.warning("Unsupported file type.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
st.stop()
|
65 |
+
|
66 |
+
if df_original.isnull().values.any():
|
67 |
+
st.error("Error: The file contains missing values.")
|
68 |
st.stop()
|
69 |
+
else:
|
70 |
+
st.session_state.df_original = df_original # Store the original dataframe
|
71 |
+
|
72 |
+
all_columns = df_original.columns.tolist()
|
73 |
+
st.subheader("Select columns for the Tree Map", divider="blue")
|
74 |
+
parent_column = st.selectbox("Select the parent column:", all_columns)
|
75 |
+
value_column = st.selectbox("Select the value column:", all_columns)
|
76 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
if parent_column and value_column:
|
78 |
if parent_column == value_column:
|
79 |
st.warning("Warning: You have selected the same column for both the parent and value column. Please select two different columns from your data.")
|
80 |
+
else:
|
81 |
+
df_treemap = df_original.copy() # Create a copy for treemap
|
82 |
path_columns = [px.Constant("all"), parent_column, value_column]
|
83 |
+
fig = px.treemap(df_treemap,
|
84 |
+
path=path_columns)
|
|
|
85 |
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
|
86 |
st.subheader("Tree map", divider="red")
|
87 |
st.plotly_chart(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
st.subheader("Uploaded Data", divider="green")
|
90 |
+
st.dataframe(df_original, key="uploaded_dataframe")
|
91 |
+
st.write(f"_Number of rows_: {df_original.shape[0]}")
|
92 |
+
st.write(f"_Number of columns_: {df_original.shape[1]}")
|
93 |
|
94 |
+
except pd.errors.ParserError:
|
95 |
+
st.error("Error: The CSV file is not readable or is incorrectly formatted.")
|
96 |
+
st.stop()
|
97 |
+
except UnicodeDecodeError:
|
98 |
+
st.error("Error: The CSV file could not be decoded.")
|
99 |
+
st.stop()
|
100 |
+
except ValueError:
|
101 |
+
st.error("Error: The Excel file is not readable or is incorrectly formatted.")
|
102 |
+
st.stop()
|
103 |
+
except Exception as e:
|
104 |
+
st.error(f"An unexpected error occurred: {e}")
|
105 |
+
st.stop()
|
106 |
|
|
|
107 |
st.divider()
|
108 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
def clear_question():
|
111 |
st.session_state["question"] = ""
|