File size: 2,228 Bytes
bd75eb9
 
0ab2e61
b90ed81
bd75eb9
 
 
 
 
55021e9
 
 
 
b90ed81
 
 
bd75eb9
 
 
55021e9
bd75eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
908f2bb
 
55021e9
908f2bb
 
 
55021e9
bd75eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from datetime import datetime
import os

# Create a directory to store messages
messages_dir = "messages"
if not os.path.exists(messages_dir):
    os.makedirs(messages_dir)

# Initialize session state to store the list of threads
if 'thread_list' not in st.session_state:
    st.session_state.thread_list = []

# Create a dictionary to store messages and threads
messages = {}

def create_thread(thread_id, user_input):
    thread = {'messages': [user_input], 'created_at': datetime.now()}
    messages[thread_id] = thread
    st.session_state.thread_list.append(thread_id)

def add_message(thread_id, user_input):
    if thread_id in messages:
        messages[thread_id]['messages'].append(user_input)
    else:
        create_thread(thread_id, user_input)

def save_message_to_file(thread_id, message):
    timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
    file_name = f"{messages_dir}/{thread_id}_{timestamp}.txt"
    with open(file_name, 'w') as file:
        file.write(message)

# Main app
def main():
    st.title("Free Board with Streamlit")

    # User input for creating a new thread
    new_thread = st.text_input("Create a new thread:", key='new_thread_input')
    if new_thread:
        create_thread(new_thread, "Thread created.")
        st.success(f"Thread '{new_thread}' created!")

    # Display existing threads
    st.subheader("Existing Threads")
    for thread_id in st.session_state.thread_list:
        st.write(f"- {thread_id}")

    # Allow user to select a thread and add messages
    thread_to_post = st.selectbox("Select a thread to post:", st.session_state.thread_list, key='thread_to_post')
    if thread_to_post:
        user_input = st.text_area("Write your message:", key='user_input')
        if user_input:
            add_message(thread_to_post, user_input)
            save_message_to_file(thread_to_post, user_input)  # Save the message to a file
            st.success("Message added!")

    # Display threads and their messages
    for thread_id, thread_data in messages.items():
        st.subheader(f"Thread: {thread_id}")
        for message in thread_data['messages']:
            st.write(f"- {message}")
    
if __name__ == "__main__":
    main()