awacke1 commited on
Commit
46a9188
โ€ข
1 Parent(s): a09d912

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -9
app.py CHANGED
@@ -4,6 +4,7 @@ from PIL import Image
4
  import random
5
  from io import BytesIO
6
  import datetime
 
7
 
8
  # Adjust Streamlit layout to wide mode
9
  st.set_page_config(layout="wide")
@@ -83,6 +84,11 @@ def arrange_images(image_files, canvas_size=(3000, 3000)):
83
  buffer.seek(0)
84
  return buffer, canvas, room_details
85
 
 
 
 
 
 
86
  # Sidebar Title
87
  st.sidebar.markdown("#### ๐Ÿฐ Dynamic Dungeon Map Generator")
88
 
@@ -97,6 +103,8 @@ if "canvas" not in st.session_state:
97
  st.session_state["canvas"] = None
98
  if "room_details" not in st.session_state:
99
  st.session_state["room_details"] = []
 
 
100
 
101
  # Generate map if layout_image is empty
102
  if st.session_state["layout_image"] is None:
@@ -110,15 +118,26 @@ if st.session_state["layout_image"] is None:
110
  # Sidebar Controls
111
  if st.sidebar.button("๐Ÿ’พ Save Map"):
112
  now = datetime.datetime.now()
113
- filename = f"dungeon_map_{now.strftime('%Y%m%d_%H%M%S')}.png"
114
- st.session_state["canvas"].save(filename)
115
- st.sidebar.success(f"Map saved as {filename}")
 
 
 
 
 
 
 
 
 
116
 
117
- # Save room details as text
118
- layout_filename = f"room_layout_{now.strftime('%Y%m%d_%H%M%S')}.txt"
119
- with open(layout_filename, "w") as f:
120
- f.write("\n".join(st.session_state["room_details"]))
121
- st.sidebar.success(f"Room layout saved as {layout_filename}")
 
 
122
 
123
  if st.sidebar.button("๐Ÿ—บ๏ธ Regenerate Map"):
124
  image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
@@ -129,10 +148,15 @@ if st.sidebar.button("๐Ÿ—บ๏ธ Regenerate Map"):
129
  st.session_state["room_details"] = room_details
130
  st.rerun()
131
 
 
 
 
 
 
132
  # Zoom Controls
133
  st.sidebar.title("Zoom")
134
  show_zoomed = st.sidebar.checkbox("Show Zoomed Version")
135
- zoom_level = st.sidebar.slider("Zoom Level", min_value=0.5, max_value=2.0, value=1.0, step=0.1)
136
 
137
  if show_zoomed and st.session_state["canvas"] is not None:
138
  zoomed_canvas = st.session_state["canvas"].resize(
 
4
  import random
5
  from io import BytesIO
6
  import datetime
7
+ import base64
8
 
9
  # Adjust Streamlit layout to wide mode
10
  st.set_page_config(layout="wide")
 
84
  buffer.seek(0)
85
  return buffer, canvas, room_details
86
 
87
+ # Function to create a base64 link
88
+ def create_base64_download_link(file_content, filename, file_type):
89
+ b64 = base64.b64encode(file_content.encode() if file_type == "txt" else file_content).decode()
90
+ return f"[๐Ÿ“ฅ Download {filename}](data:application/{file_type};base64,{b64})"
91
+
92
  # Sidebar Title
93
  st.sidebar.markdown("#### ๐Ÿฐ Dynamic Dungeon Map Generator")
94
 
 
103
  st.session_state["canvas"] = None
104
  if "room_details" not in st.session_state:
105
  st.session_state["room_details"] = []
106
+ if "saved_files" not in st.session_state:
107
+ st.session_state["saved_files"] = []
108
 
109
  # Generate map if layout_image is empty
110
  if st.session_state["layout_image"] is None:
 
118
  # Sidebar Controls
119
  if st.sidebar.button("๐Ÿ’พ Save Map"):
120
  now = datetime.datetime.now()
121
+ filename_png = f"dungeon_map_{now.strftime('%Y%m%d_%H%M%S')}.png"
122
+ filename_txt = f"room_layout_{now.strftime('%Y%m%d_%H%M%S')}.txt"
123
+
124
+ # Save the PNG map
125
+ st.session_state["canvas"].save(filename_png)
126
+ st.sidebar.success(f"Map saved as {filename_png}")
127
+
128
+ # Save room details as a text file
129
+ room_details_content = "\n".join(st.session_state["room_details"])
130
+ with open(filename_txt, "w") as f:
131
+ f.write(room_details_content)
132
+ st.sidebar.success(f"Room layout saved as {filename_txt}")
133
 
134
+ # Generate base64 download links
135
+ with open(filename_png, "rb") as f:
136
+ png_base64 = create_base64_download_link(f.read(), filename_png, "octet-stream")
137
+ txt_base64 = create_base64_download_link(room_details_content, filename_txt, "txt")
138
+
139
+ # Add files to save history
140
+ st.session_state["saved_files"].append((png_base64, txt_base64))
141
 
142
  if st.sidebar.button("๐Ÿ—บ๏ธ Regenerate Map"):
143
  image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
 
148
  st.session_state["room_details"] = room_details
149
  st.rerun()
150
 
151
+ # Display save history in the sidebar
152
+ st.sidebar.markdown("### ๐Ÿ“œ Save History")
153
+ for idx, (png_link, txt_link) in enumerate(st.session_state["saved_files"], start=1):
154
+ st.sidebar.markdown(f"{idx}. {png_link} | {txt_link}")
155
+
156
  # Zoom Controls
157
  st.sidebar.title("Zoom")
158
  show_zoomed = st.sidebar.checkbox("Show Zoomed Version")
159
+ zoom_level = st.sidebar.slider("Zoom Level", min_value=0.1, max_value=2.0, value=1.0, step=0.1)
160
 
161
  if show_zoomed and st.session_state["canvas"] is not None:
162
  zoomed_canvas = st.session_state["canvas"].resize(