File size: 4,627 Bytes
91f00c0
 
 
 
 
 
0d937c2
9e34a83
 
 
0d937c2
 
d00334d
0d937c2
91f00c0
0d937c2
91f00c0
 
 
6c3d176
0d937c2
 
 
d00334d
0d937c2
91f00c0
 
 
 
6c3d176
 
0d937c2
 
 
 
91f00c0
 
 
6c3d176
0d937c2
 
 
91f00c0
 
0d937c2
 
 
 
 
3ba7a88
 
91f00c0
6c3d176
0d937c2
 
91f00c0
38296ed
 
 
0d937c2
 
91f00c0
d00334d
6c3d176
91f00c0
 
0d937c2
 
91f00c0
 
0d937c2
6c3d176
91f00c0
0d937c2
6c3d176
91f00c0
0d937c2
 
 
91f00c0
 
 
 
589867b
91f00c0
23fee40
91f00c0
 
 
0d937c2
589867b
 
91f00c0
 
0d937c2
91f00c0
23fee40
 
 
91f00c0
 
0d937c2
 
 
91f00c0
 
 
 
0d937c2
91f00c0
 
0d937c2
 
91f00c0
6c3d176
 
 
91f00c0
0d937c2
91f00c0
 
 
0d937c2
6c3d176
91f00c0
 
 
0d937c2
91f00c0
 
0d937c2
 
91f00c0
0d937c2
91f00c0
 
 
0d937c2
6c3d176
91f00c0
 
 
 
073a9aa
c87153b
91f00c0
0d937c2
 
 
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
import streamlit as st
import datetime
import streamlit_extras as ste
import utils


def is_valid_new_poll(poll_name):
    if len(poll_name) < 3:
        st.error("Poll name must be at least 3 characters")
        return False
    poll = utils.find_poll(poll_name)
    if poll:
        if poll["expiry"] < datetime.datetime.now():
            utils.load_polls().remove(poll)
        else:
            st.error(f"Poll {poll_name} already exists")
            return False
    return True


def is_valid_join_poll(poll_name):
    poll = utils.find_poll(poll_name)
    if poll:
        if poll["expiry"] < datetime.datetime.now():
            utils.load_polls().remove(poll)
            return False
        else:
            return True
    return False


def is_valid_admin_password(poll_name, admin_password):
    poll = utils.find_poll(poll_name)
    if poll:
        if poll["admin_password"] == admin_password:
            return True
    return False


def join_poll(poll_name, admin_password=""):
    if not is_valid_join_poll(poll_name):
        st.error(f"Invalid poll name {poll_name}")
        return
    if admin_password != "":
        if is_valid_admin_password(poll_name, admin_password):
            st.session_state["selected_poll_admin_password"] = admin_password
        elif "selected_poll_admin_password" in st.session_state:
            del st.session_state["selected_poll_admin_password"]
    st.session_state["selected_poll_name"] = poll_name
    if "poll" not in st.query_params:
        st.query_params.from_dict({"poll": poll_name})


def new_poll(poll_name, admin_password):
    if not is_valid_new_poll(poll_name):
        return
    if len(admin_password) < 8:
        st.error("Admin password must be at least 8 characters")
        return
    poll = {
        "name": poll_name,
        "admin_password": admin_password,
        "expiry": datetime.datetime.now() + datetime.timedelta(hours=3),
        "votes": {},
    }

    utils.load_polls().append(poll)
    join_poll(poll_name, admin_password)


def show_polls():
    st.write("---")
    with st.container(border=True):
        st.header("Poll list:")
        st.button("Reload Polls", use_container_width=True)

        polls = utils.load_polls()
        if len(polls) == 0:
            st.write("No polls found. You can have a better name!")
            return

        table = st.columns(3)
        with table[0]:
            st.subheader("Name")
        with table[1]:
            st.subheader("Expire in")
        with table[2]:
            st.subheader("Join Button")

        for poll in polls:
            if not is_valid_join_poll(poll["name"]):
                continue
            table = st.columns(3)
            with table[0]:
                st.write(poll["name"])
            with table[1]:
                expire_in = poll["expiry"] - datetime.datetime.now()
                formatted_time = '{:02}:{:02}'.format(expire_in.seconds // 3600, (expire_in.seconds // 60) % 60)
                st.text(formatted_time)
            with table[2]:
                st.button(
                    f"Join {poll['name']}",
                    on_click=join_poll,
                    args=(poll["name"],),
                    use_container_width=True,
                )


def show_join_poll(column):
    with column:
        with st.container(border=True):
            poll_name = st.text_input("Join Poll")
            poll_admin_password = ""
            if st.checkbox("Login as Admin", value=False):
                poll_admin_password = st.text_input(
                    "Admin Password", type="password", key="join_admin_password"
                )
            st.button(
                "Join Poll",
                type="primary",
                key="btn_join",
                use_container_width=True,
                on_click=join_poll,
                args=(poll_name, poll_admin_password),
            )


def show_new_poll(column):
    with column:
        with st.container(border=True):
            poll_name = st.text_input("Poll Name")
            poll_admin_password = st.text_input("Admin Password", type="password")
            st.button(
                "New Poll",
                type="secondary",
                key="btn_new",
                use_container_width=True,
                on_click=new_poll,
                args=(poll_name, poll_admin_password),
            )


def load():
    st.title("Audience Engagement Poll")
    st.write("Best way to measure how your audience is doing.")
    columns = st.columns(2, gap="large")
    show_new_poll(columns[0])
    show_join_poll(columns[1])
    show_polls()