Create backup-app.py
Browse files- backup-app.py +75 -0
backup-app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:')
|
| 42 |
+
uploaded_files = st.file_uploader('Upload image(s)', type=['png', 'jpg'], accept_multiple_files=True)
|
| 43 |
+
|
| 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()
|