eaglelandsonce commited on
Commit
42adde1
·
verified ·
1 Parent(s): 09f448e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -35
app.py CHANGED
@@ -1,38 +1,55 @@
1
- import json
2
  import streamlit as st
 
3
  import matplotlib.pyplot as plt
4
 
5
- # Page config
6
- st.set_page_config(layout="wide")
7
-
8
- # Load grid data
9
- with open('grid.json') as f:
10
- grid_data = json.load(f)
11
-
12
- # Extract coordinates and labels
13
- buildings = [[b['coords'][0], b['coords'][1], b['label']] for b in grid_data['buildings']]
14
- stores = [[s['coords'][0], s['coords'][1]] for s in grid_data['stores']]
15
- roads = [(r['start'], r['end']) for r in grid_data['roads']]
16
-
17
- # Plot map
18
- fig, ax = plt.subplots()
19
- ax.axis([0, 10, 0, 10])
20
- ax.set_xticks([i for i in range(11)])
21
- ax.set_yticks([i for i in range(11)])
22
- ax.grid(color='grey', linestyle='-', linewidth=0.25)
23
-
24
- # Plot and label buildings
25
- for building in buildings:
26
- ax.scatter(building[0], building[1], color='red')
27
- ax.text(building[0], building[1], building[2], color='black', fontsize=8)
28
-
29
- # Plot stores
30
- ax.scatter(*zip(*stores), color='green')
31
-
32
- # Draw roads
33
- for start, end in roads:
34
- ax.plot([start[0], end[0]], [start[1], end[1]], color='black')
35
-
36
- # Show figure in Streamlit
37
- st.pyplot(fig)
38
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import json
3
  import matplotlib.pyplot as plt
4
 
5
+ # Function to load and parse the JSON data
6
+ def load_data(filename):
7
+ with open(filename, 'r') as file:
8
+ data = json.load(file)
9
+ return data
10
+
11
+ # Function to draw the grid layout
12
+ def draw_grid(data):
13
+ # Create a figure and a grid of subplots
14
+ fig, ax = plt.subplots(figsize=(10, 10))
15
+
16
+ # Setting the grid size
17
+ nrows, ncols = data['size']['rows'], data['size']['columns']
18
+ ax.set_xlim(0, ncols)
19
+ ax.set_ylim(0, nrows)
20
+ ax.set_xticks(range(ncols+1))
21
+ ax.set_yticks(range(nrows+1))
22
+ ax.grid(True)
23
+
24
+ # Plotting each building
25
+ for building in data['buildings']:
26
+ # Extracting the building details
27
+ coords = building['coords']
28
+ b_type = building['type']
29
+ size = building['size']
30
+
31
+ # Plotting the building on the grid
32
+ ax.add_patch(plt.Rectangle((coords[1], nrows-coords[0]-size), size, size, fill=None, edgecolor='r', linewidth=2))
33
+ ax.text(coords[1]+0.5*size, nrows-coords[0]-0.5*size, b_type, ha='center', va='center')
34
+
35
+ # Setting labels and title
36
+ ax.set_xlabel('Columns')
37
+ ax.set_ylabel('Rows')
38
+ ax.set_title('Village Layout')
39
+
40
+ return fig
41
+
42
+ # Streamlit application starts here
43
+ def main():
44
+ st.title('Green Smart Village Layout')
45
+
46
+ # Load and display the data
47
+ data = load_data('grid.json') # Adjust this path if your grid.json is located elsewhere
48
+ st.json(data)
49
+
50
+ # Drawing and displaying the grid layout
51
+ fig = draw_grid(data)
52
+ st.pyplot(fig)
53
+
54
+ if __name__ == "__main__":
55
+ main()