Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -7,27 +7,41 @@ import sqlite3
|
|
7 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
8 |
|
9 |
# Initialize SQLite database
|
10 |
-
|
11 |
-
c = conn.cursor()
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
conn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# Function to get all links from the database
|
19 |
def get_links():
|
|
|
|
|
20 |
c.execute("SELECT * FROM links")
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
if not get_links():
|
25 |
-
initial_urls = [
|
26 |
-
{"id": str(uuid.uuid4()), "name": "Google", "url": "https://www.google.com"},
|
27 |
-
{"id": str(uuid.uuid4()), "name": "Bing", "url": "https://www.bing.com"},
|
28 |
-
]
|
29 |
-
c.executemany("INSERT INTO links VALUES (:id, :name, :url)", initial_urls)
|
30 |
-
conn.commit()
|
31 |
|
32 |
app.layout = dbc.Container([
|
33 |
dbc.Row([
|
@@ -56,9 +70,12 @@ app.layout = dbc.Container([
|
|
56 |
def update_url_list(n_clicks, new_name, new_link):
|
57 |
ctx = dash.callback_context
|
58 |
if ctx.triggered_id == 'add-url-button' and new_name and new_link:
|
|
|
|
|
59 |
new_id = str(uuid.uuid4())
|
60 |
c.execute("INSERT INTO links VALUES (?, ?, ?)", (new_id, new_name, new_link))
|
61 |
conn.commit()
|
|
|
62 |
|
63 |
links = get_links()
|
64 |
return [
|
@@ -88,10 +105,13 @@ def update_iframe(n_clicks):
|
|
88 |
return dash.no_update
|
89 |
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
90 |
clicked_id = eval(button_id)['index']
|
|
|
|
|
91 |
c.execute("SELECT url FROM links WHERE id = ?", (clicked_id,))
|
92 |
result = c.fetchone()
|
|
|
93 |
if result:
|
94 |
-
return result[
|
95 |
return dash.no_update
|
96 |
|
97 |
@callback(
|
@@ -105,8 +125,11 @@ def delete_url(n_clicks):
|
|
105 |
return dash.no_update
|
106 |
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
107 |
delete_id = eval(button_id)['index']
|
|
|
|
|
108 |
c.execute("DELETE FROM links WHERE id = ?", (delete_id,))
|
109 |
conn.commit()
|
|
|
110 |
return update_url_list(None, None, None)
|
111 |
|
112 |
if __name__ == '__main__':
|
|
|
7 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
8 |
|
9 |
# Initialize SQLite database
|
10 |
+
DB_FILE = 'links.db'
|
|
|
11 |
|
12 |
+
def get_db_connection():
|
13 |
+
conn = sqlite3.connect(DB_FILE, check_same_thread=False)
|
14 |
+
conn.row_factory = sqlite3.Row
|
15 |
+
return conn
|
16 |
+
|
17 |
+
def init_db():
|
18 |
+
conn = get_db_connection()
|
19 |
+
c = conn.cursor()
|
20 |
+
c.execute('''CREATE TABLE IF NOT EXISTS links
|
21 |
+
(id TEXT PRIMARY KEY, name TEXT, url TEXT)''')
|
22 |
+
conn.commit()
|
23 |
+
|
24 |
+
# Initialize with some example URLs if the table is empty
|
25 |
+
c.execute("SELECT COUNT(*) FROM links")
|
26 |
+
if c.fetchone()[0] == 0:
|
27 |
+
initial_urls = [
|
28 |
+
{"id": str(uuid.uuid4()), "name": "Google", "url": "https://www.google.com"},
|
29 |
+
{"id": str(uuid.uuid4()), "name": "Bing", "url": "https://www.bing.com"},
|
30 |
+
]
|
31 |
+
c.executemany("INSERT INTO links VALUES (:id, :name, :url)", initial_urls)
|
32 |
+
conn.commit()
|
33 |
+
conn.close()
|
34 |
+
|
35 |
+
init_db()
|
36 |
|
37 |
# Function to get all links from the database
|
38 |
def get_links():
|
39 |
+
conn = get_db_connection()
|
40 |
+
c = conn.cursor()
|
41 |
c.execute("SELECT * FROM links")
|
42 |
+
links = [dict(row) for row in c.fetchall()]
|
43 |
+
conn.close()
|
44 |
+
return links
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
app.layout = dbc.Container([
|
47 |
dbc.Row([
|
|
|
70 |
def update_url_list(n_clicks, new_name, new_link):
|
71 |
ctx = dash.callback_context
|
72 |
if ctx.triggered_id == 'add-url-button' and new_name and new_link:
|
73 |
+
conn = get_db_connection()
|
74 |
+
c = conn.cursor()
|
75 |
new_id = str(uuid.uuid4())
|
76 |
c.execute("INSERT INTO links VALUES (?, ?, ?)", (new_id, new_name, new_link))
|
77 |
conn.commit()
|
78 |
+
conn.close()
|
79 |
|
80 |
links = get_links()
|
81 |
return [
|
|
|
105 |
return dash.no_update
|
106 |
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
107 |
clicked_id = eval(button_id)['index']
|
108 |
+
conn = get_db_connection()
|
109 |
+
c = conn.cursor()
|
110 |
c.execute("SELECT url FROM links WHERE id = ?", (clicked_id,))
|
111 |
result = c.fetchone()
|
112 |
+
conn.close()
|
113 |
if result:
|
114 |
+
return result['url']
|
115 |
return dash.no_update
|
116 |
|
117 |
@callback(
|
|
|
125 |
return dash.no_update
|
126 |
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
127 |
delete_id = eval(button_id)['index']
|
128 |
+
conn = get_db_connection()
|
129 |
+
c = conn.cursor()
|
130 |
c.execute("DELETE FROM links WHERE id = ?", (delete_id,))
|
131 |
conn.commit()
|
132 |
+
conn.close()
|
133 |
return update_url_list(None, None, None)
|
134 |
|
135 |
if __name__ == '__main__':
|