eaglelandsonce commited on
Commit
ff28ea7
·
verified ·
1 Parent(s): ca883bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -81
app.py CHANGED
@@ -1,46 +1,44 @@
1
- import streamlit as st
2
  import json
3
  import matplotlib.pyplot as plt
4
- import time
5
- from SPARQLWrapper import SPARQLWrapper, JSON
6
- from streamlit_agraph import agraph, TripleStore, Node, Edge, Config
7
 
 
8
  st.set_page_config(layout="wide")
9
 
10
- # Color codes updated with hexadecimal values for compatibility
11
- color_codes = {
12
- "residential": "#ADD8E6", # Light Blue
13
- "commercial": "#90EE90", # Light Green
14
- "community_facilities": "#FFFF00", # Yellow
15
- "school": "#FFFF00", # Yellow
16
- "healthcare_facility": "#FFFF00", # Yellow
17
- "green_space": "#90EE90", # Light Green
18
- "utility_infrastructure": "#90EE90", # Light Green
19
- "emergency_services": "#FF0000", # Red
20
- "cultural_facilities": "#D8BFD8", # Light Purple
21
- "recreational_facilities": "#D8BFD8", # Light Purple
22
- "innovation_center": "#90EE90", # Light Green
23
- "elderly_care_home": "#FFFF00", # Yellow
24
- "childcare_centers": "#FFFF00", # Yellow
25
- "places_of_worship": "#D8BFD8", # Light Purple
26
- "event_spaces": "#D8BFD8", # Light Purple
27
- "guest_housing": "#FFA500", # Orange
28
- "pet_care_facilities": "#FFA500", # Orange
29
- "public_sanitation_facilities": "#A0A0A0", # Grey
30
- "environmental_monitoring_stations": "#90EE90", # Light Green
31
- "disaster_preparedness_center": "#A0A0A0", # Grey
32
- "outdoor_community_spaces": "#90EE90" # Light Green
33
- }
34
-
35
- # Function to load and parse the JSON data
36
  def load_data(filename):
37
  with open(filename, 'r') as file:
38
  data = json.load(file)
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']
46
  ax.set_xlim(0, ncols)
@@ -48,85 +46,67 @@ def draw_grid(data, highlight=None):
48
  ax.set_xticks(range(ncols+1))
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')
78
 
 
79
  col1, col2, col3 = st.columns(3)
80
-
81
  with col1:
82
  st.header("Today's Agenda")
83
  st.write("1. Morning Meeting\n2. Review Project Plans\n3. Lunch Break\n4. Site Visit\n5. Evening Wrap-up")
 
84
  st.header("Agent Advisors")
85
  st.write("Would you like to optimize your HIN number?")
 
86
  st.header("My Incentive")
87
  st.write("Total incentive for HIN optimization")
88
-
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)}")
108
 
 
 
 
 
 
 
 
 
109
  with col3:
110
  st.header("Check Your HIN Number")
111
- config = Config(height=400, width=400, nodeHighlightBehavior=True, highlightColor="#F7A7A6", directed=True, collapsible=True)
112
-
113
- with open("data/fraud.json", encoding="utf8") as f:
114
- fraud_file = json.loads(f.read())
115
- st.session_state['fraud_topic'] = fraud_file
116
- fraud_store = TripleStore()
117
- for sub_graph in fraud_file["children"]:
118
- fraud_store.add_triple(fraud_file["name"], "has_subgroup", sub_graph["name"], picture=fraud_file["img"])
119
- for node in sub_graph["children"]:
120
- node1 = node["role"]
121
- link = "belongs_to"
122
- node2 = sub_graph["name"]
123
- pic = node["img"]
124
- fraud_store.add_triple(node1, link, node2, picture=pic)
125
- agraph(list(fraud_store.getNodes()), list(fraud_store.getEdges()), config)
126
-
127
  hin_number = st.text_input("Enter your HIN number:")
128
  if hin_number:
129
- st.write("HIN number details...")
130
 
131
  if __name__ == "__main__":
132
  main()
 
1
+ ximport 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
+ # Add other types with their corresponding colors
38
+ }
39
+
40
+ # Function to draw the grid with optional highlighting
41
+ def draw_grid(data, highlight_coords=None):
42
  fig, ax = plt.subplots(figsize=(12, 12))
43
  nrows, ncols = data['size']['rows'], data['size']['columns']
44
  ax.set_xlim(0, ncols)
 
46
  ax.set_xticks(range(ncols+1))
47
  ax.set_yticks(range(nrows+1))
48
  ax.grid(True)
49
+
50
  for building in data['buildings']:
51
  coords = building['coords']
52
  b_type = building['type']
53
  size = building['size']
54
+ color = color_codes.get(b_type, '#FFFFFF') # Default color is white if not specified
55
+
56
+ if highlight_coords and (coords[0], coords[1]) == tuple(highlight_coords):
57
+ highlighted_color = "#FFD700" # Gold for highlighting
58
+ ax.add_patch(plt.Rectangle((coords[1], nrows-coords[0]-size), size, size, color=highlighted_color, edgecolor='black', linewidth=2))
59
  else:
60
+ ax.add_patch(plt.Rectangle((coords[1], nrows-coords[0]-size), size, size, color=color, edgecolor='black', linewidth=1))
61
+ ax.text(coords[1]+0.5*size, nrows-coords[0]-0.5*size, b_type, ha='center', va='center', fontsize=8, color='black')
 
 
 
 
62
 
63
  ax.set_xlabel('Columns')
64
  ax.set_ylabel('Rows')
65
  ax.set_title('Village Layout with Color Coding')
66
  return fig
67
 
68
+ # Main Streamlit app function
 
69
  def main():
70
  st.title('Green Smart Village Application')
71
 
72
+ # Divide the page into three columns
73
  col1, col2, col3 = st.columns(3)
74
+
75
  with col1:
76
  st.header("Today's Agenda")
77
  st.write("1. Morning Meeting\n2. Review Project Plans\n3. Lunch Break\n4. Site Visit\n5. Evening Wrap-up")
78
+
79
  st.header("Agent Advisors")
80
  st.write("Would you like to optimize your HIN number?")
81
+
82
  st.header("My Incentive")
83
  st.write("Total incentive for HIN optimization")
84
+
85
  with col2:
86
  st.header("Green Smart Village Layout")
87
+ data = load_data('grid.json') # Ensure this path is correct
 
 
 
88
 
89
+ # Dropdown for selecting a building
90
+ building_options = [f"{bld['type']} at ({bld['coords'][0]}, {bld['coords'][1]})" for bld in data['buildings']]
91
+ selected_building = st.selectbox("Select a building to highlight:", options=building_options)
 
 
 
 
 
 
92
  selected_index = building_options.index(selected_building)
93
+ selected_building_coords = data['buildings'][selected_index]['coords']
 
94
 
95
+ # Draw the grid with the selected building highlighted
96
+ fig = draw_grid(data, highlight_coords=selected_building_coords)
97
+ st.pyplot(fig)
98
+
99
+ # Assuming sensors are defined in your data, display them
100
+ sensors = data['buildings'][selected_index].get('sensors', [])
101
+ st.write(f"Sensors in selected building: {', '.join(sensors)}")
102
+
103
  with col3:
104
  st.header("Check Your HIN Number")
105
+ # Placeholder for HIN number functionality
106
+ st.write("Enter your HIN number to check details...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  hin_number = st.text_input("Enter your HIN number:")
108
  if hin_number:
109
+ st.write("HIN number details...") # Placeholder for actual HIN number check
110
 
111
  if __name__ == "__main__":
112
  main()