Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,6 +39,7 @@ def load_data(filename):
|
|
| 39 |
return data
|
| 40 |
|
| 41 |
# Function to draw the grid layout with color coding and to highlight a selected building
|
|
|
|
| 42 |
def draw_grid(data, highlight=None):
|
| 43 |
fig, ax = plt.subplots(figsize=(12, 12))
|
| 44 |
nrows, ncols = data['size']['rows'], data['size']['columns']
|
|
@@ -48,35 +49,29 @@ def draw_grid(data, highlight=None):
|
|
| 48 |
ax.set_yticks(range(nrows+1))
|
| 49 |
ax.grid(True)
|
| 50 |
|
| 51 |
-
highlight_coords = None
|
| 52 |
-
if highlight is not None:
|
| 53 |
-
highlight_type, highlight_coords_str = highlight.split(' at ')
|
| 54 |
-
highlight_coords = tuple(map(int, highlight_coords_str.strip('()').split(',')))
|
| 55 |
-
|
| 56 |
for building in data['buildings']:
|
| 57 |
coords = building['coords']
|
| 58 |
b_type = building['type']
|
| 59 |
size = building['size']
|
| 60 |
color = color_codes.get(b_type, '#FFFFFF')
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
ax.add_patch(plt.Rectangle((coords[1], nrows-coords[0]-size), size, size, color=color, edgecolor=edgecolor, linewidth=linewidth))
|
| 64 |
ax.text(coords[1]+0.5*size, nrows-coords[0]-0.5*size, b_type, ha='center', va='center', fontsize=8, color='black')
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
start, end = road['start'], road['end']
|
| 68 |
-
if start[0] == end[0]: # Vertical road
|
| 69 |
-
for y in range(min(start[1], end[1]), max(start[1], end[1]) + 1):
|
| 70 |
-
ax.add_patch(plt.Rectangle((start[0], nrows-y-1), 1, 1, color=road['color']))
|
| 71 |
-
else: # Horizontal road
|
| 72 |
-
for x in range(min(start[0], end[0]), max(start[0], end[0]) + 1):
|
| 73 |
-
ax.add_patch(plt.Rectangle((x, nrows-start[1]-1), 1, 1, color=road['color']))
|
| 74 |
|
| 75 |
ax.set_xlabel('Columns')
|
| 76 |
ax.set_ylabel('Rows')
|
| 77 |
ax.set_title('Village Layout with Color Coding')
|
| 78 |
return fig
|
| 79 |
|
|
|
|
| 80 |
# Streamlit application starts here
|
| 81 |
def main():
|
| 82 |
st.title('Green Smart Village Application')
|
|
@@ -94,12 +89,19 @@ def main():
|
|
| 94 |
with col2:
|
| 95 |
st.header("Green Smart Village Layout")
|
| 96 |
data = load_data('grid.json')
|
|
|
|
| 97 |
building_options = [f"{bld['type']} at ({bld['coords'][0]}, {bld['coords'][1]})" for bld in data['buildings']]
|
| 98 |
selected_building = st.selectbox("Select a building to view sensors:", options=building_options)
|
| 99 |
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
st.pyplot(fig)
|
| 102 |
-
|
|
|
|
| 103 |
selected_index = building_options.index(selected_building)
|
| 104 |
sensors = data['buildings'][selected_index]['sensors']
|
| 105 |
st.write(f"Sensors in selected building: {', '.join(sensors)}")
|
|
|
|
| 39 |
return data
|
| 40 |
|
| 41 |
# Function to draw the grid layout with color coding and to highlight a selected building
|
| 42 |
+
# Modified draw_grid function with highlighting based on coordinates
|
| 43 |
def draw_grid(data, highlight=None):
|
| 44 |
fig, ax = plt.subplots(figsize=(12, 12))
|
| 45 |
nrows, ncols = data['size']['rows'], data['size']['columns']
|
|
|
|
| 49 |
ax.set_yticks(range(nrows+1))
|
| 50 |
ax.grid(True)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
for building in data['buildings']:
|
| 53 |
coords = building['coords']
|
| 54 |
b_type = building['type']
|
| 55 |
size = building['size']
|
| 56 |
color = color_codes.get(b_type, '#FFFFFF')
|
| 57 |
+
# Highlight the selected building
|
| 58 |
+
if coords == highlight:
|
| 59 |
+
edgecolor = 'red'
|
| 60 |
+
linewidth = 3
|
| 61 |
+
else:
|
| 62 |
+
edgecolor = 'black'
|
| 63 |
+
linewidth = 1
|
| 64 |
ax.add_patch(plt.Rectangle((coords[1], nrows-coords[0]-size), size, size, color=color, edgecolor=edgecolor, linewidth=linewidth))
|
| 65 |
ax.text(coords[1]+0.5*size, nrows-coords[0]-0.5*size, b_type, ha='center', va='center', fontsize=8, color='black')
|
| 66 |
+
|
| 67 |
+
# Add the roads drawing logic here, if applicable
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
ax.set_xlabel('Columns')
|
| 70 |
ax.set_ylabel('Rows')
|
| 71 |
ax.set_title('Village Layout with Color Coding')
|
| 72 |
return fig
|
| 73 |
|
| 74 |
+
|
| 75 |
# Streamlit application starts here
|
| 76 |
def main():
|
| 77 |
st.title('Green Smart Village Application')
|
|
|
|
| 89 |
with col2:
|
| 90 |
st.header("Green Smart Village Layout")
|
| 91 |
data = load_data('grid.json')
|
| 92 |
+
# Generate building options for the dropdown, including coordinates for easy parsing
|
| 93 |
building_options = [f"{bld['type']} at ({bld['coords'][0]}, {bld['coords'][1]})" for bld in data['buildings']]
|
| 94 |
selected_building = st.selectbox("Select a building to view sensors:", options=building_options)
|
| 95 |
|
| 96 |
+
# Parse the selected building's coordinates
|
| 97 |
+
_, selected_coords = selected_building.rsplit(' at ', 1)
|
| 98 |
+
selected_coords = eval(selected_coords) # Convert string tuple to actual tuple
|
| 99 |
+
|
| 100 |
+
# Call draw_grid with the selected building's coordinates to highlight it
|
| 101 |
+
fig = draw_grid(data, highlight=selected_coords)
|
| 102 |
st.pyplot(fig)
|
| 103 |
+
|
| 104 |
+
# Display sensors for the selected building
|
| 105 |
selected_index = building_options.index(selected_building)
|
| 106 |
sensors = data['buildings'][selected_index]['sensors']
|
| 107 |
st.write(f"Sensors in selected building: {', '.join(sensors)}")
|