eaglelandsonce commited on
Commit
662683c
·
verified ·
1 Parent(s): a1b4074

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -51
app.py CHANGED
@@ -1,44 +1,60 @@
1
  import streamlit as st
2
  import json
3
  import matplotlib.pyplot as plt
 
 
4
 
5
- # Set Streamlit page configuration
6
  st.set_page_config(layout="wide")
7
 
8
- # Function to load JSON data
 
 
 
 
 
 
 
 
 
 
 
 
9
  def load_data(filename):
10
  with open(filename, 'r') as file:
11
  data = json.load(file)
12
  return data
13
 
14
- # Dictionary for color codes
15
  color_codes = {
16
- "residential": "#ADD8E6",
17
- "commercial": "#90EE90",
18
- "community_facilities": "#FFFF00",
19
- "school": "#FFFF00",
20
- "healthcare_facility": "#FFFF00",
21
- "green_space": "#90EE90",
22
- "utility_infrastructure": "#90EE90",
23
- "emergency_services": "#FF0000",
24
- "cultural_facilities": "#D8BFD8",
25
- "recreational_facilities": "#D8BFD8",
26
- "innovation_center": "#90EE90",
27
- "elderly_care_home": "#FFFF00",
28
- "childcare_centers": "#FFFF00",
29
- "places_of_worship": "#D8BFD8",
30
- "event_spaces": "#D8BFD8",
31
- "guest_housing": "#FFA500",
32
- "pet_care_facilities": "#FFA500",
33
- "public_sanitation_facilities": "#A0A0A0",
34
- "environmental_monitoring_stations": "#90EE90",
35
- "disaster_preparedness_center": "#A0A0A0",
36
- "outdoor_community_spaces": "#90EE90",
37
  }
38
 
39
- # Function to draw the grid with optional highlighting and roads
40
- def draw_grid(data, highlight_coords=None):
 
41
  fig, ax = plt.subplots(figsize=(12, 12))
 
 
42
  nrows, ncols = data['size']['rows'], data['size']['columns']
43
  ax.set_xlim(0, ncols)
44
  ax.set_ylim(0, nrows)
@@ -46,65 +62,72 @@ def draw_grid(data, highlight_coords=None):
46
  ax.set_yticks(range(nrows+1))
47
  ax.grid(True)
48
 
 
49
  for building in data['buildings']:
 
50
  coords = building['coords']
51
  b_type = building['type']
52
  size = building['size']
53
  color = color_codes.get(b_type, '#FFFFFF') # Default color is white if not specified
54
 
55
- if highlight_coords and (coords[0], coords[1]) == tuple(highlight_coords):
56
- highlighted_color = "#FFD700" # Gold for highlighting
57
- ax.add_patch(plt.Rectangle((coords[1], nrows-coords[0]-size), size, size, color=highlighted_color, edgecolor='black', linewidth=2))
58
- else:
59
- ax.add_patch(plt.Rectangle((coords[1], nrows-coords[0]-size), size, size, color=color, edgecolor='black', linewidth=1))
60
- ax.text(coords[1]+0.5*size, nrows-coords[0]-0.5*size, b_type, ha='center', va='center', fontsize=8, color='black')
61
-
62
- # Drawing roads
63
- for road in data.get('roads', []): # Ensures compatibility if 'roads' key is missing
64
  start, end = road['start'], road['end']
65
- road_color = road.get('color', '#696969') # Dark gray as default road color
66
  if start[0] == end[0]: # Vertical road
67
  for y in range(min(start[1], end[1]), max(start[1], end[1]) + 1):
68
- ax.add_patch(plt.Rectangle((start[0], nrows-y-1), 1, 1, color=road_color))
69
  else: # Horizontal road
70
  for x in range(min(start[0], end[0]), max(start[0], end[0]) + 1):
71
- ax.add_patch(plt.Rectangle((x, nrows-start[1]-1), 1, 1, color=road_color))
 
72
 
 
 
 
73
  ax.set_xlabel('Columns')
