app-portal / app.py
bluenevus's picture
Update app.py
9d9c92a verified
raw
history blame
2.75 kB
import dash
from dash import dcc, html, Input, Output, State, callback
import dash_bootstrap_components as dbc
import json
import os
import requests
from urllib3.exceptions import InsecureRequestWarning
# Disable SSL warnings
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
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:
return json.load(f)
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'},
sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-downloads",
allow="fullscreen; geolocation; microphone; camera; midi; encrypted-media; autoplay",
referrerPolicy="no-referrer"
)
], 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:
print(f"Loading URL: {link['url']}")
return link['url']
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.")