Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import dash
|
2 |
from dash import dcc, html, Input, Output, State, callback
|
3 |
import dash_bootstrap_components as dbc
|
4 |
-
import uuid
|
5 |
import json
|
6 |
import os
|
7 |
import requests
|
@@ -20,51 +19,28 @@ def load_links():
|
|
20 |
return json.load(f)
|
21 |
return []
|
22 |
|
23 |
-
def save_links(links):
|
24 |
-
with open(JSON_FILE, 'w') as f:
|
25 |
-
json.dump(links, f)
|
26 |
-
|
27 |
def generate_url_list(links):
|
28 |
url_list = [
|
29 |
-
dbc.ListGroupItem(
|
30 |
dbc.Button(
|
31 |
link['name'],
|
32 |
id={'type': 'url-button', 'index': link['id']},
|
33 |
color="link",
|
34 |
className="text-left"
|
35 |
),
|
36 |
-
|
37 |
-
|
38 |
-
id={'type': 'delete-button', 'index': link['id']},
|
39 |
-
className="float-right text-danger",
|
40 |
-
style={"cursor": "pointer"}
|
41 |
-
)
|
42 |
-
], className="d-flex justify-content-between align-items-center")
|
43 |
for link in links
|
44 |
]
|
45 |
print(f"Generated URL list: {url_list}") # Debug print
|
46 |
return url_list
|
47 |
|
48 |
app.layout = dbc.Container([
|
49 |
-
dcc.Store(id='add-url-status', data=''),
|
50 |
-
dcc.Store(id='trigger-load', data='load'),
|
51 |
-
dbc.Modal([
|
52 |
-
dbc.ModalHeader("Add New URL"),
|
53 |
-
dbc.ModalBody([
|
54 |
-
dbc.Input(id='new-url-name', placeholder="URL Name", className="mb-2"),
|
55 |
-
dbc.Input(id='new-url-link', placeholder="URL", className="mb-2"),
|
56 |
-
html.Div(id='add-url-feedback')
|
57 |
-
]),
|
58 |
-
dbc.ModalFooter(
|
59 |
-
dbc.Button("Add", id="add-url-button", className="ml-auto")
|
60 |
-
),
|
61 |
-
], id="add-url-modal"),
|
62 |
dbc.Row([
|
63 |
dbc.Col([
|
64 |
-
html.H2("
|
65 |
-
dbc.Button("Add URL", id="open-modal-button", color="primary", className="mb-3"),
|
66 |
html.Div(id='url-list'),
|
67 |
-
], width=
|
68 |
dbc.Col([
|
69 |
html.Iframe(
|
70 |
id='content-iframe',
|
@@ -73,67 +49,18 @@ app.layout = dbc.Container([
|
|
73 |
allow="fullscreen; geolocation; microphone; camera; midi; encrypted-media; autoplay",
|
74 |
referrerPolicy="no-referrer"
|
75 |
)
|
76 |
-
], width=
|
77 |
])
|
78 |
], fluid=True)
|
79 |
|
80 |
@callback(
|
81 |
-
|
82 |
-
|
83 |
-
Output('new-url-link', 'value')],
|
84 |
-
[Input("open-modal-button", "n_clicks"), Input("add-url-button", "n_clicks")],
|
85 |
-
[State("add-url-modal", "is_open")],
|
86 |
-
)
|
87 |
-
def toggle_modal(n1, n2, is_open):
|
88 |
-
if n1 or n2:
|
89 |
-
if is_open:
|
90 |
-
return False, '', '' # Close modal and reset input fields
|
91 |
-
return True, dash.no_update, dash.no_update # Open modal
|
92 |
-
return is_open, dash.no_update, dash.no_update
|
93 |
-
|
94 |
-
@callback(
|
95 |
-
[Output('url-list', 'children'),
|
96 |
-
Output('add-url-feedback', 'children'),
|
97 |
-
Output('add-url-status', 'data')],
|
98 |
-
[Input('trigger-load', 'data'),
|
99 |
-
Input('add-url-button', 'n_clicks'),
|
100 |
-
Input({'type': 'delete-button', 'index': dash.ALL}, 'n_clicks')],
|
101 |
-
[State('new-url-name', 'value'),
|
102 |
-
State('new-url-link', 'value')],
|
103 |
-
prevent_initial_call=False
|
104 |
)
|
105 |
-
def update_url_list(
|
106 |
-
ctx = dash.callback_context
|
107 |
-
if not ctx.triggered:
|
108 |
-
raise dash.exceptions.PreventUpdate
|
109 |
-
|
110 |
-
triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
111 |
-
print(f"Triggered by: {triggered_id}") # Debug print
|
112 |
-
|
113 |
-
feedback = None
|
114 |
-
status = ''
|
115 |
links = load_links()
|
116 |
-
|
117 |
-
if triggered_id == 'trigger-load':
|
118 |
-
print("Initial load triggered")
|
119 |
-
elif triggered_id == 'add-url-button' and new_name and new_link:
|
120 |
-
new_id = str(uuid.uuid4())
|
121 |
-
links.append({'id': new_id, 'name': new_name, 'url': new_link})
|
122 |
-
save_links(links)
|
123 |
-
print(f"Added new URL: {new_name}, {new_link}") # Debug print
|
124 |
-
feedback = html.Div("URL added successfully!", style={'color': 'green'})
|
125 |
-
status = 'added'
|
126 |
-
elif 'delete-button' in triggered_id:
|
127 |
-
delete_id = eval(triggered_id)['index']
|
128 |
-
links = [link for link in links if link['id'] != delete_id]
|
129 |
-
save_links(links)
|
130 |
-
print(f"Deleted URL with id: {delete_id}") # Debug print
|
131 |
-
feedback = html.Div("URL deleted successfully!", style={'color': 'green'})
|
132 |
-
status = 'deleted'
|
133 |
-
|
134 |
url_list = generate_url_list(links)
|
135 |
-
|
136 |
-
return url_list, feedback, status
|
137 |
|
138 |
@callback(
|
139 |
Output('content-iframe', 'src'),
|
|
|
1 |
import dash
|
2 |
from dash import dcc, html, Input, Output, State, callback
|
3 |
import dash_bootstrap_components as dbc
|
|
|
4 |
import json
|
5 |
import os
|
6 |
import requests
|
|
|
19 |
return json.load(f)
|
20 |
return []
|
21 |
|
|
|
|
|
|
|
|
|
22 |
def generate_url_list(links):
|
23 |
url_list = [
|
24 |
+
dbc.ListGroupItem(
|
25 |
dbc.Button(
|
26 |
link['name'],
|
27 |
id={'type': 'url-button', 'index': link['id']},
|
28 |
color="link",
|
29 |
className="text-left"
|
30 |
),
|
31 |
+
className="d-flex justify-content-between align-items-center"
|
32 |
+
)
|
|
|
|
|
|
|
|
|
|
|
33 |
for link in links
|
34 |
]
|
35 |
print(f"Generated URL list: {url_list}") # Debug print
|
36 |
return url_list
|
37 |
|
38 |
app.layout = dbc.Container([
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
dbc.Row([
|
40 |
dbc.Col([
|
41 |
+
html.H2("AI Apps", className="mt-3 mb-4"),
|
|
|
42 |
html.Div(id='url-list'),
|
43 |
+
], width=2, className="bg-light-gray p-3"),
|
44 |
dbc.Col([
|
45 |
html.Iframe(
|
46 |
id='content-iframe',
|
|
|
49 |
allow="fullscreen; geolocation; microphone; camera; midi; encrypted-media; autoplay",
|
50 |
referrerPolicy="no-referrer"
|
51 |
)
|
52 |
+
], width=10)
|
53 |
])
|
54 |
], fluid=True)
|
55 |
|
56 |
@callback(
|
57 |
+
Output('url-list', 'children'),
|
58 |
+
Input('url-list', 'id')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
)
|
60 |
+
def update_url_list(_):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
links = load_links()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
url_list = generate_url_list(links)
|
63 |
+
return url_list
|
|
|
64 |
|
65 |
@callback(
|
66 |
Output('content-iframe', 'src'),
|