Spaces:
Paused
Paused
import dash | |
from dash import dcc, html, Input, Output, State, callback | |
import dash_bootstrap_components as dbc | |
import uuid | |
import sqlite3 | |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]) | |
DB_FILE = 'links.db' | |
def get_db_connection(): | |
conn = sqlite3.connect(DB_FILE, check_same_thread=False) | |
conn.row_factory = sqlite3.Row | |
return conn | |
def init_db(): | |
conn = get_db_connection() | |
c = conn.cursor() | |
c.execute('''CREATE TABLE IF NOT EXISTS links | |
(id TEXT PRIMARY KEY, name TEXT, url TEXT)''') | |
conn.commit() | |
conn.close() | |
init_db() | |
def get_links(): | |
conn = get_db_connection() | |
c = conn.cursor() | |
c.execute("SELECT * FROM links") | |
links = [dict(row) for row in c.fetchall()] | |
conn.close() | |
return links | |
app.layout = dbc.Container([ | |
dbc.Modal([ | |
dbc.ModalHeader("Add New URL"), | |
dbc.ModalBody([ | |
dbc.Input(id='new-url-name', placeholder="URL Name", className="mb-2"), | |
dbc.Input(id='new-url-link', placeholder="URL", className="mb-2"), | |
]), | |
dbc.ModalFooter( | |
dbc.Button("Add", id="add-url-button", className="ml-auto") | |
), | |
], id="add-url-modal"), | |
dbc.Row([ | |
dbc.Col([ | |
html.H2("My URL App", className="mt-3 mb-4"), | |
dbc.Button("Add URL", id="open-modal-button", color="primary", className="mb-3"), | |
html.Div(id='url-list'), | |
], width=3, className="bg-light p-3"), | |
dbc.Col([ | |
html.Iframe(id='content-iframe', style={'width': '100%', 'height': '800px'}, sandbox="allow-scripts allow-same-origin") | |
], width=9) | |
]) | |
], fluid=True) | |
def toggle_modal(n1, n2, is_open): | |
if n1 or n2: | |
return not is_open | |
return is_open | |
def update_url_list(n_clicks, new_name, new_link): | |
ctx = dash.callback_context | |
if ctx.triggered_id == 'add-url-button' and new_name and new_link: | |
conn = get_db_connection() | |
c = conn.cursor() | |
new_id = str(uuid.uuid4()) | |
c.execute("INSERT INTO links VALUES (?, ?, ?)", (new_id, new_name, new_link)) | |
conn.commit() | |
conn.close() | |
links = get_links() | |
return [ | |
dbc.ListGroupItem([ | |
dbc.Button( | |
link['name'], | |
id={'type': 'url-button', 'index': link['id']}, | |
color="link", | |
className="text-left" | |
), | |
html.Span( | |
"β", | |
id={'type': 'delete-button', 'index': link['id']}, | |
className="float-right text-danger", | |
style={"cursor": "pointer"} | |
) | |
], className="d-flex justify-content-between align-items-center") | |
for link in links | |
] | |
def update_iframe(n_clicks): | |
ctx = dash.callback_context | |
if not ctx.triggered: | |
return dash.no_update | |
button_id = ctx.triggered[0]['prop_id'].split('.')[0] | |
clicked_id = eval(button_id)['index'] | |
conn = get_db_connection() | |
c = conn.cursor() | |
c.execute("SELECT url FROM links WHERE id = ?", (clicked_id,)) | |
result = c.fetchone() | |
conn.close() | |
if result: | |
return result['url'] | |
return dash.no_update | |
def delete_url(n_clicks): | |
ctx = dash.callback_context | |
if not ctx.triggered: | |
return dash.no_update | |
button_id = ctx.triggered[0]['prop_id'].split('.')[0] | |
delete_id = eval(button_id)['index'] | |
conn = get_db_connection() | |
c = conn.cursor() | |
c.execute("DELETE FROM links WHERE id = ?", (delete_id,)) | |
conn.commit() | |
conn.close() | |
return update_url_list(None, None, None) | |
if __name__ == '__main__': | |
print("Starting the Dash application...") | |
app.run(debug=True, host='0.0.0.0', port=7860) | |
print("Dash application has finished running.") |