CoolFace
Apppublic

BarBar288/Sorter_of_Names

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
app.py164 linesDownload Raw Back to root
1import streamlit as st2import random3 4class WheelOfNames:5    def __init__(self, names):6        self.names = names7 8    def add_name(self, name):9        if name.strip():10            self.names.append(name.strip())11            st.session_state.name_list_text = "\n".join(self.names)12 13    def upload_file(self):14        uploaded_file = st.file_uploader("Upload a text file containing names", type=["txt"])15        if uploaded_file:16            self.names.extend([line.decode().strip() for line in uploaded_file])17            st.session_state.name_list_text = "\n".join(self.names)18 19    def save_file(self):20        if self.names:21            return "\n".join(self.names)22        return None23 24    def clear_names(self):25        self.names = []26        st.session_state.name_list_text = ""27        st.session_state.groups_text = ""28        st.session_state.result_label = ""29 30    def sort_into_groups(self, remove_used_names):31        group_size = st.session_state.group_size_var32        if not group_size.isdigit():33            st.error("Please enter a valid group size.")34            return35 36        group_size = int(group_size)37        if group_size <= 0:38            st.error("Group size must be greater than zero.")39            return40 41        if not self.names:42            st.error("Please add or upload names first.")43            return44 45        # Shuffle the names46        names_copy = self.names.copy()47        random.shuffle(names_copy)48        def create_g(list, si):49            listy = ""50            choices = []51            for x in range(si):52                choice = random.choice(list) 53                list.remove(choice)54                choices.append(choice)55            for y in choices:56                listy += f"{y}, "57            return listy58                59                60        groups = []61        group_num = 162        while len(names_copy) >= group_size:63            group = create_g(names_copy, group_size)64            groups.append(f"Group {group_num}: {group}")65            group_num += 166 67        # If there are any remaining names, form a smaller group68        if names_copy:69            groups.append(names_copy)70 71        # Join groups into a single string72        groups_text = "\n".join(f"Group {i}: {', '.join(group)}" for i, group in enumerate(groups, 1))73        st.session_state.groups_text = groups_text74 75        if remove_used_names:76            # Remove used names from the original list77            used_names = [name for group in groups for name in group]78            self.names = [name for name in self.names if name not in used_names]79            st.session_state.name_list_text = "\n".join(self.names)80 81    def spin_wheel(self):82        if not self.names:83            st.error("Please add or upload names first")84            return85 86        winning_name = random.choice(self.names)87        st.session_state.result_label = winning_name88 89        # Ask user if they want to remove the name90        remove_name = st.button("Remove Selected Name", key="remove_name_button")91        if remove_name:92            self.names.remove(winning_name)93            st.session_state.name_list_text = "\n".join(self.names)94            st.session_state.result_label = ""95 96def main():97    st.title("Wheel of Names")98 99    if 'names' not in st.session_state:100        st.session_state.names = []101    if 'name_list_text' not in st.session_state:102        st.session_state.name_list_text = ""103    if 'groups_text' not in st.session_state:104        st.session_state.groups_text = ""105    if 'result_label' not in st.session_state:106        st.session_state.result_label = ""107    if 'group_size_var' not in st.session_state:108        st.session_state.group_size_var = "2"109    if 'remove_used_names' not in st.session_state:110        st.session_state.remove_used_names = False111 112    wheel = WheelOfNames(st.session_state.names)113 114    with st.sidebar:115        st.subheader("Manage Names")116        name_entry = st.text_input("Enter a name")117        if st.button("Add Name"):118            wheel.add_name(name_entry)119            st.session_state.names = wheel.names120 121        wheel.upload_file()122 123        if st.button("Save Names to File"):124            names_data = wheel.save_file()125            if names_data:126                st.download_button(127                    label="Download Names",128                    data=names_data,129                    file_name="names.txt",130                    mime="text/plain"131                )132            else:133                st.error("No names to save.")134 135        if st.button("Clear Names"):136            wheel.clear_names()137            st.session_state.names = wheel.names138 139        st.subheader("Names List")140        st.text_area("Names List", value=st.session_state.name_list_text, height=110)141 142    st.subheader("Group Size")143    group_size_var = st.number_input("Enter group size", min_value=1, value=int(st.session_state.group_size_var))144    st.session_state.group_size_var = str(group_size_var)145 146    st.subheader("Sorting Options")147    remove_used_names = st.checkbox("Remove Used Names After Sorting", value=st.session_state.remove_used_names)148    st.session_state.remove_used_names = remove_used_names149 150    if st.button("Sort into Groups"):151        wheel.sort_into_groups(remove_used_names)152 153    st.subheader("Groups")154    st.text_area("Groups List", value=st.session_state.groups_text, height=150)155 156    st.subheader("Spin the Wheel")157    if st.button("Spin"):158        wheel.spin_wheel()159 160    st.subheader("Result")161    st.markdown(f"<h1 style='text-align: center;'>{st.session_state.result_label}</h1>", unsafe_allow_html=True)162 163if __name__ == "__main__":164    main()