import os
import datetime
import streamlit as st
hide_streamlit_style = """
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
# Create a 'diary' folder in the current directory if it doesn't exist
if not os.path.exists("diary"):
os.makedirs("diary")
# Name of the single journal file
journal_file = "diary/diary_journal.txt"
def parse_date(date_string):
try:
return datetime.datetime.strptime(date_string, "%m-%d-%Y %A")
except ValueError:
return datetime.datetime.strptime(date_string, "%m-%d-%Y")
def get_journal_entries():
entries = []
if not os.path.exists(journal_file):
return entries
with open(journal_file, "r", encoding="utf-8") as f:
for line in f:
if line.startswith("Date: "):
entry_date = parse_date(line[6:].strip())
entries.append(entry_date)
entries.sort(reverse=True)
return entries
def read_entry(date):
content = ""
with open(journal_file, "r", encoding="utf-8") as f:
lines = f.readlines()
start_reading = False
for line in lines:
if line.startswith("Date: ") and start_reading:
break
if start_reading:
content += line
if line.startswith("Date: ") and date == parse_date(line[6:].strip()):
start_reading = True
return content
def write_entry(date, content):
new_entry = f"\nDate: {date}\n{content}\n"
# Check if the entry already exists
entry_exists = False
if os.path.exists(journal_file):
with open(journal_file, "r", encoding="utf-8") as f:
lines = f.readlines()
entry_exists = any(line.strip() == f"Date: {date}" for line in lines)
# If the entry does not exist, append the new entry to the end of the file
if not entry_exists:
with open(journal_file, "a", encoding="utf-8") as f:
f.write(new_entry)
else:
# If the entry exists, update the existing entry
with open(journal_file, "r", encoding="utf-8") as f:
lines = f.readlines()
# Remove existing entry if present
lines = [line for line in lines if line.strip() != f"Date: {date}"]
with open(journal_file, "w", encoding="utf-8") as f:
f.writelines(lines)
f.write(new_entry)
st.title("Digital Brain Journal Entry ✍️")
st.write("Write a diary journal entry or edit an existing one by selecting on the date picker.")
selected_date = st.date_input("Select the date for the journal entry:", value=datetime.date.today())
formatted_date = selected_date.strftime("%m-%d-%Y %A")
st.write(f"Selected date: {formatted_date}")
entry = ""
if selected_date in get_journal_entries():
entry = read_entry(selected_date)
new_entry = st.text_area("Write your journal entry:", entry)
if st.button("Submit"):
write_entry(formatted_date, new_entry)
st.success("Journal entry saved successfully!")
st.header("Previous Journal Entries")
entries = get_journal_entries()
if entries:
selected_entry_date = st.selectbox("Select an entry to view or edit:", entries, format_func=lambda x: x.strftime("%m-%d-%Y %A"))
if st.button("Load Entry"):
entry_text = read_entry(selected_entry_date)
st.write(f"**{selected_entry_date.strftime('%m-%d-%Y %A')}**")
st.markdown(entry_text.replace("\n", "
"), unsafe_allow_html=True)
else:
st.write("No previous entries found.")
st.markdown("---")
st.markdown("")
st.markdown("