ahsangahothi commited on
Commit
d3f3670
·
verified ·
1 Parent(s): ddead5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -85
app.py CHANGED
@@ -1,115 +1,110 @@
1
  import streamlit as st
2
  import matplotlib.pyplot as plt
 
3
  import io
4
  import random
5
 
6
- # Convert the plot to bytes for download
7
  def fig_to_bytes(fig):
8
  img_bytes = io.BytesIO()
9
  fig.savefig(img_bytes, format="png")
10
  img_bytes.seek(0)
11
  return img_bytes.read()
12
 
13
- # Function to draw the house floor plan with detailed layout and random room placement
14
  def draw_house(length, width, unit):
15
- fig, ax = plt.subplots(figsize=(8, 8))
16
-
17
- # Plotting the plot boundary
18
  ax.plot([0, length], [0, 0], color="black", lw=2) # Bottom side
19
  ax.plot([0, length], [width, width], color="black", lw=2) # Top side
20
  ax.plot([0, 0], [0, width], color="black", lw=2) # Left side
21
  ax.plot([length, length], [0, width], color="black", lw=2) # Right side
22
 
23
- # Randomly place rooms within the plot
24
- # Bedroom (Randomly positioned)
25
- x1 = random.randint(0, length//2)
26
- y1 = random.randint(width//2, width)
27
- room_length1 = random.randint(10, 20)
28
- room_width1 = random.randint(6, 12)
29
- ax.plot([x1, x1 + room_length1], [y1, y1], color="blue", lw=2)
30
- ax.plot([x1 + room_length1, x1 + room_length1], [y1, y1 - room_width1], color="blue", lw=2)
31
- ax.plot([x1 + room_length1, x1], [y1 - room_width1, y1 - room_width1], color="blue", lw=2)
32
- ax.plot([x1, x1], [y1, y1 - room_width1], color="blue", lw=2)
33
- ax.text(x1 + room_length1//2, y1 - room_width1//2, "Bedroom 1", fontsize=10, ha="center", color="blue")
34
-
35
- # Drawing Room (Randomly positioned)
36
- x2 = random.randint(length//2, length)
37
- y2 = random.randint(width//2, width)
38
- room_length2 = random.randint(10, 20)
39
- room_width2 = random.randint(6, 12)
40
- ax.plot([x2, x2 + room_length2], [y2, y2], color="green", lw=2)
41
- ax.plot([x2 + room_length2, x2 + room_length2], [y2, y2 - room_width2], color="green", lw=2)
42
- ax.plot([x2 + room_length2, x2], [y2 - room_width2, y2 - room_width2], color="green", lw=2)
43
- ax.plot([x2, x2], [y2, y2 - room_width2], color="green", lw=2)
44
- ax.text(x2 + room_length2//2, y2 - room_width2//2, "Drawing Room", fontsize=10, ha="center", color="green")
45
-
46
- # Kitchen (Randomly positioned)
47
- x3 = random.randint(0, length//2)
48
- y3 = random.randint(0, width//2)
49
- room_length3 = random.randint(8, 15)
50
- room_width3 = random.randint(5, 10)
51
- ax.plot([x3, x3 + room_length3], [y3, y3], color="red", lw=2)
52
- ax.plot([x3 + room_length3, x3 + room_length3], [y3, y3 - room_width3], color="red", lw=2)
53
- ax.plot([x3 + room_length3, x3], [y3 - room_width3, y3 - room_width3], color="red", lw=2)
54
- ax.plot([x3, x3], [y3, y3 - room_width3], color="red", lw=2)
55
- ax.text(x3 + room_length3//2, y3 - room_width3//2, "Kitchen", fontsize=10, ha="center", color="red")
56
-
57
- # Lounge (Randomly positioned)
58
- x4 = random.randint(length//2, length)
59
- y4 = random.randint(0, width//2)
60
- room_length4 = random.randint(10, 18)
61
- room_width4 = random.randint(6, 12)
62
- ax.plot([x4, x4 + room_length4], [y4, y4], color="purple", lw=2)
63
- ax.plot([x4 + room_length4, x4 + room_length4], [y4, y4 - room_width4], color="purple", lw=2)
64
- ax.plot([x4 + room_length4, x4], [y4 - room_width4, y4 - room_width4], color="purple", lw=2)
65
- ax.plot([x4, x4], [y4, y4 - room_width4], color="purple", lw=2)
66
- ax.text(x4 + room_length4//2, y4 - room_width4//2, "Lounge", fontsize=10, ha="center", color="purple")
67
-
68
- # Car Parking (Randomly positioned)
69
- x5 = random.randint(length//4, length//2)
70
- y5 = random.randint(0, width//4)
71
- room_length5 = random.randint(12, 20)
72
- room_width5 = random.randint(8, 15)
73
- ax.plot([x5, x5 + room_length5], [y5, y5], color="orange", lw=2)
74
- ax.plot([x5 + room_length5, x5 + room_length5], [y5, y5 - room_width5], color="orange", lw=2)
75
- ax.plot([x5 + room_length5, x5], [y5 - room_width5, y5 - room_width5], color="orange", lw=2)
76
- ax.plot([x5, x5], [y5, y5 - room_width5], color="orange", lw=2)
77
- ax.text(x5 + room_length5//2, y5 - room_width5//2, "Car Parking", fontsize=10, ha="center", color="orange")
78
-
79
- # Servant Room (Randomly positioned)
80
- x6 = random.randint(0, length//4)
81
- y6 = random.randint(0, width//4)
82
- room_length6 = random.randint(6, 10)
83
- room_width6 = random.randint(4, 8)
84
- ax.plot([x6, x6 + room_length6], [y6, y6], color="brown", lw=2)
85
- ax.plot([x6 + room_length6, x6 + room_length6], [y6, y6 - room_width6], color="brown", lw=2)
86
- ax.plot([x6 + room_length6, x6], [y6 - room_width6, y6 - room_width6], color="brown", lw=2)
87
- ax.plot([x6, x6], [y6, y6 - room_width6], color="brown", lw=2)
88
- ax.text(x6 + room_length6//2, y6 - room_width6//2, "Servant Room", fontsize=10, ha="center", color="brown")
89
-
90
- # Ventilation (Randomly placed)
91
- x7 = random.randint(length//2, length)
92
- y7 = random.randint(width//2, width)
93
- ax.plot([x7, x7 + 4], [y7, y7], color="grey", lw=2)
94
- ax.text(x7 + 2, y7, "Ventilation", fontsize=8, ha="center", color="grey")
95
-
96
- # Adding Labels and Title
97
- ax.set_title(f"Random House Layout: Length = {length} {unit}, Width = {width} {unit}")
98
  ax.set_xlabel(f"Length ({unit})")
99
  ax.set_ylabel(f"Width ({unit})")
100
 
101
- # Set the limits
102
  ax.set_xlim(0, length + 10)
103
  ax.set_ylim(0, width + 10)
104
-
105
- # Hide the axes for better visual
106
  ax.axis('off')
107
 
108
  return fig
109
 
110
  # Streamlit interface
111
- st.title("Random House Layout Drawing App")
112
 
113
  # User input for plot dimensions and units
114
  length = st.number_input("Enter the Length of the Plot", min_value=1, max_value=1000, value=50)
115
- width = st.number_input("Enter the Width of
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import matplotlib.pyplot as plt
3
+ import matplotlib.patches as patches
4
  import io
5
  import random
6
 
7
+ # Function to convert the figure to bytes for download
8
  def fig_to_bytes(fig):
9
  img_bytes = io.BytesIO()
10
  fig.savefig(img_bytes, format="png")
11
  img_bytes.seek(0)
12
  return img_bytes.read()
13
 
14
+ # Function to draw the house floor plan with detailed rooms, windows, doors, and walls
15
  def draw_house(length, width, unit):
16
+ fig, ax = plt.subplots(figsize=(10, 10))
17
+
18
+ # Plot the boundary of the house (Plot limits)
19
  ax.plot([0, length], [0, 0], color="black", lw=2) # Bottom side
20
  ax.plot([0, length], [width, width], color="black", lw=2) # Top side
21
  ax.plot([0, 0], [0, width], color="black", lw=2) # Left side
22
  ax.plot([length, length], [0, width], color="black", lw=2) # Right side
23
 
24
+ # Define Room Dimensions and Placement (For this example we place 5 rooms)
25
+
26
+ # Bedroom (Fixed position for simplicity, real app should randomize it)
27
+ bedroom_length = 20
28
+ bedroom_width = 15
29
+ bedroom_x = 5
30
+ bedroom_y = 15
31
+ ax.add_patch(patches.Rectangle((bedroom_x, bedroom_y), bedroom_length, bedroom_width, linewidth=2, edgecolor="blue", facecolor="lightblue"))
32
+ ax.text(bedroom_x + bedroom_length/2, bedroom_y + bedroom_width/2, "Bedroom", fontsize=12, ha="center", va="center", color="black")
33
+
34
+ # Living Room
35
+ living_room_length = 25
36
+ living_room_width = 18
37
+ living_room_x = 5
38
+ living_room_y = 0
39
+ ax.add_patch(patches.Rectangle((living_room_x, living_room_y), living_room_length, living_room_width, linewidth=2, edgecolor="green", facecolor="lightgreen"))
40
+ ax.text(living_room_x + living_room_length/2, living_room_y + living_room_width/2, "Living Room", fontsize=12, ha="center", va="center", color="black")
41
+
42
+ # Kitchen
43
+ kitchen_length = 15
44
+ kitchen_width = 10
45
+ kitchen_x = 30
46
+ kitchen_y = 0
47
+ ax.add_patch(patches.Rectangle((kitchen_x, kitchen_y), kitchen_length, kitchen_width, linewidth=2, edgecolor="red", facecolor="lightcoral"))
48
+ ax.text(kitchen_x + kitchen_length/2, kitchen_y + kitchen_width/2, "Kitchen", fontsize=12, ha="center", va="center", color="black")
49
+
50
+ # Bathroom
51
+ bathroom_length = 8
52
+ bathroom_width = 6
53
+ bathroom_x = 30
54
+ bathroom_y = 12
55
+ ax.add_patch(patches.Rectangle((bathroom_x, bathroom_y), bathroom_length, bathroom_width, linewidth=2, edgecolor="purple", facecolor="violet"))
56
+ ax.text(bathroom_x + bathroom_length/2, bathroom_y + bathroom_width/2, "Bathroom", fontsize=12, ha="center", va="center", color="black")
57
+
58
+ # Garage
59
+ garage_length = 20
60
+ garage_width = 15
61
+ garage_x = 5
62
+ garage_y = 35
63
+ ax.add_patch(patches.Rectangle((garage_x, garage_y), garage_length, garage_width, linewidth=2, edgecolor="orange", facecolor="orange"))
64
+ ax.text(garage_x + garage_length/2, garage_y + garage_width/2, "Garage", fontsize=12, ha="center", va="center", color="black")
65
+
66
+ # Drawing some doors as small rectangles
67
+ ax.add_patch(patches.Rectangle((bedroom_x + 10, bedroom_y), 3, 1, linewidth=2, edgecolor="black", facecolor="brown")) # Bedroom Door
68
+ ax.add_patch(patches.Rectangle((living_room_x + 10, living_room_y), 3, 1, linewidth=2, edgecolor="black", facecolor="brown")) # Living Room Door
69
+ ax.add_patch(patches.Rectangle((kitchen_x + 5, kitchen_y), 3, 1, linewidth=2, edgecolor="black", facecolor="brown")) # Kitchen Door
70
+ ax.add_patch(patches.Rectangle((garage_x + 10, garage_y), 3, 1, linewidth=2, edgecolor="black", facecolor="brown")) # Garage Door
71
+
72
+ # Adding window details (small rectangles representing windows)
73
+ ax.add_patch(patches.Rectangle((bedroom_x + 5, bedroom_y + 15), 5, 1, linewidth=2, edgecolor="blue", facecolor="lightblue")) # Bedroom window
74
+ ax.add_patch(patches.Rectangle((living_room_x + 5, living_room_y + 18), 5, 1, linewidth=2, edgecolor="green", facecolor="lightgreen")) # Living room window
75
+
76
+ # Adding Ventilation (Small lines for windows)
77
+ ax.plot([living_room_x + 20, living_room_x + 22], [living_room_y + 5, living_room_y + 5], color="black", lw=2) # Ventilation Line
78
+
79
+ # Add title and labels
80
+ ax.set_title(f"House Floor Plan: Length = {length} {unit}, Width = {width} {unit}", fontsize=14)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  ax.set_xlabel(f"Length ({unit})")
82
  ax.set_ylabel(f"Width ({unit})")
83
 
84
+ # Set limits and remove axes for clarity
85
  ax.set_xlim(0, length + 10)
86
  ax.set_ylim(0, width + 10)
 
 
87
  ax.axis('off')
88
 
89
  return fig
90
 
91
  # Streamlit interface
92
+ st.title("AutoCAD-style House Layout Drawing")
93
 
94
  # User input for plot dimensions and units
95
  length = st.number_input("Enter the Length of the Plot", min_value=1, max_value=1000, value=50)
96
+ width = st.number_input("Enter the Width of the Plot", min_value=1, max_value=1000, value=30)
97
+
98
+ # Dropdown menu for unit selection
99
+ unit = st.selectbox("Select Unit", ["ft", "m", "cm"])
100
+
101
+ # Draw the house layout when inputs are provided
102
+ if length and width:
103
+ fig = draw_house(length, width, unit)
104
+
105
+ # Display the plot
106
+ st.pyplot(fig)
107
+
108
+ # Option to download the floor plan as a PNG image
109
+ img_data = fig_to_bytes(fig)
110
+ st.download_button("Download Floor Plan", img_data, file_name="house_layout.png", mime="image/png")