Sorter_of_Names / app.py
BarBar288's picture
Update app.py
958a80b verified
import streamlit as st
import random
class WheelOfNames:
def __init__(self, names):
self.names = names
def add_name(self, name):
if name.strip():
self.names.append(name.strip())
st.session_state.name_list_text = "\n".join(self.names)
def upload_file(self):
uploaded_file = st.file_uploader("Upload a text file containing names", type=["txt"])
if uploaded_file:
self.names.extend([line.decode().strip() for line in uploaded_file])
st.session_state.name_list_text = "\n".join(self.names)
def save_file(self):
if self.names:
return "\n".join(self.names)
return None
def clear_names(self):
self.names = []
st.session_state.name_list_text = ""
st.session_state.groups_text = ""
st.session_state.result_label = ""
def sort_into_groups(self, remove_used_names):
group_size = st.session_state.group_size_var
if not group_size.isdigit():
st.error("Please enter a valid group size.")
return
group_size = int(group_size)
if group_size <= 0:
st.error("Group size must be greater than zero.")
return
if not self.names:
st.error("Please add or upload names first.")
return
# Shuffle the names
names_copy = self.names.copy()
random.shuffle(names_copy)
def create_g(list, si):
listy = ""
choices = []
for x in range(si):
choice = random.choice(list)
list.remove(choice)
choices.append(choice)
for y in choices:
listy += f"{y}, "
return listy
groups = []
group_num = 1
while len(names_copy) >= group_size:
group = create_g(names_copy, group_size)
groups.append(f"Group {group_num}: {group}")
group_num += 1
# If there are any remaining names, form a smaller group
if names_copy:
groups.append(names_copy)
# Join groups into a single string
groups_text = "\n".join(f"Group {i}: {', '.join(group)}" for i, group in enumerate(groups, 1))
st.session_state.groups_text = groups_text
if remove_used_names:
# Remove used names from the original list
used_names = [name for group in groups for name in group]
self.names = [name for name in self.names if name not in used_names]
st.session_state.name_list_text = "\n".join(self.names)
def spin_wheel(self):
if not self.names:
st.error("Please add or upload names first")
return
winning_name = random.choice(self.names)
st.session_state.result_label = winning_name
# Ask user if they want to remove the name
remove_name = st.button("Remove Selected Name", key="remove_name_button")
if remove_name:
self.names.remove(winning_name)
st.session_state.name_list_text = "\n".join(self.names)
st.session_state.result_label = ""
def main():
st.title("Wheel of Names")
if 'names' not in st.session_state:
st.session_state.names = []
if 'name_list_text' not in st.session_state:
st.session_state.name_list_text = ""
if 'groups_text' not in st.session_state:
st.session_state.groups_text = ""
if 'result_label' not in st.session_state:
st.session_state.result_label = ""
if 'group_size_var' not in st.session_state:
st.session_state.group_size_var = "2"
if 'remove_used_names' not in st.session_state:
st.session_state.remove_used_names = False
wheel = WheelOfNames(st.session_state.names)
with st.sidebar:
st.subheader("Manage Names")
name_entry = st.text_input("Enter a name")
if st.button("Add Name"):
wheel.add_name(name_entry)
st.session_state.names = wheel.names
wheel.upload_file()
if st.button("Save Names to File"):
names_data = wheel.save_file()
if names_data:
st.download_button(
label="Download Names",
data=names_data,
file_name="names.txt",
mime="text/plain"
)
else:
st.error("No names to save.")
if st.button("Clear Names"):
wheel.clear_names()
st.session_state.names = wheel.names
st.subheader("Names List")
st.text_area("Names List", value=st.session_state.name_list_text, height=110)
st.subheader("Group Size")
group_size_var = st.number_input("Enter group size", min_value=1, value=int(st.session_state.group_size_var))
st.session_state.group_size_var = str(group_size_var)
st.subheader("Sorting Options")
remove_used_names = st.checkbox("Remove Used Names After Sorting", value=st.session_state.remove_used_names)
st.session_state.remove_used_names = remove_used_names
if st.button("Sort into Groups"):
wheel.sort_into_groups(remove_used_names)
st.subheader("Groups")
st.text_area("Groups List", value=st.session_state.groups_text, height=150)
st.subheader("Spin the Wheel")
if st.button("Spin"):
wheel.spin_wheel()
st.subheader("Result")
st.markdown(f"<h1 style='text-align: center;'>{st.session_state.result_label}</h1>", unsafe_allow_html=True)
if __name__ == "__main__":
main()