2023-02-26 16:17:57 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import gradio as gr
|
|
|
|
|
2023-02-27 17:41:00 +01:00
|
|
|
from modules.html_generator import get_image_cache
|
2023-02-26 16:37:26 +01:00
|
|
|
|
2023-02-26 16:17:57 +01:00
|
|
|
|
|
|
|
def generate_html():
|
|
|
|
css = """
|
2023-02-27 14:52:43 +01:00
|
|
|
.character-gallery {
|
|
|
|
margin: 1rem 0;
|
|
|
|
display: grid;
|
2023-02-27 15:30:52 +01:00
|
|
|
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
2023-02-27 14:52:43 +01:00
|
|
|
grid-column-gap: 0.4rem;
|
|
|
|
grid-row-gap: 1.2rem;
|
|
|
|
}
|
|
|
|
|
|
|
|
.character-container {
|
|
|
|
cursor: pointer;
|
|
|
|
text-align: center;
|
|
|
|
position: relative;
|
2023-02-27 15:55:36 +01:00
|
|
|
opacity: 0.85;
|
2023-02-27 14:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
.character-container:hover {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
.character-container .placeholder, .character-container img {
|
|
|
|
width: 150px;
|
|
|
|
height: 200px;
|
|
|
|
background-color: gray;
|
|
|
|
object-fit: cover;
|
|
|
|
margin: 0 auto;
|
|
|
|
border-radius: 1rem;
|
|
|
|
border: 3px solid white;
|
2023-02-27 15:55:36 +01:00
|
|
|
box-shadow: 5px 5px 8px 0px rgb(0 0 0 / 50%);
|
2023-02-27 14:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
.character-name {
|
|
|
|
margin-top: 0.2rem;
|
|
|
|
display: block;
|
|
|
|
font-size: 1.2rem;
|
|
|
|
font-weight: 600;
|
2023-02-27 15:46:15 +01:00
|
|
|
overflow-wrap: anywhere;
|
2023-02-27 14:52:43 +01:00
|
|
|
}
|
2023-02-26 16:17:57 +01:00
|
|
|
"""
|
|
|
|
|
2023-02-27 14:52:43 +01:00
|
|
|
container_html = f'<style>{css}</style><div class="character-gallery">'
|
2023-02-26 16:17:57 +01:00
|
|
|
|
|
|
|
# Iterate through files in image folder
|
2023-02-27 17:04:06 +01:00
|
|
|
for file in sorted(Path("characters").glob("*")):
|
2023-02-26 16:17:57 +01:00
|
|
|
if file.name.endswith(".json"):
|
2023-02-26 16:37:26 +01:00
|
|
|
character = file.name.replace(".json", "")
|
2023-02-27 14:52:43 +01:00
|
|
|
container_html += f'<div class="character-container" onclick=\'document.getElementById("character-menu").children[1].children[1].value = "{character}"; document.getElementById("character-menu").children[1].children[1].dispatchEvent(new Event("change"));\'>'
|
2023-02-26 16:37:26 +01:00
|
|
|
image_html = "<div class='placeholder'></div>"
|
|
|
|
|
|
|
|
for i in [
|
|
|
|
f"characters/{character}.png",
|
|
|
|
f"characters/{character}.jpg",
|
|
|
|
f"characters/{character}.jpeg",
|
|
|
|
]:
|
|
|
|
|
|
|
|
path = Path(i)
|
|
|
|
if path.exists():
|
|
|
|
try:
|
2023-02-27 17:41:00 +01:00
|
|
|
image_html = f'<img src="file/{get_image_cache(path)}">'
|
2023-02-26 16:37:26 +01:00
|
|
|
break
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
2023-02-27 14:52:43 +01:00
|
|
|
container_html += f'{image_html} <span class="character-name">{character}</span>'
|
|
|
|
container_html += "</div>"
|
2023-02-26 16:17:57 +01:00
|
|
|
|
2023-02-27 14:52:43 +01:00
|
|
|
container_html += "</div>"
|
|
|
|
return container_html
|
2023-02-26 16:17:57 +01:00
|
|
|
|
|
|
|
def ui():
|
|
|
|
with gr.Accordion("Character gallery"):
|
|
|
|
update = gr.Button("Refresh")
|
|
|
|
gallery = gr.HTML(value=generate_html())
|
|
|
|
update.click(generate_html, [], gallery)
|