Update app.py via AI Editor
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import dash
|
| 2 |
-
from dash import dcc, html, Input, Output, State,
|
| 3 |
import dash_bootstrap_components as dbc
|
| 4 |
import base64
|
| 5 |
import io
|
|
@@ -25,11 +25,13 @@ SESSION_DATA = {}
|
|
| 25 |
SESSION_LOCKS = {}
|
| 26 |
|
| 27 |
def get_session_id(set_cookie=False):
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
if sid and sid in SESSION_DATA:
|
| 31 |
return sid
|
| 32 |
-
# If not present, generate a new one
|
| 33 |
sid = str(uuid.uuid4())
|
| 34 |
if set_cookie:
|
| 35 |
g.set_cookie_sid = sid
|
|
@@ -48,6 +50,17 @@ def get_session_data():
|
|
| 48 |
SESSION_LOCKS[sid] = threading.Lock()
|
| 49 |
return SESSION_DATA[sid], SESSION_LOCKS[sid]
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def cleanup_session_tempdirs():
|
| 52 |
for sess in SESSION_DATA.values():
|
| 53 |
try:
|
|
@@ -82,8 +95,11 @@ openai.api_key = os.environ.get('OPENAI_API_KEY')
|
|
| 82 |
|
| 83 |
@server.before_request
|
| 84 |
def ensure_session_cookie():
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
if not sid or sid not in SESSION_DATA:
|
| 88 |
sid_new = str(uuid.uuid4())
|
| 89 |
g.set_cookie_sid = sid_new
|
|
@@ -279,15 +295,16 @@ def update_output(list_of_contents, list_of_names):
|
|
| 279 |
raise PreventUpdate
|
| 280 |
|
| 281 |
@app.callback(
|
| 282 |
-
Output('file-list', 'children'
|
| 283 |
Input({'type': 'remove-file', 'index': ALL}, 'n_clicks'),
|
|
|
|
| 284 |
prevent_initial_call=True
|
| 285 |
)
|
| 286 |
-
def remove_file(n_clicks):
|
| 287 |
-
ctx =
|
| 288 |
-
|
| 289 |
-
if not ctx.triggered:
|
| 290 |
raise PreventUpdate
|
|
|
|
| 291 |
triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
| 292 |
import ast
|
| 293 |
try:
|
|
@@ -299,6 +316,7 @@ def remove_file(n_clicks):
|
|
| 299 |
if removed_file in session_data['uploaded_files']:
|
| 300 |
try:
|
| 301 |
os.remove(session_data['uploaded_files'][removed_file])
|
|
|
|
| 302 |
except Exception as e:
|
| 303 |
logger.warning(f"Failed to delete temp file {removed_file}: {e}")
|
| 304 |
session_data['uploaded_files'].pop(removed_file, None)
|
|
@@ -306,6 +324,17 @@ def remove_file(n_clicks):
|
|
| 306 |
logger.info(f"Files after deletion: {list(session_data['uploaded_files'].keys())}")
|
| 307 |
return get_file_cards(session_data['uploaded_files'])
|
| 308 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
@app.callback(
|
| 310 |
Output('matrix-preview', 'children'),
|
| 311 |
Output('loading-output', 'children'),
|
|
@@ -314,7 +343,7 @@ def remove_file(n_clicks):
|
|
| 314 |
)
|
| 315 |
def generate_matrix(*args):
|
| 316 |
session_data, lock = get_session_data()
|
| 317 |
-
ctx =
|
| 318 |
if not ctx.triggered:
|
| 319 |
raise PreventUpdate
|
| 320 |
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
|
|
|
| 1 |
import dash
|
| 2 |
+
from dash import dcc, html, Input, Output, State, ALL, callback_context
|
| 3 |
import dash_bootstrap_components as dbc
|
| 4 |
import base64
|
| 5 |
import io
|
|
|
|
| 25 |
SESSION_LOCKS = {}
|
| 26 |
|
| 27 |
def get_session_id(set_cookie=False):
|
| 28 |
+
sid = None
|
| 29 |
+
try:
|
| 30 |
+
sid = request.cookies.get('session-id')
|
| 31 |
+
except Exception:
|
| 32 |
+
pass
|
| 33 |
if sid and sid in SESSION_DATA:
|
| 34 |
return sid
|
|
|
|
| 35 |
sid = str(uuid.uuid4())
|
| 36 |
if set_cookie:
|
| 37 |
g.set_cookie_sid = sid
|
|
|
|
| 50 |
SESSION_LOCKS[sid] = threading.Lock()
|
| 51 |
return SESSION_DATA[sid], SESSION_LOCKS[sid]
|
| 52 |
|
| 53 |
+
def restore_session_files(session_data):
|
| 54 |
+
temp_dir = session_data.get('temp_dir')
|
| 55 |
+
if not temp_dir or not os.path.exists(temp_dir):
|
| 56 |
+
return
|
| 57 |
+
files = os.listdir(temp_dir)
|
| 58 |
+
for filename in files:
|
| 59 |
+
file_path = os.path.join(temp_dir, filename)
|
| 60 |
+
if filename not in session_data['uploaded_files']:
|
| 61 |
+
session_data['uploaded_files'][filename] = file_path
|
| 62 |
+
session_data['file_texts'][filename] = parse_file_content(file_path, filename)
|
| 63 |
+
|
| 64 |
def cleanup_session_tempdirs():
|
| 65 |
for sess in SESSION_DATA.values():
|
| 66 |
try:
|
|
|
|
| 95 |
|
| 96 |
@server.before_request
|
| 97 |
def ensure_session_cookie():
|
| 98 |
+
sid = None
|
| 99 |
+
try:
|
| 100 |
+
sid = request.cookies.get('session-id')
|
| 101 |
+
except Exception:
|
| 102 |
+
sid = None
|
| 103 |
if not sid or sid not in SESSION_DATA:
|
| 104 |
sid_new = str(uuid.uuid4())
|
| 105 |
g.set_cookie_sid = sid_new
|
|
|
|
| 295 |
raise PreventUpdate
|
| 296 |
|
| 297 |
@app.callback(
|
| 298 |
+
Output('file-list', 'children'),
|
| 299 |
Input({'type': 'remove-file', 'index': ALL}, 'n_clicks'),
|
| 300 |
+
State('file-list', 'children'),
|
| 301 |
prevent_initial_call=True
|
| 302 |
)
|
| 303 |
+
def remove_file(n_clicks, file_list_children):
|
| 304 |
+
ctx = callback_context
|
| 305 |
+
if not ctx.triggered or not any(n_clicks):
|
|
|
|
| 306 |
raise PreventUpdate
|
| 307 |
+
session_data, lock = get_session_data()
|
| 308 |
triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
| 309 |
import ast
|
| 310 |
try:
|
|
|
|
| 316 |
if removed_file in session_data['uploaded_files']:
|
| 317 |
try:
|
| 318 |
os.remove(session_data['uploaded_files'][removed_file])
|
| 319 |
+
logger.info(f"Deleted file from disk: {removed_file}")
|
| 320 |
except Exception as e:
|
| 321 |
logger.warning(f"Failed to delete temp file {removed_file}: {e}")
|
| 322 |
session_data['uploaded_files'].pop(removed_file, None)
|
|
|
|
| 324 |
logger.info(f"Files after deletion: {list(session_data['uploaded_files'].keys())}")
|
| 325 |
return get_file_cards(session_data['uploaded_files'])
|
| 326 |
|
| 327 |
+
@app.callback(
|
| 328 |
+
Output('file-list', 'children', allow_duplicate=True),
|
| 329 |
+
Input('file-list', 'children'),
|
| 330 |
+
prevent_initial_call=False
|
| 331 |
+
)
|
| 332 |
+
def restore_file_list(file_list_children):
|
| 333 |
+
session_data, lock = get_session_data()
|
| 334 |
+
with lock:
|
| 335 |
+
restore_session_files(session_data)
|
| 336 |
+
return get_file_cards(session_data['uploaded_files'])
|
| 337 |
+
|
| 338 |
@app.callback(
|
| 339 |
Output('matrix-preview', 'children'),
|
| 340 |
Output('loading-output', 'children'),
|
|
|
|
| 343 |
)
|
| 344 |
def generate_matrix(*args):
|
| 345 |
session_data, lock = get_session_data()
|
| 346 |
+
ctx = callback_context
|
| 347 |
if not ctx.triggered:
|
| 348 |
raise PreventUpdate
|
| 349 |
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
|