Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,10 @@ def main():
|
|
| 19 |
df = pd.read_csv(uploaded_file)
|
| 20 |
elif uploaded_file.name.endswith(".xlsx"):
|
| 21 |
df = pd.read_excel(uploaded_file)
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
# Get all columns that start with "Question"
|
| 24 |
question_columns = [col for col in df.columns if col.startswith("Question")]
|
| 25 |
|
|
@@ -67,6 +70,19 @@ def main():
|
|
| 67 |
insert_position = df_selected.columns.get_loc(col) + 1
|
| 68 |
df_selected.insert(insert_position, freq_col_name, df_selected[col].map(frequency))
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
# Create a BytesIO object to save the Excel file in memory
|
| 71 |
excel_buffer = io.BytesIO()
|
| 72 |
writer = pd.ExcelWriter(excel_buffer, engine='xlsxwriter')
|
|
@@ -81,8 +97,9 @@ def main():
|
|
| 81 |
# Get the question text (after "Question: " prefix)
|
| 82 |
question_text = question_col.replace("Question: ", "").strip()
|
| 83 |
|
| 84 |
-
# Select the corresponding columns
|
| 85 |
df_triplet = df_selected[[question_col, response_col, frequency_col]].drop_duplicates(subset=[response_col])
|
|
|
|
| 86 |
|
| 87 |
# Prepare a valid sheet name by sanitizing the question text
|
| 88 |
valid_sheet_name = "".join([c for c in question_text if c.isalnum() or c in [' ', '_']]).replace(" ", "_")
|
|
@@ -90,6 +107,36 @@ def main():
|
|
| 90 |
# Save each DataFrame to a separate sheet in the Excel file (max 31 chars for Excel sheet names)
|
| 91 |
df_triplet.to_excel(writer, sheet_name=valid_sheet_name[:31], index=False)
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# Save the Excel file
|
| 94 |
writer.close()
|
| 95 |
excel_buffer.seek(0)
|
|
|
|
| 19 |
df = pd.read_csv(uploaded_file)
|
| 20 |
elif uploaded_file.name.endswith(".xlsx"):
|
| 21 |
df = pd.read_excel(uploaded_file)
|
| 22 |
+
|
| 23 |
+
# Set Color Pallette for Later Use
|
| 24 |
+
colors = ['#36516E', '#D6DCE5','#99CCE0', '#A5A5A5', '#74A9E8', '#636363'] # Adjust as needed for more data points
|
| 25 |
+
|
| 26 |
# Get all columns that start with "Question"
|
| 27 |
question_columns = [col for col in df.columns if col.startswith("Question")]
|
| 28 |
|
|
|
|
| 70 |
insert_position = df_selected.columns.get_loc(col) + 1
|
| 71 |
df_selected.insert(insert_position, freq_col_name, df_selected[col].map(frequency))
|
| 72 |
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
# Create a BytesIO object to save the Excel file in memory
|
| 87 |
excel_buffer = io.BytesIO()
|
| 88 |
writer = pd.ExcelWriter(excel_buffer, engine='xlsxwriter')
|
|
|
|
| 97 |
# Get the question text (after "Question: " prefix)
|
| 98 |
question_text = question_col.replace("Question: ", "").strip()
|
| 99 |
|
| 100 |
+
# Select the corresponding columns, drop duplicates, and sort by "response_col" in descending order
|
| 101 |
df_triplet = df_selected[[question_col, response_col, frequency_col]].drop_duplicates(subset=[response_col])
|
| 102 |
+
df_triplet = df_triplet.sort_values(by=response_col, ascending=True)
|
| 103 |
|
| 104 |
# Prepare a valid sheet name by sanitizing the question text
|
| 105 |
valid_sheet_name = "".join([c for c in question_text if c.isalnum() or c in [' ', '_']]).replace(" ", "_")
|
|
|
|
| 107 |
# Save each DataFrame to a separate sheet in the Excel file (max 31 chars for Excel sheet names)
|
| 108 |
df_triplet.to_excel(writer, sheet_name=valid_sheet_name[:31], index=False)
|
| 109 |
|
| 110 |
+
# Access the worksheet
|
| 111 |
+
worksheet = writer.sheets[valid_sheet_name[:31]]
|
| 112 |
+
|
| 113 |
+
# Create a pie chart object
|
| 114 |
+
chart = workbook.add_chart({'type': 'pie'})
|
| 115 |
+
|
| 116 |
+
# Define the data for the chart
|
| 117 |
+
categories_range = f"='{valid_sheet_name[:31]}'!B2:B{len(df_triplet) + 1}" # Response column (Category names)
|
| 118 |
+
values_range = f"='{valid_sheet_name[:31]}'!C2:C{len(df_triplet) + 1}" # Frequency column
|
| 119 |
+
|
| 120 |
+
# Add series to the chart with Category Names (responses), values, and percentages
|
| 121 |
+
chart.add_series({
|
| 122 |
+
'categories': categories_range,
|
| 123 |
+
'values': values_range,
|
| 124 |
+
'data_labels': {
|
| 125 |
+
'category': True, # Show Category Names (Response)
|
| 126 |
+
'percentage': True, # Show percentage
|
| 127 |
+
'value': False, # Show value
|
| 128 |
+
'position': 'outside_end', # Position labels outside the pie chart
|
| 129 |
+
'font': {'size': 14, 'bold': False, 'color': '#36516E'} # Set font size to 14 and color to #36516E
|
| 130 |
+
},
|
| 131 |
+
'points': [{'fill': {'color': color}} for color in colors[:len(df_triplet)]], # Apply the colors
|
| 132 |
+
})
|
| 133 |
+
|
| 134 |
+
# Disable the chart legend
|
| 135 |
+
chart.set_legend({'none': True})
|
| 136 |
+
|
| 137 |
+
# Insert the chart into the worksheet
|
| 138 |
+
worksheet.insert_chart('E5', chart)
|
| 139 |
+
|
| 140 |
# Save the Excel file
|
| 141 |
writer.close()
|
| 142 |
excel_buffer.seek(0)
|