File size: 2,754 Bytes
fdfcd02
 
 
3887b17
fdfcd02
 
 
 
 
 
 
 
 
3887b17
fdfcd02
 
 
 
 
 
 
3887b17
 
 
 
 
 
 
 
 
 
fdfcd02
 
 
6ee2107
 
 
fdfcd02
6ee2107
fdfcd02
 
 
6ee2107
fdfcd02
 
 
6ee2107
3887b17
 
 
 
 
 
 
 
 
 
 
6ee2107
 
3887b17
6ee2107
3887b17
 
 
 
 
 
6ee2107
 
3887b17
 
6ee2107
d45b3ad
3887b17
d45b3ad
 
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
import streamlit as st
import csv
import os
import random

def save_data(name, email, phone):
    with open('community.csv', mode='a') as csv_file:
        fieldnames = ['Name', 'Email', 'Phone']
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        if os.stat('community.csv').st_size == 0:
            writer.writeheader()
        writer.writerow({'Name': name, 'Email': email, 'Phone': phone})
    with open(f'{name}.txt', mode='w') as file:
        file.write(f'Name: {name}\nEmail: {email}\nPhone: {phone}\n')

def reset_data():
    os.remove('community.csv')
    for file in os.listdir():
        if file.endswith('.txt'):
            os.remove(file)

def show_data():
    count = 0
    with open('community.csv', mode='r') as csv_file:
        csv_reader = csv.DictReader(csv_file)
        for row in csv_reader:
            count += 1
            if count == 3:
                st.write(f'{row["Name"]}: {row["Email"]} - {row["Phone"]}')
                count = 0

def app():
    st.title('Community Hub Form')

    name = st.text_input('Name', key='name_input')
    email = st.text_input('Email', key='email_input')
    phone = st.text_input('Phone', key='phone_input')

    if st.button('Submit', key='submit_button'):
        save_data(name, email, phone)
        st.success('Form submitted!')

    if st.button('Reset', key='reset_button'):
        reset_data()
        st.success('Data reset!')

    if st.button('Show data', key='show_data_button'):
        show_data()

    st.write('Emails/SMS')
    with open('community.csv', mode='r') as csv_file:
        csv_reader = csv.DictReader(csv_file)
        for row in csv_reader:
            name = row['Name']
            email = row['Email']
            phone = row['Phone']
            st.write(f'{name}: {email} - {phone}')
            if st.button('Reply', key=f'reply_{name}'):
                st.text_input(f'Reply to {name}', key=f'reply_input_{name}')
                if st.button(f'Vote up {name}', key=f'vote_up_{name}'):
                    st.success(f'{name} has been voted up!')
                if st.button(f'No thanks to {name}', key=f'vote_down_{name}'):
                    st.warning(f'{name} has been voted down!')
                    with open(f'{name}.txt', mode='w') as file:
                        file.write(f'Life points: 0\n')
            st.write('')

    st.write('Add tip')
    tip = st.text_input('Tip', key='tip_input')
    if st.button('Submit tip', key='submit_tip_button'):
        emoji = random.choice(['👍', '👌', '👏', '💡'])
        with open(f'{name}.txt', mode='a') as file:
            file.write(f'Tip' + ': {tip} {emoji}\nLife points: 10\n')
        st.success('Tip submitted!')

if __name__ == '__main__':
    app()