Spaces:
Sleeping
Sleeping
File size: 3,763 Bytes
214e401 |
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 117 |
import os
import datetime
import streamlit as st
hide_streamlit_style = """
<style>
footer {visibility: hidden;}
</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", "<br>"), unsafe_allow_html=True)
else:
st.write("No previous entries found.")
st.markdown("---")
st.markdown("")
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)
|