Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
NOTES_FILE = "notes_storage.txt"
|
| 6 |
+
|
| 7 |
+
# Ensure notes file exists
|
| 8 |
+
if not os.path.exists(NOTES_FILE):
|
| 9 |
+
with open(NOTES_FILE, "w", encoding="utf-8") as f:
|
| 10 |
+
pass
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# -------- Core Functions -------- #
|
| 14 |
+
|
| 15 |
+
def add_note(title, content):
|
| 16 |
+
if title.strip() == "" or content.strip() == "":
|
| 17 |
+
return "β οΈ Title and content cannot be empty!", get_notes()
|
| 18 |
+
|
| 19 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 20 |
+
|
| 21 |
+
formatted_note = f"""
|
| 22 |
+
π Title: {title}
|
| 23 |
+
π Time: {timestamp}
|
| 24 |
+
π Note:
|
| 25 |
+
{content}
|
| 26 |
+
{'-'*50}
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
with open(NOTES_FILE, "a", encoding="utf-8") as f:
|
| 30 |
+
f.write(formatted_note)
|
| 31 |
+
|
| 32 |
+
return "β
Note Added Successfully!", get_notes()
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def get_notes():
|
| 36 |
+
with open(NOTES_FILE, "r", encoding="utf-8") as f:
|
| 37 |
+
data = f.read()
|
| 38 |
+
|
| 39 |
+
return data if data else "β¨ No notes yet. Start writing something amazing!"
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def clear_notes():
|
| 43 |
+
with open(NOTES_FILE, "w", encoding="utf-8") as f:
|
| 44 |
+
f.write("")
|
| 45 |
+
return "ποΈ All notes cleared!", "β¨ No notes yet."
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def download_notes():
|
| 49 |
+
return NOTES_FILE
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# -------- UI Design -------- #
|
| 53 |
+
|
| 54 |
+
with gr.Blocks(
|
| 55 |
+
theme=gr.themes.Soft(
|
| 56 |
+
primary_hue="violet",
|
| 57 |
+
secondary_hue="blue",
|
| 58 |
+
)
|
| 59 |
+
) as demo:
|
| 60 |
+
|
| 61 |
+
gr.Markdown(
|
| 62 |
+
"""
|
| 63 |
+
<h1 style="text-align:center;">π Smart Notes Pro</h1>
|
| 64 |
+
<p style="text-align:center; font-size:18px;">
|
| 65 |
+
Beautiful β’ Organized β’ Simple β’ Powerful
|
| 66 |
+
</p>
|
| 67 |
+
""",
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
with gr.Column(scale=1):
|
| 72 |
+
gr.Markdown("### βοΈ Create a New Note")
|
| 73 |
+
|
| 74 |
+
title_input = gr.Textbox(
|
| 75 |
+
label="Title",
|
| 76 |
+
placeholder="Enter note title..."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
content_input = gr.Textbox(
|
| 80 |
+
label="Content",
|
| 81 |
+
placeholder="Write your note here...",
|
| 82 |
+
lines=8
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
add_btn = gr.Button("β Add Note", variant="primary")
|
| 86 |
+
clear_btn = gr.Button("π Clear All Notes", variant="stop")
|
| 87 |
+
|
| 88 |
+
status_output = gr.Textbox(
|
| 89 |
+
label="Status",
|
| 90 |
+
interactive=False
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
with gr.Column(scale=1):
|
| 94 |
+
gr.Markdown("### π Your Notes")
|
| 95 |
+
|
| 96 |
+
notes_display = gr.Textbox(
|
| 97 |
+
lines=20,
|
| 98 |
+
interactive=False
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
refresh_btn = gr.Button("π Refresh Notes")
|
| 102 |
+
download_btn = gr.DownloadButton("π₯ Download Notes")
|
| 103 |
+
|
| 104 |
+
# Button Interactions
|
| 105 |
+
add_btn.click(
|
| 106 |
+
add_note,
|
| 107 |
+
inputs=[title_input, content_input],
|
| 108 |
+
outputs=[status_output, notes_display]
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
clear_btn.click(
|
| 112 |
+
clear_notes,
|
| 113 |
+
outputs=[status_output, notes_display]
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
refresh_btn.click(
|
| 117 |
+
get_notes,
|
| 118 |
+
outputs=notes_display
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
download_btn.click(
|
| 122 |
+
download_notes,
|
| 123 |
+
outputs=download_btn
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
demo.load(get_notes, outputs=notes_display)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
demo.launch(server_name="0.0.0.0")
|