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]) # Initialize SQLite database conn = sqlite3.connect('links.db', check_same_thread=False) c = conn.cursor() # Create table if it doesn't exist c.execute('''CREATE TABLE IF NOT EXISTS links (id TEXT PRIMARY KEY, name TEXT, url TEXT)''') conn.commit() # Function to get all links from the database def get_links(): c.execute("SELECT * FROM links") return [{"id": row[0], "name": row[1], "url": row[2]} for row in c.fetchall()] # Initialize with some example URLs if the table is empty if not get_links(): initial_urls = [ {"id": str(uuid.uuid4()), "name": "Google", "url": "https://www.google.com"}, {"id": str(uuid.uuid4()), "name": "Bing", "url": "https://www.bing.com"}, ] c.executemany("INSERT INTO links VALUES (:id, :name, :url)", initial_urls) conn.commit() app.layout = dbc.Container([ dbc.Row([ # Left navigation column dbc.Col([ html.H2("My URL App", className="mt-3 mb-4"), html.Div(id='url-list'), dbc.Input(id='new-url-name', placeholder="New URL Name", className="mb-2"), dbc.Input(id='new-url-link', placeholder="New URL", className="mb-2"), dbc.Button("Add URL", id='add-url-button', color="primary", className="mb-3"), ], width=3, className="bg-light p-3"), # Main content column with iframe dbc.Col([ html.Iframe(id='content-iframe', style={'width': '100%', 'height': '800px'}) ], width=9) ]) ], fluid=True) @callback( Output('url-list', 'children'), Input('add-url-button', 'n_clicks'), State('new-url-name', 'value'), State('new-url-link', 'value') ) 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: new_id = str(uuid.uuid4()) c.execute("INSERT INTO links VALUES (?, ?, ?)", (new_id, new_name, new_link)) conn.commit() links = get_links() return [ dbc.Button( link['name'], id={'type': 'url-button', 'index': link['id']}, color="link", className="d-block text-left mb-2" ) for link in links ] + [ dbc.Button( "Delete", id={'type': 'delete-button', 'index': link['id']}, color="danger", size="sm", className="mr-2 mb-2" ) for link in links ] @callback( Output('content-iframe', 'src'), Input({'type': 'url-button', 'index': dash.ALL}, 'n_clicks') ) 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'] c.execute("SELECT url FROM links WHERE id = ?", (clicked_id,)) result = c.fetchone() if result: return result[0] return dash.no_update @callback( Output('url-list', 'children', allow_duplicate=True), Input({'type': 'delete-button', 'index': dash.ALL}, 'n_clicks'), prevent_initial_call=True ) 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'] c.execute("DELETE FROM links WHERE id = ?", (delete_id,)) conn.commit() 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.")