| import streamlit as st |
| from datetime import datetime |
| import json |
| import os |
|
|
| |
| DATA_FILE = "diary_entries.json" |
| PASSWORD_FILE = "password.txt" |
|
|
| |
| def load_entries(): |
| if os.path.exists(DATA_FILE): |
| with open(DATA_FILE, "r") as file: |
| return json.load(file) |
| return {} |
|
|
| |
| def save_entries(entries): |
| with open(DATA_FILE, "w") as file: |
| json.dump(entries, file, indent=4) |
|
|
| |
| def load_password(): |
| if os.path.exists(PASSWORD_FILE): |
| with open(PASSWORD_FILE, "r") as file: |
| return file.read().strip() |
| return "diary123" |
|
|
| |
| def save_password(new_password): |
| with open(PASSWORD_FILE, "w") as file: |
| file.write(new_password) |
|
|
| |
| def verify_password(): |
| if "password_verified" not in st.session_state: |
| st.session_state.password_verified = False |
|
|
| if not st.session_state.password_verified: |
| password_input = st.text_input("Enter Password:", type="password") |
| if st.button("Submit"): |
| if password_input == load_password(): |
| st.session_state.password_verified = True |
| st.success("Password verified!") |
| else: |
| st.error("Incorrect password. Try again.") |
| return False |
| return True |
|
|
| |
| def change_password(): |
| st.subheader("Change Password") |
| current_password = st.text_input("Enter Current Password:", type="password") |
| new_password = st.text_input("Enter New Password:", type="password") |
| confirm_password = st.text_input("Confirm New Password:", type="password") |
|
|
| if st.button("Change Password"): |
| if current_password != load_password(): |
| st.error("Current password is incorrect.") |
| elif new_password != confirm_password: |
| st.error("New passwords do not match.") |
| else: |
| save_password(new_password) |
| st.success("Password changed successfully!") |
|
|
| |
| def main(): |
| st.set_page_config(page_title="My Secret Diary", page_icon="π", layout="centered") |
| st.title("π My Secret Diary") |
| st.markdown("Welcome to your personal diary! Keep your thoughts safe and organized.") |
|
|
| |
| if not verify_password(): |
| return |
|
|
| |
| diary_entries = load_entries() |
|
|
| |
| st.sidebar.title("Navigation") |
| option = st.sidebar.radio("Choose an option:", ["Add Entry", "View Entries", "Change Password"]) |
|
|
| if option == "Add Entry": |
| st.subheader("Add a New Diary Entry") |
| selected_date = st.date_input("Select a date", datetime.today()) |
| entry_key = str(selected_date) |
| entry = st.text_area("Write your diary entry here:", value=diary_entries.get(entry_key, ""), height=200) |
|
|
| if st.button("Save Entry"): |
| diary_entries[entry_key] = entry |
| save_entries(diary_entries) |
| st.success("Entry saved successfully!") |
|
|
| elif option == "View Entries": |
| st.subheader("View Past Entries") |
| if not diary_entries: |
| st.info("No entries found. Start by adding a new entry!") |
| else: |
| selected_entry_key = st.selectbox("Select a date to view:", sorted(diary_entries.keys(), reverse=True)) |
| st.write(f"**Entry for {selected_entry_key}:**") |
| st.write(diary_entries[selected_entry_key]) |
|
|
| elif option == "Change Password": |
| change_password() |
|
|
| if __name__ == "__main__": |
| main() |