Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from dash import dcc, html, Input, Output, State, callback
|
|
| 3 |
import dash_bootstrap_components as dbc
|
| 4 |
import uuid
|
| 5 |
import sqlite3
|
|
|
|
| 6 |
|
| 7 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
| 8 |
|
|
@@ -20,7 +21,7 @@ def init_db():
|
|
| 20 |
c.execute('''CREATE TABLE IF NOT EXISTS links
|
| 21 |
(id TEXT PRIMARY KEY, name TEXT, url TEXT)''')
|
| 22 |
conn.commit()
|
| 23 |
-
print("Database initialized successfully")
|
| 24 |
except Exception as e:
|
| 25 |
print(f"Error initializing database: {e}")
|
| 26 |
finally:
|
|
@@ -34,7 +35,7 @@ def get_links():
|
|
| 34 |
c = conn.cursor()
|
| 35 |
c.execute("SELECT * FROM links")
|
| 36 |
links = [dict(row) for row in c.fetchall()]
|
| 37 |
-
print(f"Retrieved links: {links}") # Debug print
|
| 38 |
return links
|
| 39 |
except Exception as e:
|
| 40 |
print(f"Error retrieving links: {e}")
|
|
@@ -63,51 +64,7 @@ def generate_url_list(links):
|
|
| 63 |
print(f"Generated URL list: {url_list}") # Debug print
|
| 64 |
return url_list
|
| 65 |
|
| 66 |
-
|
| 67 |
-
dcc.Store(id='add-url-status', data=''),
|
| 68 |
-
dcc.Store(id='trigger-load', data='load'), # New store to trigger initial load
|
| 69 |
-
dbc.Modal([
|
| 70 |
-
dbc.ModalHeader("Add New URL"),
|
| 71 |
-
dbc.ModalBody([
|
| 72 |
-
dbc.Input(id='new-url-name', placeholder="URL Name", className="mb-2"),
|
| 73 |
-
dbc.Input(id='new-url-link', placeholder="URL", className="mb-2"),
|
| 74 |
-
html.Div(id='add-url-feedback')
|
| 75 |
-
]),
|
| 76 |
-
dbc.ModalFooter(
|
| 77 |
-
dbc.Button("Add", id="add-url-button", className="ml-auto")
|
| 78 |
-
),
|
| 79 |
-
], id="add-url-modal"),
|
| 80 |
-
dbc.Row([
|
| 81 |
-
dbc.Col([
|
| 82 |
-
html.H2("My URL App", className="mt-3 mb-4"),
|
| 83 |
-
dbc.Button("Add URL", id="open-modal-button", color="primary", className="mb-3"),
|
| 84 |
-
html.Div(id='url-list'),
|
| 85 |
-
], width=3, className="bg-light p-3"),
|
| 86 |
-
dbc.Col([
|
| 87 |
-
html.Iframe(
|
| 88 |
-
id='content-iframe',
|
| 89 |
-
style={'width': '100%', 'height': '800px'},
|
| 90 |
-
sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-downloads",
|
| 91 |
-
allow="fullscreen; geolocation; microphone; camera; midi; encrypted-media; autoplay",
|
| 92 |
-
referrerPolicy="no-referrer"
|
| 93 |
-
)
|
| 94 |
-
], width=9)
|
| 95 |
-
])
|
| 96 |
-
], fluid=True)
|
| 97 |
-
|
| 98 |
-
@callback(
|
| 99 |
-
[Output("add-url-modal", "is_open"),
|
| 100 |
-
Output('new-url-name', 'value'),
|
| 101 |
-
Output('new-url-link', 'value')],
|
| 102 |
-
[Input("open-modal-button", "n_clicks"), Input("add-url-button", "n_clicks")],
|
| 103 |
-
[State("add-url-modal", "is_open")],
|
| 104 |
-
)
|
| 105 |
-
def toggle_modal(n1, n2, is_open):
|
| 106 |
-
if n1 or n2:
|
| 107 |
-
if is_open:
|
| 108 |
-
return False, '', '' # Close modal and reset input fields
|
| 109 |
-
return True, dash.no_update, dash.no_update # Open modal
|
| 110 |
-
return is_open, dash.no_update, dash.no_update
|
| 111 |
|
| 112 |
@callback(
|
| 113 |
[Output('url-list', 'children'),
|
|
@@ -140,11 +97,11 @@ def update_url_list(trigger_load, add_clicks, delete_clicks, new_name, new_link)
|
|
| 140 |
new_id = str(uuid.uuid4())
|
| 141 |
c.execute("INSERT INTO links VALUES (?, ?, ?)", (new_id, new_name, new_link))
|
| 142 |
conn.commit()
|
| 143 |
-
print(f"Added new URL: {new_name}, {new_link}") # Debug print
|
| 144 |
feedback = html.Div("URL added successfully!", style={'color': 'green'})
|
| 145 |
status = 'added'
|
| 146 |
except Exception as e:
|
| 147 |
-
print(f"Error adding URL: {e}")
|
| 148 |
feedback = html.Div("Error adding URL. Please try again.", style={'color': 'red'})
|
| 149 |
status = 'error'
|
| 150 |
finally:
|
|
@@ -156,11 +113,11 @@ def update_url_list(trigger_load, add_clicks, delete_clicks, new_name, new_link)
|
|
| 156 |
c = conn.cursor()
|
| 157 |
c.execute("DELETE FROM links WHERE id = ?", (delete_id,))
|
| 158 |
conn.commit()
|
| 159 |
-
print(f"Deleted URL with id: {delete_id}") # Debug print
|
| 160 |
feedback = html.Div("URL deleted successfully!", style={'color': 'green'})
|
| 161 |
status = 'deleted'
|
| 162 |
except Exception as e:
|
| 163 |
-
print(f"Error deleting URL: {e}")
|
| 164 |
feedback = html.Div("Error deleting URL. Please try again.", style={'color': 'red'})
|
| 165 |
status = 'error'
|
| 166 |
finally:
|
|
|
|
| 3 |
import dash_bootstrap_components as dbc
|
| 4 |
import uuid
|
| 5 |
import sqlite3
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
| 9 |
|
|
|
|
| 21 |
c.execute('''CREATE TABLE IF NOT EXISTS links
|
| 22 |
(id TEXT PRIMARY KEY, name TEXT, url TEXT)''')
|
| 23 |
conn.commit()
|
| 24 |
+
print(f"Database initialized successfully at {os.path.abspath(DB_FILE)}")
|
| 25 |
except Exception as e:
|
| 26 |
print(f"Error initializing database: {e}")
|
| 27 |
finally:
|
|
|
|
| 35 |
c = conn.cursor()
|
| 36 |
c.execute("SELECT * FROM links")
|
| 37 |
links = [dict(row) for row in c.fetchall()]
|
| 38 |
+
print(f"Retrieved links from database: {links}") # Debug print
|
| 39 |
return links
|
| 40 |
except Exception as e:
|
| 41 |
print(f"Error retrieving links: {e}")
|
|
|
|
| 64 |
print(f"Generated URL list: {url_list}") # Debug print
|
| 65 |
return url_list
|
| 66 |
|
| 67 |
+
# ... (rest of the layout code remains the same)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
@callback(
|
| 70 |
[Output('url-list', 'children'),
|
|
|
|
| 97 |
new_id = str(uuid.uuid4())
|
| 98 |
c.execute("INSERT INTO links VALUES (?, ?, ?)", (new_id, new_name, new_link))
|
| 99 |
conn.commit()
|
| 100 |
+
print(f"Added new URL to database: {new_name}, {new_link}") # Debug print
|
| 101 |
feedback = html.Div("URL added successfully!", style={'color': 'green'})
|
| 102 |
status = 'added'
|
| 103 |
except Exception as e:
|
| 104 |
+
print(f"Error adding URL to database: {e}")
|
| 105 |
feedback = html.Div("Error adding URL. Please try again.", style={'color': 'red'})
|
| 106 |
status = 'error'
|
| 107 |
finally:
|
|
|
|
| 113 |
c = conn.cursor()
|
| 114 |
c.execute("DELETE FROM links WHERE id = ?", (delete_id,))
|
| 115 |
conn.commit()
|
| 116 |
+
print(f"Deleted URL with id from database: {delete_id}") # Debug print
|
| 117 |
feedback = html.Div("URL deleted successfully!", style={'color': 'green'})
|
| 118 |
status = 'deleted'
|
| 119 |
except Exception as e:
|
| 120 |
+
print(f"Error deleting URL from database: {e}")
|
| 121 |
feedback = html.Div("Error deleting URL. Please try again.", style={'color': 'red'})
|
| 122 |
status = 'error'
|
| 123 |
finally:
|