File size: 4,362 Bytes
6bbbe77
 
 
5f7ff4f
6bbbe77
 
 
 
 
 
5f7ff4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bbbe77
5f7ff4f
6bbbe77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f7ff4f
 
 
 
6bbbe77
 
5f7ff4f
6bbbe77
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
from datetime import datetime
import os

# Initialize session state for temporary and historical storage
if "users" not in st.session_state:
    st.session_state.users = []
if "current_selections" not in st.session_state:
    st.session_state.current_selections = []

# Directory to store history
history_dir = "history"
if not os.path.exists(history_dir):
    os.makedirs(history_dir)

# Load history from existing text files
def load_history():
    history = []
    for filename in os.listdir(history_dir):
        if filename.endswith(".txt"):
            date = filename.split(".txt")[0]
            filepath = os.path.join(history_dir, filename)
            with open(filepath, "r") as file:
                content = file.read()
                summary_df = pd.read_csv(filepath)
                history.append({"Date": date, "Summary": summary_df})
    return history

# Save current summary to a text file
def save_summary_to_file(summary_df):
    timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    filepath = os.path.join(history_dir, f"{timestamp}.txt")
    summary_df.to_csv(filepath, index=False)

# Load history into session state
if "history" not in st.session_state:
    st.session_state.history = load_history()

# Sidebar for navigating through different views
menu = st.sidebar.selectbox("Select View", ["Poll", "History"])

# Function to reset the current selections after submission
def reset_selections():
    st.session_state.users = []
    st.session_state.current_selections = []

# Poll view with four consecutive steps
if menu == "Poll":
    st.title("Breakfast Poll Application")

    # Step 1: User's Name
    if "step" not in st.session_state:
        st.session_state.step = 1

    if st.session_state.step == 1:
        st.header("Step 1: Enter your name")
        name = st.text_input("Name:")
        if st.button("Next") and name:
            st.session_state.users.append(name)
            st.session_state.step = 2

    # Step 2: Select Drinks
    if st.session_state.step == 2:
        st.header("Step 2: Select your drink(s)")
        drinks_options = [
            "Café con leche", "Colacao", "Descafeinado con leche", "Cortado", 
            "Aguasusia", "Aguasusia susia", "Café descafeinado con leche desnatada", 
            "Italiano", "Café con soja", "Té", "Manzanilla", "Nada"
        ]
        selected_drinks = st.multiselect("Choose your drinks:", drinks_options)
        if st.button("Next") and selected_drinks:
            st.session_state.current_selections.append({"Name": st.session_state.users[-1], "Drinks": selected_drinks})
            st.session_state.step = 3

    # Step 3: Select Food
    if st.session_state.step == 3:
        st.header("Step 3: Select your food(s)")
        food_options = [
            "Barrita con aceite", "Barrita con tomate", "Palmera de chocolate", 
            "Palmera de chocolate blanco", "Yogurt", "Pincho de tortilla", "Nada"
        ]
        selected_food = st.multiselect("Choose your food:", food_options)
        if st.button("Next") and selected_food:
            st.session_state.current_selections[-1]["Food"] = selected_food
            st.session_state.step = 4

    # Step 4: Display Summary
    if st.session_state.step == 4:
        st.header("Step 4: Summary of Selections")
        if st.session_state.current_selections:
            df = pd.DataFrame(st.session_state.current_selections)
            st.table(df)

            if st.button("Submit Summary"):
                # Save the current summary to a text file in the history directory
                save_summary_to_file(df)
                
                # Add to session state history
                timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                st.session_state.history.append({"Date": timestamp, "Summary": df})
                
                st.success(f"Summary submitted at {timestamp}")
                reset_selections()
                st.session_state.step = 1

# History view to check past summaries
elif menu == "History":
    st.title("Breakfast Poll History")
    if st.session_state.history:
        for record in st.session_state.history:
            st.subheader(f"Date: {record['Date']}")
            st.table(record["Summary"])
    else:
        st.write("No history records found.")