forked from michabirklbauer/python_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
163 lines (131 loc) · 4.73 KB
/
Copy pathapp.py
File metadata and controls
163 lines (131 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
# SCRIPT NAME - GUI
# 2026 (c) YOUR NAME
# https://github.com/username/
# your.mail@mail.com
## disable unused variable checks for streamlit variables
# ruff: noqa: F841
"""
#####################################################
## ##
## -- STREAMLIT MAIN APP -- ##
## ##
#####################################################
"""
import os
from tempfile import NamedTemporaryFile
import streamlit as st
from streamlit.runtime.uploaded_file_manager import UploadedFile
from main import Character, character_factory, battle
@st.cache_data
def read_characters(
uploaded_file: UploadedFile,
) -> list[Character]:
r"""
Reads an uploaded character file.
"""
with NamedTemporaryFile(
suffix=os.path.splitext(uploaded_file.name)[1], delete_on_close=False
) as f:
f.write(uploaded_file.getbuffer())
f.close()
return character_factory(f.name)
# main page content
def main_page():
r"""
The Streamlit main page.
"""
title = st.title("TITLE")
general_description = """
DESCRIPTION
"""
description = st.markdown(general_description)
header_1 = st.subheader("Battle Simulator", divider="rainbow")
uploaded_file = st.file_uploader(
"Upload a Character file:",
type=["csv"],
accept_multiple_files=False,
key="uploaded_file",
help="Upload a csv file containing character information of at least two characters.",
)
health = st.number_input(
"Character health:",
min_value=1.0,
max_value=10000.0,
value=130.0,
step=1.0,
help="How many hit points the characters have.",
)
if "astarion" not in st.session_state:
st.session_state["astarion"] = 0
if "shadowheart" not in st.session_state:
st.session_state["shadowheart"] = 0
total_wins = st.markdown(
f"Astarion won a total of {st.session_state['astarion']} times and Shadowheart won a total of {st.session_state['shadowheart']} times!"
)
l1, l2, center, r1, r2 = st.columns(5)
with center:
compute = st.button("Battle!", type="secondary", width="stretch")
if compute:
if uploaded_file is not None:
characters = read_characters(uploaded_file)
if len(characters) < 2:
st.error("You need to upload a file with at least two characters!")
else:
winner = battle(characters[0], characters[1], health=float(health))
if winner.name == "Astarion":
st.session_state["astarion"] += 1
st.success("Astarion won the battle!")
with center:
st.image(
"https://bg3.wiki/w/images/3/3c/Astarion.png",
caption="(c) Larian Studios",
width="stretch",
)
if winner.name == "Shadowheart":
st.session_state["shadowheart"] += 1
st.success("Shadowheart won the battle!")
with center:
st.image(
"https://bg3.wiki/w/images/f/f9/Shadowheart.png",
caption="(c) Larian Studios",
width="stretch",
)
else:
st.error("You need to upload a file with at least two characters!")
# side bar and main page loader
def main():
r"""
Streamlit app main function.
"""
about_str = """
ABOUT
"""
st.set_page_config(
page_title="TITLE",
page_icon=":test_tube:",
layout="wide",
initial_sidebar_state="expanded",
menu_items={
"Get Help": "https://github.com/YOUR_REPO/discussions",
"Report a bug": "https://github.com/YOUR_REPO/issues",
"About": about_str,
},
)
title = st.sidebar.title("TITLE")
logo = st.sidebar.image(
"https://baldursgate3.game/png/logo-bg3.png",
caption="(c) Larian Studios",
)
doc = st.sidebar.markdown(about_str)
citing_str = "**Citing:**\n- CITATION INFO"
citing = st.sidebar.markdown(citing_str)
contact_str = "**Contact:**\n- CONTACT INFO"
contact = st.sidebar.markdown(contact_str)
project_license_str = "**License:**\n- [MIT License](https://github.com/YOUR_REPO/blob/master/LICENSE.md)"
project_license = st.sidebar.markdown(project_license_str)
project_str = "**Project Page:**\n- [GitHub](https://github.com/YOUR_REPO/)"
project = st.sidebar.markdown(project_str)
main_page()
if __name__ == "__main__":
main()