Spaces:
Sleeping
Sleeping
| 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() | |