74
  ax.set_ylabel('Rows')
75
  ax.set_title('Village Layout with Color Coding')
 
76
  return fig
77
 
78
- # Main Streamlit app function
 
 
79
  def main():
80
  st.title('Green Smart Village Application')
81
-
82
- # Divide the page into three columns
83
  col1, col2, col3 = st.columns(3)
84
 
85
  with col1:
86
  st.header("Today's Agenda")
 
87
  st.write("1. Morning Meeting\n2. Review Project Plans\n3. Lunch Break\n4. Site Visit\n5. Evening Wrap-up")
88
-
89
  st.header("Agent Advisors")
90
- st.write("Would you like to optimize your HIN number?")
91
-
92
  st.header("My Incentive")
93
  st.write("Total incentive for HIN optimization")
94
 
95
  with col2:
96
  st.header("Green Smart Village Layout")
 
97
  data = load_data('grid.json') # Ensure this path is correct
98
-
99
- # Dropdown for selecting a building
 
 
100
  building_options = [f"{bld['type']} at ({bld['coords'][0]}, {bld['coords'][1]})" for bld in data['buildings']]
101
- selected_building = st.selectbox("Select a building to highlight:", options=building_options)
102
  selected_index = building_options.index(selected_building)
103
- selected_building_coords = data['buildings'][selected_index]['coords']
 
 
104
 
105
- # Draw the grid with the selected building highlighted and roads
106
- fig = draw_grid(data, highlight_coords=selected_building_coords)
107
- st.pyplot(fig)
108
 
109
  with col3:
110
  st.header("Check Your HIN Number")
@@ -131,6 +154,7 @@ def main():
131
  if hin_number:
132
  st.write("HIN number details...") # Placeholder for actual HIN number check
133
 
 
134
 
135
  if __name__ == "__main__":
136
  main()
 
1
  import streamlit as st
2
  import json
3
  import matplotlib.pyplot as plt
4
+ import time
5
+
6
 
 
7
  st.set_page_config(layout="wide")
8
 
9
+
10
+
11
+
12
+ # HIN Number +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
+ from SPARQLWrapper import SPARQLWrapper, JSON
14
+ from streamlit_agraph import agraph, TripleStore, Node, Edge, Config
15
+ import json
16
+
17
+
18
+ # Green Village ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
+
20
+
21
+ # Function to load and parse the JSON data
22
  def load_data(filename):
23
  with open(filename, 'r') as file:
24
  data = json.load(file)
25
  return data
26
 
27
+ # Color codes updated with hexadecimal values for compatibility
28
  color_codes = {
29
+ "residential": "#ADD8E6", # Light Blue
30
+ "commercial": "#90EE90", # Light Green
31
+ "community_facilities": "#FFFF00", # Yellow
32
+ "school": "#FFFF00", # Yellow
33
+ "healthcare_facility": "#FFFF00", # Yellow
34
+ "green_space": "#90EE90", # Light Green
35
+ "utility_infrastructure": "#90EE90", # Light Green
36
+ "emergency_services": "#FF0000", # Red
37
+ "cultural_facilities": "#D8BFD8", # Light Purple
38
+ "recreational_facilities": "#D8BFD8", # Light Purple
39
+ "innovation_center": "#90EE90", # Light Green
40
+ "elderly_care_home": "#FFFF00", # Yellow
41
+ "childcare_centers": "#FFFF00", # Yellow
42
+ "places_of_worship": "#D8BFD8", # Light Purple
43
+ "event_spaces": "#D8BFD8", # Light Purple
44
+ "guest_housing": "#FFA500", # Orange
45
+ "pet_care_facilities": "#FFA500", # Orange
46
+ "public_sanitation_facilities": "#A0A0A0", # Grey
47
+ "environmental_monitoring_stations": "#90EE90", # Light Green
48
+ "disaster_preparedness_center": "#A0A0A0", # Grey
49
+ "outdoor_community_spaces": "#90EE90" # Light Green
50
  }
51
 
