awacke1's picture
Update app.py
6ee2107
raw
history blame
3.09 kB
import streamlit as st
import csv
import os
import random
# Define function to save form data as text file
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')
# Define function to reset form data
def reset_data():
os.remove('community.csv')
for file in os.listdir():
if file.endswith('.txt'):
os.remove(file)
# Define function to show data after three views
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
# Define Streamlit app
def app():
st.title('Community Hub Form')
# Get form inputs
name = st.text_input('Name', key='name_input')
email = st.text_input('Email', key='email_input')
phone = st.text_input('Phone', key='phone_input')
# Save form data when user submits
if st.button('Submit', key='submit_button'):
save_data(name, email, phone)
st.success('Form submitted!')
# Reset form data when user clicks button
if st.button('Reset', key='reset_button'):
reset_data()
st.success('Data reset!')
# Show data when user clicks button
if st.button('Show data', key='show_data_button'):
show_data()
# Reply and vote buttons
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('')
# Add tip and emoji
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()