Kaludi commited on
Commit
83a0bae
1 Parent(s): 3939755

Upload Brain_Entry.py

Browse files
Files changed (1) hide show
  1. Brain_Entry.py +139 -0
Brain_Entry.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import datetime
3
+ import streamlit as st
4
+
5
+ hide_streamlit_style = """
6
+ <style>
7
+ footer {visibility: hidden;}
8
+ </style>
9
+ """
10
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)
11
+
12
+ # Create a 'brain' folder in the current directory if it doesn't exist
13
+ if not os.path.exists("brain"):
14
+ os.makedirs("brain")
15
+
16
+ # Name of the single journal file
17
+ journal_file = "brain/brain_journal.txt"
18
+
19
+ def parse_date(date_string):
20
+ try:
21
+ return datetime.datetime.strptime(date_string, "%m-%d-%Y %A")
22
+ except ValueError:
23
+ return datetime.datetime.strptime(date_string, "%m-%d-%Y")
24
+
25
+ def get_journal_entries():
26
+ entries = []
27
+ if not os.path.exists(journal_file):
28
+ return entries
29
+
30
+ with open(journal_file, "r", encoding="utf-8") as f:
31
+ for line in f:
32
+ if line.startswith("Date: "):
33
+ entry_date = parse_date(line[6:].strip())
34
+ entries.append(entry_date)
35
+ entries.sort(reverse=True)
36
+ return entries
37
+
38
+ def read_entry(date):
39
+ content = ""
40
+ with open(journal_file, "r", encoding="utf-8") as f:
41
+ lines = f.readlines()
42
+
43
+ start_reading = False
44
+ for line in lines:
45
+ if line.startswith("Date: ") and start_reading:
46
+ break
47
+
48
+ if start_reading:
49
+ content += line
50
+
51
+ if line.startswith("Date: ") and date == parse_date(line[6:].strip()):
52
+ start_reading = True
53
+
54
+ return content
55
+
56
+ def write_entry(date, content):
57
+ new_entry = f"\nDate: {date}\n{content}\n\n"
58
+
59
+ # If the journal file does not exist, create it with the new entry
60
+ if not os.path.exists(journal_file):
61
+ with open(journal_file, "w", encoding="utf-8") as f:
62
+ f.write(new_entry)
63
+ else:
64
+ with open(journal_file, "r", encoding="utf-8") as f:
65
+ lines = f.readlines()
66
+
67
+ # Remove existing entry if present
68
+ lines_to_remove = set()
69
+ removing_entry = False
70
+ for i, line in enumerate(lines):
71
+ if line.startswith("Date: "):
72
+ if date == line[6:].strip():
73
+ removing_entry = True
74
+ lines_to_remove.add(i)
75
+ else:
76
+ removing_entry = False
77
+
78
+ if removing_entry:
79
+ lines_to_remove.add(i)
80
+
81
+ lines = [line for i, line in enumerate(lines) if i not in lines_to_remove]
82
+
83
+ # Find the correct position for the new entry based on its date
84
+ new_entry_date = parse_date(date)
85
+ position = None
86
+ for i, line in enumerate(lines):
87
+ if line.startswith("Date: "):
88
+ entry_date = parse_date(line[6:].strip())
89
+ if new_entry_date < entry_date:
90
+ position = i
91
+ break
92
+
93
+ # Insert the new entry at the correct position
94
+ if position is None:
95
+ lines.append(new_entry)
96
+ else:
97
+ lines.insert(position, new_entry)
98
+
99
+ # Write the updated journal entries to the file
100
+ with open(journal_file, "w", encoding="utf-8") as f:
101
+ f.writelines(lines)
102
+
103
+
104
+
105
+ st.title("Digital Brain Journal Entry ✍️")
106
+ st.write("Write a diary journal entry or edit an existing one by selecting on the date picker.")
107
+
108
+ selected_date = st.date_input("Select the date for the journal entry:", value=datetime.date.today())
109
+ formatted_date = selected_date.strftime("%m-%d-%Y %A")
110
+ st.write(f"Selected date: {formatted_date}")
111
+
112
+ entry = ""
113
+
114
+ if selected_date in get_journal_entries():
115
+ entry = read_entry(selected_date)
116
+
117
+ new_entry = st.text_area("Write your journal entry:", entry)
118
+
119
+ if st.button("Submit"):
120
+ write_entry(formatted_date, new_entry)
121
+ st.success("Journal entry saved successfully!")
122
+
123
+ st.header("Previous Journal Entries")
124
+ entries = get_journal_entries()
125
+
126
+ if entries:
127
+ selected_entry_date = st.selectbox("Select an entry to view:", entries, format_func=lambda x: x.strftime("%m-%d-%Y %A"))
128
+
129
+ if st.button("Load Entry"):
130
+ entry_text = read_entry(selected_entry_date)
131
+ st.write(f"**{selected_entry_date.strftime('%m-%d-%Y %A')}**")
132
+ st.markdown(entry_text.replace("\n", "<br>"), unsafe_allow_html=True)
133
+
134
+ else:
135
+ st.write("No previous entries found.")
136
+
137
+ st.markdown("---")
138
+ st.markdown("")
139
+ st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)