Update app.py
Browse files
app.py
CHANGED
|
@@ -1,144 +1,78 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import csv
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
uploaded_images = {'characters': {}, 'terrain': {}}
|
| 7 |
|
| 8 |
def get_image_path(img, name, image_type):
|
| 9 |
-
file_path =
|
| 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
|
| 16 |
-
csv_file_path = "Resources.csv"
|
| 17 |
-
# read existing file contents
|
| 18 |
-
file_contents = set()
|
| 19 |
-
if os.path.exists(csv_file_path):
|
| 20 |
-
with open(csv_file_path, mode='r') as csv_file:
|
| 21 |
-
csv_reader = csv.reader(csv_file)
|
| 22 |
-
for row in csv_reader:
|
| 23 |
-
if row:
|
| 24 |
-
file_contents.add(tuple(row))
|
| 25 |
-
# add new file content
|
| 26 |
file_content = (name, uploaded_file.name, image_type)
|
| 27 |
-
|
| 28 |
-
# write updated file contents
|
| 29 |
-
with open(csv_file_path, mode='w', newline='') as csv_file:
|
| 30 |
csv_writer = csv.writer(csv_file)
|
| 31 |
-
|
| 32 |
-
csv_writer.writerow(row)
|
| 33 |
|
| 34 |
-
def
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
with open(csv_file_path, mode='r') as csv_file:
|
| 38 |
csv_reader = csv.reader(csv_file)
|
| 39 |
-
|
| 40 |
-
for row in csv_reader:
|
| 41 |
-
if row:
|
| 42 |
-
files_info.append(row)
|
| 43 |
-
return files_info
|
| 44 |
else:
|
| 45 |
return []
|
| 46 |
|
| 47 |
-
def
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# remove extension from file name
|
| 52 |
-
file_name_without_extension = os.path.splitext(file_name)[0]
|
| 53 |
-
if image_type == 'characters':
|
| 54 |
-
img_path = f"data/uploadedImages/{image_type}/{name}/{file_name}"
|
| 55 |
-
st.sidebar.image(img_path, width=100, caption=name)
|
| 56 |
-
else:
|
| 57 |
-
img_path = f"data/uploadedImages/{image_type}/{name}/{file_name}"
|
| 58 |
-
st.image(img_path, width=100, caption=name)
|
| 59 |
-
|
| 60 |
-
def get_table_from_csv():
|
| 61 |
-
csv_file_path = "Resources.csv"
|
| 62 |
-
if os.path.exists(csv_file_path):
|
| 63 |
-
with open(csv_file_path, mode='r') as csv_file:
|
| 64 |
-
csv_reader = csv.reader(csv_file)
|
| 65 |
-
table_rows = [row for row in csv_reader if row]
|
| 66 |
-
# create markdown table from table rows
|
| 67 |
-
table_md = "| Name | File Name | Image Type |\n| --- | --- | --- |\n"
|
| 68 |
-
for row in table_rows:
|
| 69 |
-
name, file_name, image_type = row
|
| 70 |
-
# remove extension from file name
|
| 71 |
-
file_name_without_extension = os.path.splitext(file_name)[0]
|
| 72 |
-
table_md += f"| {name} | {file_name_without_extension} | {image_type} |\n"
|
| 73 |
-
return table_md
|
| 74 |
-
else:
|
| 75 |
-
return ""
|
| 76 |
-
|
| 77 |
-
# show uploaded images and save them to file
|
| 78 |
-
image_type = st.selectbox('Choose image type:', options=['characters', 'terrain'])
|
| 79 |
-
name = st.text_input('Enter a name for the image:')
|
| 80 |
-
uploaded_files = st.file_uploader('Upload image(s)', type=['png', 'jpg'], accept_multiple_files=True)
|
| 81 |
-
for uploaded_file in uploaded_files:
|
| 82 |
-
if uploaded_file is not None:
|
| 83 |
-
# Get actual image file
|
| 84 |
-
bytes_data = get_image_path(uploaded_file, name, image_type)
|
| 85 |
-
uploaded_images[image_type].setdefault(name, [])
|
| 86 |
-
uploaded_images[image_type][ name].append(bytes_data)
|
| 87 |
-
st.image(bytes_data, use_column_width=True)
|
| 88 |
-
update_csv_file(uploaded_file, name, image_type)
|
| 89 |
-
|
| 90 |
-
def display_characters():
|
| 91 |
-
if uploaded_images['characters']:
|
| 92 |
-
st.sidebar.write('**Characters**')
|
| 93 |
-
for name, files in uploaded_images['characters'].items():
|
| 94 |
-
for file in files:
|
| 95 |
-
st.sidebar.image(file, width=100, caption=name)
|
| 96 |
|
| 97 |
def display_terrain():
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
if row:
|
| 108 |
-
st.image(row, width=100 * len(row)) # Last row, if not complete
|
| 109 |
|
| 110 |
def display_uploaded_images():
|
| 111 |
-
st.write("## Uploaded
|
| 112 |
if image_type == 'characters':
|
| 113 |
-
|
| 114 |
else:
|
| 115 |
display_terrain()
|
| 116 |
|
| 117 |
st.write("## Uploaded Images Table")
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 122 |
else:
|
| 123 |
st.write("No images uploaded yet.")
|
| 124 |
|
| 125 |
-
while True:
|
| 126 |
-
time.sleep(20)
|
| 127 |
-
st.empty()
|
| 128 |
-
display_images_from_csv()
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
# show uploaded images and save them to file
|
| 132 |
image_type = st.selectbox('Choose image type:', options=['characters', 'terrain'])
|
| 133 |
name = st.text_input('Enter a name for the image:')
|
| 134 |
-
uploaded_files = st.file_uploader(
|
|
|
|
| 135 |
for uploaded_file in uploaded_files:
|
| 136 |
if uploaded_file is not None:
|
| 137 |
# Get actual image file
|
| 138 |
-
|
| 139 |
uploaded_images[image_type].setdefault(name, [])
|
| 140 |
-
uploaded_images[image_type][name].append(
|
| 141 |
-
st.image(
|
| 142 |
-
|
| 143 |
|
| 144 |
display_uploaded_images()
|
|
|
|
| 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 = os.path.join(DATA_DIR, image_type, name, img.name)
|
| 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 update_resources_file(uploaded_file, name, image_type):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
file_content = (name, uploaded_file.name, image_type)
|
| 19 |
+
with open(RESOURCES_FILE, mode='a', newline='') as csv_file:
|
|
|
|
|
|
|
| 20 |
csv_writer = csv.writer(csv_file)
|
| 21 |
+
csv_writer.writerow(file_content)
|
|
|
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 display_uploaded_images():
|
| 48 |
+
st.write(f"## Uploaded {image_type.capitalize()}")
|
| 49 |
if image_type == 'characters':
|
| 50 |
+
display_images('characters')
|
| 51 |
else:
|
| 52 |
display_terrain()
|
| 53 |
|
| 54 |
st.write("## Uploaded Images Table")
|
| 55 |
+
resources = get_resources()
|
| 56 |
+
if resources:
|
| 57 |
+
st.write("| Name | File Name | Image Type |")
|
| 58 |
+
st.write("| --- | --- | --- |")
|
| 59 |
+
for name, file_name, image_type in resources:
|
| 60 |
+
file_name_without_extension = os.path.splitext(file_name)[0]
|
| 61 |
+
st.write(f"| {name} | {file_name_without_extension} | {image_type} |")
|
| 62 |
else:
|
| 63 |
st.write("No images uploaded yet.")
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
image_type = st.selectbox('Choose image type:', options=['characters', 'terrain'])
|
| 66 |
name = st.text_input('Enter a name for the image:')
|
| 67 |
+
uploaded_files = st.file_uploader('Upload image(s)', type=['png', 'jpg'], accept_multiple_files=True)
|
| 68 |
+
|
| 69 |
for uploaded_file in uploaded_files:
|
| 70 |
if uploaded_file is not None:
|
| 71 |
# Get actual image file
|
| 72 |
+
file_path = get_image_path(uploaded_file, name, image_type)
|
| 73 |
uploaded_images[image_type].setdefault(name, [])
|
| 74 |
+
uploaded_images[image_type][name].append(file_path)
|
| 75 |
+
st.image(file_path, use_column_width=True)
|
| 76 |
+
update_resources_file(uploaded_file, name, image_type)
|
| 77 |
|
| 78 |
display_uploaded_images()
|