Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,41 @@
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
import csv
|
4 |
-
|
5 |
-
DATA_DIR = "data/uploadedImages"
|
6 |
-
RESOURCES_FILE = "Resources.csv"
|
7 |
|
8 |
uploaded_images = {'characters': {}, 'terrain': {}}
|
9 |
|
10 |
def get_image_path(img, name, image_type):
|
11 |
-
file_path =
|
12 |
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
13 |
with open(file_path, "wb") as img_file:
|
14 |
img_file.write(img.getbuffer())
|
15 |
return file_path
|
16 |
|
17 |
-
def
|
18 |
-
|
19 |
-
with open(
|
20 |
csv_writer = csv.writer(csv_file)
|
21 |
-
csv_writer.writerow(
|
22 |
-
|
23 |
-
def get_resources():
|
24 |
-
if os.path.exists(RESOURCES_FILE):
|
25 |
-
with open(RESOURCES_FILE, mode='r') as csv_file:
|
26 |
-
csv_reader = csv.reader(csv_file)
|
27 |
-
return [row for row in csv_reader if row]
|
28 |
-
else:
|
29 |
-
return []
|
30 |
-
|
31 |
-
def display_images(image_type):
|
32 |
-
for name, files in uploaded_images[image_type].items():
|
33 |
-
for file in files:
|
34 |
-
st.sidebar.image(file, width=100, caption=name, use_column_width=True, output_format='jpg', link_url=file)
|
35 |
-
|
36 |
-
def display_terrain():
|
37 |
-
row = []
|
38 |
-
for name, files in uploaded_images['terrain'].items():
|
39 |
-
for file in files:
|
40 |
-
row.append(file)
|
41 |
-
if len(row) == 3:
|
42 |
-
st.image(row, width=100 * 3)
|
43 |
-
row = []
|
44 |
-
if row:
|
45 |
-
st.image(row, width=100 * len(row)) # Last row, if not complete
|
46 |
|
47 |
-
def
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
download_url = f"[Download]({image_url})"
|
64 |
-
st.write(f"| {name} | {file_name_without_extension} | {image_type} | {image_tag} {download_url} |")
|
65 |
-
else:
|
66 |
-
st.write("No images uploaded yet.")
|
67 |
|
68 |
image_type = st.selectbox('Choose image type:', options=['characters', 'terrain'])
|
69 |
name = st.text_input('Enter a name for the image:')
|
@@ -72,15 +44,32 @@ uploaded_files = st.file_uploader('Upload image(s)', type=['png', 'jpg'], accept
|
|
72 |
for uploaded_file in uploaded_files:
|
73 |
if uploaded_file is not None:
|
74 |
# Get actual image file
|
75 |
-
|
76 |
uploaded_images[image_type].setdefault(name, [])
|
77 |
-
uploaded_images[image_type][name].append(
|
78 |
-
st.image(
|
79 |
-
|
80 |
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
import csv
|
4 |
+
import time
|
|
|
|
|
5 |
|
6 |
uploaded_images = {'characters': {}, 'terrain': {}}
|
7 |
|
8 |
def get_image_path(img, name, image_type):
|
9 |
+
file_path = f"data/uploadedImages/{image_type}/{name}/{img.name}"
|
10 |
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
11 |
with open(file_path, "wb") as img_file:
|
12 |
img_file.write(img.getbuffer())
|
13 |
return file_path
|
14 |
|
15 |
+
def update_csv_file(uploaded_file, name, image_type):
|
16 |
+
csv_file_path = "Resources.csv"
|
17 |
+
with open(csv_file_path, mode='a', newline='') as csv_file:
|
18 |
csv_writer = csv.writer(csv_file)
|
19 |
+
csv_writer.writerow([name, uploaded_file.name, image_type])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
def get_uploaded_files_info():
|
22 |
+
csv_file_path = "Resources.csv"
|
23 |
+
with open(csv_file_path, mode='r') as csv_file:
|
24 |
+
csv_reader = csv.reader(csv_file)
|
25 |
+
files_info = []
|
26 |
+
for row in csv_reader:
|
27 |
+
files_info.append(row)
|
28 |
+
return files_info
|
29 |
|
30 |
+
def display_images_from_csv():
|
31 |
+
files_info = get_uploaded_files_info()
|
32 |
+
for row in files_info:
|
33 |
+
if row[2] == 'characters':
|
34 |
+
img_path = f"data/uploadedImages/{row[2]}/{row[0]}/{row[1]}"
|
35 |
+
st.sidebar.image(img_path, width=100, caption=row[0])
|
36 |
+
else:
|
37 |
+
img_path = f"data/uploadedImages/{row[2]}/{row[0]}/{row[1]}"
|
38 |
+
st.image(img_path, width=100, caption=row[0])
|
|
|
|
|
|
|
|
|
39 |
|
40 |
image_type = st.selectbox('Choose image type:', options=['characters', 'terrain'])
|
41 |
name = st.text_input('Enter a name for the image:')
|
|
|
44 |
for uploaded_file in uploaded_files:
|
45 |
if uploaded_file is not None:
|
46 |
# Get actual image file
|
47 |
+
bytes_data = get_image_path(uploaded_file, name, image_type)
|
48 |
uploaded_images[image_type].setdefault(name, [])
|
49 |
+
uploaded_images[image_type][name].append(bytes_data)
|
50 |
+
st.image(bytes_data, use_column_width=True)
|
51 |
+
update_csv_file(uploaded_file, name, image_type)
|
52 |
|
53 |
+
if image_type == 'characters':
|
54 |
+
if uploaded_images['characters']:
|
55 |
+
st.sidebar.write('**Characters**')
|
56 |
+
for name, files in uploaded_images['characters'].items():
|
57 |
+
for file in files:
|
58 |
+
st.sidebar.image(file, width=100, caption=name)
|
59 |
+
else:
|
60 |
+
if uploaded_images['terrain']:
|
61 |
+
st.write('**Terrain**')
|
62 |
+
row = []
|
63 |
+
for name, files in uploaded_images['terrain'].items():
|
64 |
+
for file in files:
|
65 |
+
row.append(file)
|
66 |
+
if len(row) == 3:
|
67 |
+
st.image(row, width=100 * 3)
|
68 |
+
row = []
|
69 |
+
if row:
|
70 |
+
st.image(row, width=100 * len(row)) # Last row, if not complete
|
71 |
|
72 |
+
while True:
|
73 |
+
time.sleep(20)
|
74 |
+
st.empty()
|
75 |
+
display_images_from_csv()
|