bluenevus commited on
Commit
e15ce8c
·
verified ·
1 Parent(s): 6fe716c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -33
app.py CHANGED
@@ -6,7 +6,6 @@ import sqlite3
6
 
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():
@@ -20,21 +19,10 @@ 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
-
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()
@@ -44,23 +32,38 @@ def get_links():
44
  return links
45
 
46
  app.layout = dbc.Container([
 
 
 
 
 
 
 
 
 
 
47
  dbc.Row([
48
- # Left navigation column
49
  dbc.Col([
50
  html.H2("My URL App", className="mt-3 mb-4"),
 
51
  html.Div(id='url-list'),
52
- dbc.Input(id='new-url-name', placeholder="New URL Name", className="mb-2"),
53
- dbc.Input(id='new-url-link', placeholder="New URL", className="mb-2"),
54
- dbc.Button("Add URL", id='add-url-button', color="primary", className="mb-3"),
55
  ], width=3, className="bg-light p-3"),
56
-
57
- # Main content column with iframe
58
  dbc.Col([
59
- html.Iframe(id='content-iframe', style={'width': '100%', 'height': '800px'})
60
  ], width=9)
61
  ])
62
  ], fluid=True)
63
 
 
 
 
 
 
 
 
 
 
 
64
  @callback(
65
  Output('url-list', 'children'),
66
  Input('add-url-button', 'n_clicks'),
@@ -79,20 +82,21 @@ def update_url_list(n_clicks, new_name, new_link):
79
 
80
  links = get_links()
81
  return [
82
- dbc.Button(
83
- link['name'],
84
- id={'type': 'url-button', 'index': link['id']},
85
- color="link",
86
- className="d-block text-left mb-2"
87
- ) for link in links
88
- ] + [
89
- dbc.Button(
90
- "Delete",
91
- id={'type': 'delete-button', 'index': link['id']},
92
- color="danger",
93
- size="sm",
94
- className="mr-2 mb-2"
95
- ) for link in links
 
96
  ]
97
 
98
  @callback(
 
6
 
7
  app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
8
 
 
9
  DB_FILE = 'links.db'
10
 
11
  def get_db_connection():
 
19
  c.execute('''CREATE TABLE IF NOT EXISTS links
20
  (id TEXT PRIMARY KEY, name TEXT, url TEXT)''')
21
  conn.commit()
 
 
 
 
 
 
 
 
 
 
22
  conn.close()
23
 
24
  init_db()
25
 
 
26
  def get_links():
27
  conn = get_db_connection()
28
  c = conn.cursor()
 
32
  return links
33
 
34
  app.layout = dbc.Container([
35
+ dbc.Modal([
36
+ dbc.ModalHeader("Add New URL"),
37
+ dbc.ModalBody([
38
+ dbc.Input(id='new-url-name', placeholder="URL Name", className="mb-2"),
39
+ dbc.Input(id='new-url-link', placeholder="URL", className="mb-2"),
40
+ ]),
41
+ dbc.ModalFooter(
42
+ dbc.Button("Add", id="add-url-button", className="ml-auto")
43
+ ),
44
+ ], id="add-url-modal"),
45
  dbc.Row([
 
46
  dbc.Col([
47
  html.H2("My URL App", className="mt-3 mb-4"),
48
+ dbc.Button("Add URL", id="open-modal-button", color="primary", className="mb-3"),
49
  html.Div(id='url-list'),
 
 
 
50
  ], width=3, className="bg-light p-3"),
 
 
51
  dbc.Col([
52
+ html.Iframe(id='content-iframe', style={'width': '100%', 'height': '800px'}, sandbox="allow-scripts allow-same-origin")
53
  ], width=9)
54
  ])
55
  ], fluid=True)
56
 
57
+ @callback(
58
+ Output("add-url-modal", "is_open"),
59
+ [Input("open-modal-button", "n_clicks"), Input("add-url-button", "n_clicks")],
60
+ [State("add-url-modal", "is_open")],
61
+ )
62
+ def toggle_modal(n1, n2, is_open):
63
+ if n1 or n2:
64
+ return not is_open
65
+ return is_open
66
+
67
  @callback(
68
  Output('url-list', 'children'),
69
  Input('add-url-button', 'n_clicks'),
 
82
 
83
  links = get_links()
84
  return [
85
+ dbc.ListGroupItem([
86
+ dbc.Button(
87
+ link['name'],
88
+ id={'type': 'url-button', 'index': link['id']},
89
+ color="link",
90
+ className="text-left"
91
+ ),
92
+ html.Span(
93
+ "",
94
+ id={'type': 'delete-button', 'index': link['id']},
95
+ className="float-right text-danger",
96
+ style={"cursor": "pointer"}
97
+ )
98
+ ], className="d-flex justify-content-between align-items-center")
99
+ for link in links
100
  ]
101
 
102
  @callback(