import dash
from dash import dcc, html, Input, Output, State, callback
import dash_bootstrap_components as dbc
import json
import os
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
# Disable SSL warnings
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# Create a session that ignores SSL verification
session = requests.Session()
session.verify = False
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
JSON_FILE = 'app.json'
def load_links():
if os.path.exists(JSON_FILE):
with open(JSON_FILE, 'r') as f:
links = json.load(f)
print(f"Loaded links: {links}") # Debug print
return links
print("JSON file not found") # Debug print
return []
def generate_url_list(links):
url_list = [
dbc.ListGroupItem(
dbc.Button(
link['name'],
id={'type': 'url-button', 'index': link['id']},
color="link",
className="text-left w-100"
),
className="p-0"
)
for link in links
]
print(f"Generated URL list: {url_list}") # Debug print
return url_list
app.layout = dbc.Container([
dbc.Row([
dbc.Col([
html.H2("AI Apps", className="mt-3 mb-4"),
html.Div(id='url-list'),
], width=2, className="bg-light-grey p-3"),
dbc.Col([
html.Iframe(
id='content-iframe',
style={'width': '100%', 'height': '800px'},
allow="*",
src=""
)
], width=10, className="bg-white")
])
], fluid=True)
@callback(
Output('url-list', 'children'),
Input('url-list', 'id')
)
def update_url_list(_):
links = load_links()
url_list = generate_url_list(links)
return url_list
@callback(
Output('content-iframe', 'src'),
Input({'type': 'url-button', 'index': dash.ALL}, 'n_clicks'),
prevent_initial_call=True
)
def update_iframe(*n_clicks):
ctx = dash.callback_context
if not ctx.triggered:
print("No button clicked")
return dash.no_update
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
clicked_id = eval(button_id)['index']
print(f"Button clicked: {clicked_id}")
links = load_links()
for link in links:
if link['id'] == clicked_id:
url = link['url']
print(f"Loading URL: {url}")
try:
# Use the session to make a request, ignoring SSL errors
response = session.get(url, verify=False)
response.raise_for_status()
return url
except requests.exceptions.RequestException as e:
print(f"Error loading URL: {e}")
return url # Return the URL even if there's an error
print("No matching URL found")
return dash.no_update
if __name__ == '__main__':
print("Starting the Dash application...")
print(f"JSON file location: {os.path.abspath(JSON_FILE)}")
app.run(debug=True, host='0.0.0.0', port=7860)
print("Dash application has finished running.")