File size: 5,622 Bytes
01511c0
 
46e7303
fd6811c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c99ca8
01511c0
46e7303
01511c0
46e7303
 
 
 
01511c0
46e7303
 
 
01511c0
46e7303
 
4aa53d8
c57d80c
 
958a80b
 
 
 
 
 
 
 
 
 
 
 
4aa53d8
958a80b
231fb18
958a80b
 
 
46e7303
231fb18
 
 
 
63390f4
 
 
46e7303
86bd4e5
7c99ca8
86bd4e5
 
 
 
46e7303
 
01511c0
46e7303
 
01511c0
 
3a2a23c
 
4aa53d8
3a2a23c
 
 
 
46e7303
01511c0
 
46e7303
0979a77
 
4dde2c8
 
 
 
 
 
 
 
43631a1
 
4dde2c8
71ea3b5
01511c0
 
 
 
 
 
0979a77
01511c0
 
e1d146f
 
 
 
 
 
 
 
 
 
 
 
 
01511c0
 
0979a77
01511c0
ea9e68b
3a2a23c
01511c0
 
4dde2c8
01511c0
 
43631a1
 
 
86bd4e5
01511c0
7c99ca8
01511c0
 
4dde2c8
01511c0
 
 
 
 
 
4dde2c8
01511c0
 
 
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
164
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()