52
+ # Function to draw the grid layout with color coding
53
+ def draw_grid(data):
54
+ # Create a figure and a grid of subplots
55
  fig, ax = plt.subplots(figsize=(12, 12))
56
+
57
+ # Setting the grid size
58
  nrows, ncols = data['size']['rows'], data['size']['columns']
59
  ax.set_xlim(0, ncols)
60
  ax.set_ylim(0, nrows)
 
62
  ax.set_yticks(range(nrows+1))
63
  ax.grid(True)
64
 
65
+ # Plotting each building with its assigned color from the color_codes dictionary
66
  for building in data['buildings']:
67
+ # Extracting the building details
68
  coords = building['coords']
69
  b_type = building['type']
70
  size = building['size']
71
  color = color_codes.get(b_type, '#FFFFFF') # Default color is white if not specified
72
 
73
+ # Plotting the building on the grid with color
74
+ ax.add_patch(plt.Rectangle((coords[1], nrows-coords[0]-size), size, size, color=color, edgecolor='black', linewidth=1))
75
+ ax.text(coords[1]+0.5*size, nrows-coords[0]-0.5*size, b_type, ha='center', va='center', fontsize=8, color='black')
76
+
77
+ # Draw roads
78
+ for road in data.get('roads', []): # Check for roads in the data, default to empty list if not found
 
 
 
79
  start, end = road['start'], road['end']
80
+ # Determine if the road is vertical or horizontal based on start and end coordinates
81
  if start[0] == end[0]: # Vertical road
82
  for y in range(min(start[1], end[1]), max(start[1], end[1]) + 1):
83
+ ax.add_patch(plt.Rectangle((start[0], nrows-y-1), 1, 1, color=road['color']))
84
  else: # Horizontal road
85
  for x in range(min(start[0], end[0]), max(start[0], end[0]) + 1):
86
+ ax.add_patch(plt.Rectangle((x, nrows-start[1]-1), 1, 1, color=road['color']))
87
+
88
 
89
+ # Reverse the y-axis numbers
90
+ # ax.invert_yaxis()
91
+ # Setting labels and title
92
  ax.set_xlabel('Columns')
93
  ax.set_ylabel('Rows')
94
  ax.set_title('Village Layout with Color Coding')
95
+
96
  return fig
97
 
98
+
99
+
100
+ # Streamlit application starts here
101
  def main():
102
  st.title('Green Smart Village Application')
103
+
104
+ # Creating three columns
105
  col1, col2, col3 = st.columns(3)
106
 
107
  with col1:
108
  st.header("Today's Agenda")
109
+ # Example content for Today's Agenda
110
  st.write("1. Morning Meeting\n2. Review Project Plans\n3. Lunch Break\n4. Site Visit\n5. Evening Wrap-up")
 
111
  st.header("Agent Advisors")
112
+ st.write("Would you like to optimize your HIN number")
 
113
  st.header("My Incentive")
114
  st.write("Total incentive for HIN optimization")
115
 
116
  with col2:
117
  st.header("Green Smart Village Layout")
118
+ # Load and display the data with color coding
119
  data = load_data('grid.json') # Ensure this path is correct
120
+ fig = draw_grid(data)
121
+ st.pyplot(fig)
122
+
123
+ # Interactivity: Selecting a building to display sensor data
124
  building_options = [f"{bld['type']} at ({bld['coords'][0]}, {bld['coords'][1]})" for bld in data['buildings']]
125
+ selected_building = st.selectbox("Select a building to view sensors:", options=building_options)
126
  selected_index = building_options.index(selected_building)
127
+ sensors = data['buildings'][selected_index]['sensors']
128
+ st.write(f"Sensors in selected building: {', '.join(sensors)}")
129
+
130
 
 
 
 
131
 
132
  with col3:
133
  st.header("Check Your HIN Number")
 
154
  if hin_number:
155
  st.write("HIN number details...") # Placeholder for actual HIN number check
156
 
157
+
158
 
159
  if __name__ == "__main__":
160
  main()