Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -93,40 +93,19 @@ if viz_type == "Complaints Over Time":
|
|
| 93 |
start_date = st.sidebar.date_input("Start Date", pd.to_datetime("2020-01-01"), key="start_date")
|
| 94 |
end_date = st.sidebar.date_input("End Date", pd.to_datetime("2024-12-31"), key="end_date")
|
| 95 |
|
| 96 |
-
# # Dropdown for Housing Block (only show when Complaints by Housing Block and Type or the updated version is selected)
|
| 97 |
-
# if viz_type in ["Complaints by Housing Block and Type", "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)"]:
|
| 98 |
-
# block_options = ['All Blocks'] + sorted(data['Housing Block'].unique().tolist())
|
| 99 |
-
# selected_block = st.sidebar.selectbox(
|
| 100 |
-
# "Select Housing Block",
|
| 101 |
-
# options=block_options,
|
| 102 |
-
# key=f"block_select_{viz_type}" # Unique key for each visualization
|
| 103 |
-
# )
|
| 104 |
-
|
| 105 |
-
# # Ensuring selected_block is only used if defined
|
| 106 |
-
# if viz_type in ["Complaints by Housing Block and Type", "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)"] and 'selected_block' not in locals():
|
| 107 |
-
# selected_block = 'All Blocks' # Default to 'All Blocks' if no selection made
|
| 108 |
-
|
| 109 |
# Dropdown for Housing Block (only show when Complaints by Housing Block and Type or the updated version is selected)
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
'4600 block', '4700 block', '4800 block', '4900 block', '5000 block'
|
| 118 |
-
]
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
'1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
|
| 125 |
-
'1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
|
| 126 |
-
'2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
|
| 127 |
-
'2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
|
| 128 |
-
'3200 block', '3300 block'
|
| 129 |
-
]
|
| 130 |
|
| 131 |
# Filtering the data based on selected year
|
| 132 |
filtered_data_time = data # Use filtered_data if date range is not needed
|
|
@@ -539,6 +518,93 @@ elif viz_type == "Complaints by Housing Block and Type":
|
|
| 539 |
# """)
|
| 540 |
# In the above code , We incorporated all of the professor's suggestions and refined the chart to make it more useful for analysis while ensuring good aesthetics. Given that the data from block 3400 onwards is very sparse, we decided to exclude these records. This adjustment helped focus the visualization on the more relevant data, providing clearer insights and improving its overall effectiveness for analysis.
|
| 541 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
|
| 543 |
st.subheader("Complaints by Housing Block and Type - Incorporating Suggestions Based on Professor's Feedback")
|
| 544 |
|
|
@@ -573,7 +639,11 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
|
|
| 573 |
|
| 574 |
# Dropdown for Housing Block (excluding unwanted blocks)
|
| 575 |
block_options = ['All Blocks'] + available_blocks
|
| 576 |
-
selected_block = st.sidebar.selectbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
|
| 578 |
# Further filtering by selected Housing Block (if applicable)
|
| 579 |
if selected_block != 'All Blocks':
|
|
@@ -626,6 +696,7 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
|
|
| 626 |
# Display the plot in Streamlit
|
| 627 |
st.pyplot(fig)
|
| 628 |
|
|
|
|
| 629 |
# Description of the visualization
|
| 630 |
st.write("""
|
| 631 |
**What this visualization shows:**
|
|
|
|
| 93 |
start_date = st.sidebar.date_input("Start Date", pd.to_datetime("2020-01-01"), key="start_date")
|
| 94 |
end_date = st.sidebar.date_input("End Date", pd.to_datetime("2024-12-31"), key="end_date")
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
# Dropdown for Housing Block (only show when Complaints by Housing Block and Type or the updated version is selected)
|
| 97 |
+
if viz_type in ["Complaints by Housing Block and Type", "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)"]:
|
| 98 |
+
block_options = ['All Blocks'] + sorted(data['Housing Block'].unique().tolist())
|
| 99 |
+
selected_block = st.sidebar.selectbox(
|
| 100 |
+
"Select Housing Block",
|
| 101 |
+
options=block_options,
|
| 102 |
+
key=f"block_select_{viz_type}" # Unique key for each visualization
|
| 103 |
+
)
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
# Ensuring selected_block is only used if defined
|
| 106 |
+
if viz_type in ["Complaints by Housing Block and Type", "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)"] and 'selected_block' not in locals():
|
| 107 |
+
selected_block = 'All Blocks' # Default to 'All Blocks' if no selection made
|
| 108 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
# Filtering the data based on selected year
|
| 111 |
filtered_data_time = data # Use filtered_data if date range is not needed
|
|
|
|
| 518 |
# """)
|
| 519 |
# In the above code , We incorporated all of the professor's suggestions and refined the chart to make it more useful for analysis while ensuring good aesthetics. Given that the data from block 3400 onwards is very sparse, we decided to exclude these records. This adjustment helped focus the visualization on the more relevant data, providing clearer insights and improving its overall effectiveness for analysis.
|
| 520 |
|
| 521 |
+
# elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
|
| 522 |
+
# st.subheader("Complaints by Housing Block and Type - Incorporating Suggestions Based on Professor's Feedback")
|
| 523 |
+
|
| 524 |
+
# # Define blocks to be excluded
|
| 525 |
+
# excluded_blocks = [
|
| 526 |
+
# '3400 block', '3500 block', '3600 block', '3700 block', '3800 block', '3900 block',
|
| 527 |
+
# '4000 block', '4100 block', '4200 block', '4300 block', '4400 block', '4500 block',
|
| 528 |
+
# '4600 block', '4700 block', '4800 block', '4900 block', '5000 block'
|
| 529 |
+
# ]
|
| 530 |
+
|
| 531 |
+
# # Creating the desired order, excluding unwanted blocks
|
| 532 |
+
# desired_order = [
|
| 533 |
+
# '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
|
| 534 |
+
# '600 block', '700 block', '800 block', '900 block', '1000 block', '1100 block',
|
| 535 |
+
# '1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
|
| 536 |
+
# '1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
|
| 537 |
+
# '2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
|
| 538 |
+
# '2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
|
| 539 |
+
# '3200 block', '3300 block'
|
| 540 |
+
# ]
|
| 541 |
+
|
| 542 |
+
# # Filtering the data based on selected year
|
| 543 |
+
# filtered_data_time = data # Use filtered_data if date range is not needed
|
| 544 |
+
# if selected_year != 'All Time':
|
| 545 |
+
# filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
|
| 546 |
+
|
| 547 |
+
# # Exclude blocks from the data
|
| 548 |
+
# filtered_data_time = filtered_data_time[~filtered_data_time['Housing Block'].isin(excluded_blocks)]
|
| 549 |
+
|
| 550 |
+
# # Get the list of blocks excluding the unwanted ones
|
| 551 |
+
# available_blocks = sorted(filtered_data_time['Housing Block'].unique().tolist())
|
| 552 |
+
|
| 553 |
+
# # Dropdown for Housing Block (excluding unwanted blocks)
|
| 554 |
+
# block_options = ['All Blocks'] + available_blocks
|
| 555 |
+
# selected_block = st.sidebar.selectbox("Select Housing Block", options=block_options, key="block_select")
|
| 556 |
+
|
| 557 |
+
# # Further filtering by selected Housing Block (if applicable)
|
| 558 |
+
# if selected_block != 'All Blocks':
|
| 559 |
+
# filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
|
| 560 |
+
|
| 561 |
+
# # Pivoting the data
|
| 562 |
+
# complaint_pivot = filtered_data_time.pivot_table(
|
| 563 |
+
# index='Housing Block',
|
| 564 |
+
# columns='Type of Complaint',
|
| 565 |
+
# values='Disposition',
|
| 566 |
+
# aggfunc='count',
|
| 567 |
+
# fill_value=0
|
| 568 |
+
# )
|
| 569 |
+
|
| 570 |
+
# # Ensure the pivot data is numeric for plotting
|
| 571 |
+
# complaint_pivot = complaint_pivot.astype(float)
|
| 572 |
+
|
| 573 |
+
# # Reordering the pivot table by the desired order (excluding unwanted blocks)
|
| 574 |
+
# complaint_pivot = complaint_pivot.reindex(desired_order)
|
| 575 |
+
|
| 576 |
+
# # If a specific block is selected, only show that block on the x-axis
|
| 577 |
+
# if selected_block != 'All Blocks':
|
| 578 |
+
# complaint_pivot = complaint_pivot.loc[[selected_block]]
|
| 579 |
+
|
| 580 |
+
# # Calculating percentages for each complaint type per housing block
|
| 581 |
+
# percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
|
| 582 |
+
|
| 583 |
+
# # Plotting the data
|
| 584 |
+
# fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
|
| 585 |
+
|
| 586 |
+
# # Adding percentage labels to the plot
|
| 587 |
+
# ax = fig.gca()
|
| 588 |
+
# for idx, block in enumerate(complaint_pivot.index):
|
| 589 |
+
# cumulative_height = 0
|
| 590 |
+
# for i, complaint_type in enumerate(complaint_pivot.columns):
|
| 591 |
+
# count = complaint_pivot.iloc[idx, i]
|
| 592 |
+
# percent = percentages.iloc[idx, i]
|
| 593 |
+
# if count > 0:
|
| 594 |
+
# # Compute the position for the percentage label
|
| 595 |
+
# x_pos = idx - 0.4 + 0.8 / 2 # Adjusting the position of the label
|
| 596 |
+
# y_pos = cumulative_height + count / 2
|
| 597 |
+
# ax.text(
|
| 598 |
+
# x_pos, y_pos, f"{percent:.1f}%",
|
| 599 |
+
# ha='center', va='center',
|
| 600 |
+
# fontsize=10, color='black',
|
| 601 |
+
# bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
|
| 602 |
+
# )
|
| 603 |
+
# cumulative_height += count
|
| 604 |
+
|
| 605 |
+
# # Display the plot in Streamlit
|
| 606 |
+
# st.pyplot(fig)
|
| 607 |
+
|
| 608 |
elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
|
| 609 |
st.subheader("Complaints by Housing Block and Type - Incorporating Suggestions Based on Professor's Feedback")
|
| 610 |
|
|
|
|
| 639 |
|
| 640 |
# Dropdown for Housing Block (excluding unwanted blocks)
|
| 641 |
block_options = ['All Blocks'] + available_blocks
|
| 642 |
+
selected_block = st.sidebar.selectbox(
|
| 643 |
+
"Select Housing Block",
|
| 644 |
+
options=block_options,
|
| 645 |
+
key=f"block_select_{viz_type}" # Unique key for each visualization type
|
| 646 |
+
)
|
| 647 |
|
| 648 |
# Further filtering by selected Housing Block (if applicable)
|
| 649 |
if selected_block != 'All Blocks':
|
|
|
|
| 696 |
# Display the plot in Streamlit
|
| 697 |
st.pyplot(fig)
|
| 698 |
|
| 699 |
+
|
| 700 |
# Description of the visualization
|
| 701 |
st.write("""
|
| 702 |
**What this visualization shows:**
|