Spaces:
Sleeping
Sleeping
Commit
Β·
43e66de
1
Parent(s):
9bd82e6
Update 2026-01-30 09:01:46
Browse files
app.py
CHANGED
|
@@ -25,6 +25,7 @@ def init_db():
|
|
| 25 |
''')
|
| 26 |
cursor.execute("CREATE INDEX IF NOT EXISTS idx_category ON items(category)")
|
| 27 |
cursor.execute("CREATE INDEX IF NOT EXISTS idx_section ON items(section)")
|
|
|
|
| 28 |
conn.commit()
|
| 29 |
conn.close()
|
| 30 |
|
|
@@ -48,7 +49,11 @@ def load_table(category_filter, section_filter, title_filter):
|
|
| 48 |
"""Load and filter table data"""
|
| 49 |
conn = sqlite3.connect(DB_PATH)
|
| 50 |
|
| 51 |
-
query = "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
params = []
|
| 53 |
|
| 54 |
if category_filter:
|
|
@@ -110,43 +115,51 @@ def load_for_edit(df, evt: gr.SelectData):
|
|
| 110 |
row = df.iloc[evt.index[0]]
|
| 111 |
return row['id'], row['category'], row['section'], row['title'], row['content']
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
# Initialize
|
| 114 |
init_db()
|
| 115 |
|
| 116 |
# UI
|
| 117 |
-
with gr.Blocks(title="Text Archive") as app:
|
| 118 |
gr.Markdown("# π Simple Text Archive")
|
| 119 |
|
| 120 |
# Add Form
|
| 121 |
-
gr.Markdown("### β Add New Item")
|
| 122 |
with gr.Row():
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
gr.Markdown("---")
|
| 134 |
|
| 135 |
# Search/Filter
|
| 136 |
gr.Markdown("### π Search & Filter")
|
| 137 |
with gr.Row():
|
| 138 |
-
search_category = gr.Textbox(label="
|
| 139 |
-
search_section = gr.Textbox(label="
|
| 140 |
-
search_title = gr.Textbox(label="
|
| 141 |
-
clear_btn = gr.Button("Clear
|
| 142 |
|
| 143 |
# Table
|
| 144 |
gr.Markdown("### π All Items (Click row to edit)")
|
| 145 |
table = gr.Dataframe(
|
| 146 |
value=load_table("", "", ""),
|
| 147 |
-
label="
|
| 148 |
interactive=False,
|
| 149 |
-
wrap=True
|
|
|
|
| 150 |
)
|
| 151 |
|
| 152 |
gr.Markdown("---")
|
|
@@ -160,21 +173,34 @@ with gr.Blocks(title="Text Archive") as app:
|
|
| 160 |
edit_section = gr.Textbox(label="Section", scale=1)
|
| 161 |
edit_title = gr.Textbox(label="Title", scale=2)
|
| 162 |
|
| 163 |
-
edit_content = gr.Textbox(label="Content", lines=
|
|
|
|
| 164 |
|
| 165 |
with gr.Row():
|
| 166 |
-
update_btn = gr.Button("πΎ
|
| 167 |
-
delete_btn = gr.Button("ποΈ Delete", variant="stop")
|
| 168 |
-
edit_status = gr.Textbox(label="
|
| 169 |
|
| 170 |
# Events
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
add_btn.click(
|
| 172 |
fn=add_item,
|
| 173 |
inputs=[add_category, add_section, add_title, add_content],
|
| 174 |
outputs=[add_status, table]
|
| 175 |
).then(
|
| 176 |
-
fn=lambda: ("", "", "", ""),
|
| 177 |
-
outputs=[add_category, add_section, add_title, add_content]
|
| 178 |
)
|
| 179 |
|
| 180 |
# Auto-search on filter change
|
|
@@ -195,6 +221,10 @@ with gr.Blocks(title="Text Archive") as app:
|
|
| 195 |
fn=load_for_edit,
|
| 196 |
inputs=[table],
|
| 197 |
outputs=[edit_id, edit_category, edit_section, edit_title, edit_content]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
)
|
| 199 |
|
| 200 |
update_btn.click(
|
|
@@ -208,8 +238,8 @@ with gr.Blocks(title="Text Archive") as app:
|
|
| 208 |
inputs=[edit_id],
|
| 209 |
outputs=[edit_status, table]
|
| 210 |
).then(
|
| 211 |
-
fn=lambda: (None, "", "", "", ""),
|
| 212 |
-
outputs=[edit_id, edit_category, edit_section, edit_title, edit_content]
|
| 213 |
)
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
|
|
|
| 25 |
''')
|
| 26 |
cursor.execute("CREATE INDEX IF NOT EXISTS idx_category ON items(category)")
|
| 27 |
cursor.execute("CREATE INDEX IF NOT EXISTS idx_section ON items(section)")
|
| 28 |
+
cursor.execute("CREATE INDEX IF NOT EXISTS idx_title ON items(title)")
|
| 29 |
conn.commit()
|
| 30 |
conn.close()
|
| 31 |
|
|
|
|
| 49 |
"""Load and filter table data"""
|
| 50 |
conn = sqlite3.connect(DB_PATH)
|
| 51 |
|
| 52 |
+
query = """
|
| 53 |
+
SELECT id, category, section, title, content,
|
| 54 |
+
datetime(updated_at, 'localtime') as update
|
| 55 |
+
FROM items WHERE 1=1
|
| 56 |
+
"""
|
| 57 |
params = []
|
| 58 |
|
| 59 |
if category_filter:
|
|
|
|
| 115 |
row = df.iloc[evt.index[0]]
|
| 116 |
return row['id'], row['category'], row['section'], row['title'], row['content']
|
| 117 |
|
| 118 |
+
def get_char_count(text):
|
| 119 |
+
"""Count characters"""
|
| 120 |
+
return f"π {len(text)} chars" if text else "π 0 chars"
|
| 121 |
+
|
| 122 |
# Initialize
|
| 123 |
init_db()
|
| 124 |
|
| 125 |
# UI
|
| 126 |
+
with gr.Blocks(title="Text Archive", theme=gr.themes.Soft()) as app:
|
| 127 |
gr.Markdown("# π Simple Text Archive")
|
| 128 |
|
| 129 |
# Add Form
|
|
|
|
| 130 |
with gr.Row():
|
| 131 |
+
with gr.Column(scale=3):
|
| 132 |
+
gr.Markdown("### β Add New Item")
|
| 133 |
+
with gr.Row():
|
| 134 |
+
add_category = gr.Textbox(label="Category", placeholder="e.g., Python, Notes, SQL", scale=1)
|
| 135 |
+
add_section = gr.Textbox(label="Section", placeholder="e.g., Functions, Snippets", scale=1)
|
| 136 |
+
add_title = gr.Textbox(label="Title", placeholder="Short description", scale=2)
|
| 137 |
+
|
| 138 |
+
add_content = gr.Textbox(label="Content", placeholder="Your text here...", lines=4)
|
| 139 |
+
add_char_count = gr.Markdown("π 0 chars")
|
| 140 |
+
|
| 141 |
+
with gr.Row():
|
| 142 |
+
add_btn = gr.Button("β Add Item", variant="primary", size="sm")
|
| 143 |
+
add_status = gr.Textbox(label="", container=False, show_label=False, interactive=False)
|
| 144 |
|
| 145 |
gr.Markdown("---")
|
| 146 |
|
| 147 |
# Search/Filter
|
| 148 |
gr.Markdown("### π Search & Filter")
|
| 149 |
with gr.Row():
|
| 150 |
+
search_category = gr.Textbox(label="π·οΈ Category", placeholder="Filter...", scale=1)
|
| 151 |
+
search_section = gr.Textbox(label="π Section", placeholder="Filter...", scale=1)
|
| 152 |
+
search_title = gr.Textbox(label="π Title", placeholder="Filter...", scale=1)
|
| 153 |
+
clear_btn = gr.Button("π Clear", size="sm", scale=1)
|
| 154 |
|
| 155 |
# Table
|
| 156 |
gr.Markdown("### π All Items (Click row to edit)")
|
| 157 |
table = gr.Dataframe(
|
| 158 |
value=load_table("", "", ""),
|
| 159 |
+
label="",
|
| 160 |
interactive=False,
|
| 161 |
+
wrap=True,
|
| 162 |
+
column_widths=["5%", "12%", "12%", "20%", "40%", "11%"]
|
| 163 |
)
|
| 164 |
|
| 165 |
gr.Markdown("---")
|
|
|
|
| 173 |
edit_section = gr.Textbox(label="Section", scale=1)
|
| 174 |
edit_title = gr.Textbox(label="Title", scale=2)
|
| 175 |
|
| 176 |
+
edit_content = gr.Textbox(label="Content", lines=6, max_lines=20)
|
| 177 |
+
edit_char_count = gr.Markdown("π 0 chars")
|
| 178 |
|
| 179 |
with gr.Row():
|
| 180 |
+
update_btn = gr.Button("πΎ Save Changes", variant="primary", size="sm")
|
| 181 |
+
delete_btn = gr.Button("ποΈ Delete", variant="stop", size="sm")
|
| 182 |
+
edit_status = gr.Textbox(label="", container=False, show_label=False, interactive=False)
|
| 183 |
|
| 184 |
# Events
|
| 185 |
+
add_content.change(
|
| 186 |
+
fn=get_char_count,
|
| 187 |
+
inputs=add_content,
|
| 188 |
+
outputs=add_char_count
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
edit_content.change(
|
| 192 |
+
fn=get_char_count,
|
| 193 |
+
inputs=edit_content,
|
| 194 |
+
outputs=edit_char_count
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
add_btn.click(
|
| 198 |
fn=add_item,
|
| 199 |
inputs=[add_category, add_section, add_title, add_content],
|
| 200 |
outputs=[add_status, table]
|
| 201 |
).then(
|
| 202 |
+
fn=lambda: ("", "", "", "", "π 0 chars"),
|
| 203 |
+
outputs=[add_category, add_section, add_title, add_content, add_char_count]
|
| 204 |
)
|
| 205 |
|
| 206 |
# Auto-search on filter change
|
|
|
|
| 221 |
fn=load_for_edit,
|
| 222 |
inputs=[table],
|
| 223 |
outputs=[edit_id, edit_category, edit_section, edit_title, edit_content]
|
| 224 |
+
).then(
|
| 225 |
+
fn=get_char_count,
|
| 226 |
+
inputs=edit_content,
|
| 227 |
+
outputs=edit_char_count
|
| 228 |
)
|
| 229 |
|
| 230 |
update_btn.click(
|
|
|
|
| 238 |
inputs=[edit_id],
|
| 239 |
outputs=[edit_status, table]
|
| 240 |
).then(
|
| 241 |
+
fn=lambda: (None, "", "", "", "", "π 0 chars"),
|
| 242 |
+
outputs=[edit_id, edit_category, edit_section, edit_title, edit_content, edit_char_count]
|
| 243 |
)
|
| 244 |
|
| 245 |
if __name__ == "__main__":
|