Note-taking / app.py
Minahel's picture
Update app.py
63e3b8c verified
import gradio as gr
import os
from datetime import datetime
NOTES_FILE = "notes_storage.txt"
# Ensure notes file exists
if not os.path.exists(NOTES_FILE):
with open(NOTES_FILE, "w", encoding="utf-8") as f:
pass
# -------- Core Functions -------- #
def add_note(title, content):
if not title.strip() or not content.strip():
return "⚠️ Title and content cannot be empty!", get_notes()
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
formatted_note = f"""
πŸ—‚ Title: {title}
πŸ•’ Time: {timestamp}
πŸ“ Note:
{content}
{'-'*50}
"""
with open(NOTES_FILE, "a", encoding="utf-8") as f:
f.write(formatted_note)
return "βœ… Note Added Successfully!", get_notes()
def get_notes():
if not os.path.exists(NOTES_FILE):
return "✨ No notes yet."
with open(NOTES_FILE, "r", encoding="utf-8") as f:
data = f.read()
return data if data else "✨ No notes yet. Start writing something amazing!"
def clear_notes():
with open(NOTES_FILE, "w", encoding="utf-8") as f:
f.write("")
return "πŸ—‘οΈ All notes cleared!", "✨ No notes yet."
def download_notes():
return NOTES_FILE
# -------- UI Design -------- #
with gr.Blocks(
theme=gr.themes.Soft(
primary_hue="violet",
secondary_hue="blue",
)
) as demo:
gr.Markdown(
"""
<h1 style="text-align:center;">πŸ“ Smart Notes Pro</h1>
<p style="text-align:center; font-size:18px;">
Beautiful β€’ Organized β€’ Simple β€’ Powerful
</p>
"""
)
with gr.Row():
with gr.Column():
gr.Markdown("### ✍️ Create a New Note")
title_input = gr.Textbox(
label="Title",
placeholder="Enter note title..."
)
content_input = gr.Textbox(
label="Content",
placeholder="Write your note here...",
lines=8
)
add_btn = gr.Button("βž• Add Note", variant="primary")
clear_btn = gr.Button("πŸ—‘ Clear All Notes", variant="stop")
status_output = gr.Textbox(
label="Status",
interactive=False
)
with gr.Column():
gr.Markdown("### πŸ“š Your Notes")
notes_display = gr.Textbox(
lines=20,
interactive=False
)
refresh_btn = gr.Button("πŸ”„ Refresh Notes")
download_btn = gr.DownloadButton("πŸ“₯ Download Notes")
# Button Actions
add_btn.click(
add_note,
inputs=[title_input, content_input],
outputs=[status_output, notes_display]
)
clear_btn.click(
clear_notes,
outputs=[status_output, notes_display]
)
refresh_btn.click(
get_notes,
outputs=notes_display
)
download_btn.click(
download_notes,
outputs=download_btn
)
demo.load(get_notes, outputs=notes_display)
# πŸ”₯ IMPORTANT FIX FOR HUGGING FACE
if __name__ == "__main__":
demo.launch()