**Refactor flag reset mechanism in functi.py**
Browse filesReplace threading.Timer with invoke_long_callback for flag resets in multiple functions to improve code maintainability and avoid potential issues with daemon threads. Add a new reset_flag function to handle all flag resets.
functi.py
CHANGED
|
@@ -7,7 +7,7 @@ import time
|
|
| 7 |
import datetime
|
| 8 |
from line_db import DatabaseManager
|
| 9 |
from urllib.parse import urlencode
|
| 10 |
-
from taipy.gui import navigate # type: ignore
|
| 11 |
from gradio_client import Client
|
| 12 |
import pandas as pd
|
| 13 |
from requests_oauthlib import OAuth2Session
|
|
@@ -270,12 +270,8 @@ def add_scheduling(state):
|
|
| 270 |
except Exception as e:
|
| 271 |
print(f"❌ Error in add_scheduling: {e}", flush=True)
|
| 272 |
|
| 273 |
-
#
|
| 274 |
-
|
| 275 |
-
state.has_add_scheduling = False
|
| 276 |
-
timer = threading.Timer(2.0, reset_flag)
|
| 277 |
-
timer.daemon = True # permet de ne pas bloquer la fermeture de l’app
|
| 278 |
-
timer.start()
|
| 279 |
|
| 280 |
def planning():
|
| 281 |
df = db_manager.fetch_schedule_table()
|
|
@@ -318,12 +314,8 @@ def post_publishing(state) :
|
|
| 318 |
resp = requests.post(url, headers=headers, json=body)
|
| 319 |
print([resp.status_code, resp.text],flush = True)
|
| 320 |
|
| 321 |
-
#
|
| 322 |
-
|
| 323 |
-
state.has_post_publishing = False
|
| 324 |
-
timer = threading.Timer(2.0, reset_flag)
|
| 325 |
-
timer.daemon = True # permet de ne pas bloquer la fermeture de l’app
|
| 326 |
-
timer.start()
|
| 327 |
|
| 328 |
def post_generation(state) :
|
| 329 |
if not state.has_post_generation:
|
|
@@ -333,12 +325,8 @@ def post_generation(state) :
|
|
| 333 |
api_name="/poster_linkedin"
|
| 334 |
)
|
| 335 |
|
| 336 |
-
#
|
| 337 |
-
|
| 338 |
-
state.has_post_generation = False
|
| 339 |
-
timer = threading.Timer(2.0, reset_flag)
|
| 340 |
-
timer.daemon = True # permet de ne pas bloquer la fermeture de l’app
|
| 341 |
-
timer.start()
|
| 342 |
|
| 343 |
def authen(state) :
|
| 344 |
if not state.has_authen:
|
|
@@ -350,12 +338,37 @@ def authen(state) :
|
|
| 350 |
)
|
| 351 |
navigate(state, state.urlss)
|
| 352 |
|
| 353 |
-
#
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
def on_my_clicking(state, action, payload) :
|
| 361 |
if not state.has_on_my_clicking:
|
|
@@ -365,12 +378,8 @@ def on_my_clicking(state, action, payload) :
|
|
| 365 |
if payload["args"][0] == "Accueil" :
|
| 366 |
on_logout(state)
|
| 367 |
navigate(state, payload["args"][0])
|
| 368 |
-
#
|
| 369 |
-
|
| 370 |
-
state.has_on_my_clicking = False
|
| 371 |
-
timer = threading.Timer(2.0, reset_flag)
|
| 372 |
-
timer.daemon = True # permet de ne pas bloquer la fermeture de l’app
|
| 373 |
-
timer.start()
|
| 374 |
|
| 375 |
return " "
|
| 376 |
|
|
@@ -387,12 +396,8 @@ def add_source(state) :
|
|
| 387 |
data = db_manager.fetch_source_table(state.user_inf.user.id)
|
| 388 |
state.Source_table = pd.DataFrame(data)
|
| 389 |
|
| 390 |
-
#
|
| 391 |
-
|
| 392 |
-
state.has_add_source = False
|
| 393 |
-
timer = threading.Timer(2.0, reset_flag)
|
| 394 |
-
timer.daemon = True # permet de ne pas bloquer la fermeture de l’app
|
| 395 |
-
timer.start()
|
| 396 |
|
| 397 |
def delete_source(state, var_name: str, payload: dict) :
|
| 398 |
if not state.has_delete_source:
|
|
@@ -406,12 +411,8 @@ def delete_source(state, var_name: str, payload: dict) :
|
|
| 406 |
valeurs = diff['id'].tolist()
|
| 407 |
db_manager.delete_from_table("Source",valeurs)
|
| 408 |
|
| 409 |
-
#
|
| 410 |
-
|
| 411 |
-
state.has_delete_source = False
|
| 412 |
-
timer = threading.Timer(2.0, reset_flag)
|
| 413 |
-
timer.daemon = True # permet de ne pas bloquer la fermeture de l’app
|
| 414 |
-
timer.start()
|
| 415 |
|
| 416 |
def delete_account(state, var_name: str, payload: dict) :
|
| 417 |
if not state.has_delete_account:
|
|
@@ -425,12 +426,8 @@ def delete_account(state, var_name: str, payload: dict) :
|
|
| 425 |
valeurs = diff['id'].tolist()
|
| 426 |
db_manager.delete_from_table("Social_network",valeurs)
|
| 427 |
|
| 428 |
-
#
|
| 429 |
-
|
| 430 |
-
state.has_delete_account = False
|
| 431 |
-
timer = threading.Timer(2.0, reset_flag)
|
| 432 |
-
timer.daemon = True # permet de ne pas bloquer la fermeture de l’app
|
| 433 |
-
timer.start()
|
| 434 |
|
| 435 |
def delete_schedule(state, var_name: str, payload: dict) :
|
| 436 |
if not state.has_delete_schedule:
|
|
@@ -444,12 +441,8 @@ def delete_schedule(state, var_name: str, payload: dict) :
|
|
| 444 |
valeurs = diff['id'].tolist()
|
| 445 |
db_manager.delete_from_table("Scheduling",valeurs)
|
| 446 |
|
| 447 |
-
#
|
| 448 |
-
|
| 449 |
-
state.has_delete_schedule = False
|
| 450 |
-
timer = threading.Timer(2.0, reset_flag)
|
| 451 |
-
timer.daemon = True # permet de ne pas bloquer la fermeture de l’app
|
| 452 |
-
timer.start()
|
| 453 |
|
| 454 |
def on_login(state, payload):
|
| 455 |
"""Handle login form submission"""
|
|
@@ -491,12 +484,8 @@ def on_login(state, payload):
|
|
| 491 |
else:
|
| 492 |
state.message = "Email ou mot de passe incorrect."
|
| 493 |
|
| 494 |
-
#
|
| 495 |
-
|
| 496 |
-
state.has_on_login = False
|
| 497 |
-
timer = threading.Timer(2.0, reset_flag)
|
| 498 |
-
timer.daemon = True # permet de ne pas bloquer la fermeture de l’app
|
| 499 |
-
timer.start()
|
| 500 |
|
| 501 |
def on_register(state):
|
| 502 |
"""Handle registration form submission"""
|
|
@@ -531,12 +520,8 @@ def on_register(state):
|
|
| 531 |
else:
|
| 532 |
state.message = message or "Erreur lors de l'inscription. Veuillez réessayer."
|
| 533 |
|
| 534 |
-
#
|
| 535 |
-
|
| 536 |
-
state.has_on_register = False
|
| 537 |
-
timer = threading.Timer(2.0, reset_flag)
|
| 538 |
-
timer.daemon = True # permet de ne pas bloquer la fermeture de l’app
|
| 539 |
-
timer.start()
|
| 540 |
|
| 541 |
def on_logout(state):
|
| 542 |
if not state.has_on_logout:
|
|
@@ -550,12 +535,8 @@ def on_logout(state):
|
|
| 550 |
state.login_email = ""
|
| 551 |
state.login_password = ""
|
| 552 |
|
| 553 |
-
#
|
| 554 |
-
|
| 555 |
-
state.has_on_logout = False
|
| 556 |
-
timer = threading.Timer(2.0, reset_flag)
|
| 557 |
-
timer.daemon = True # permet de ne pas bloquer la fermeture de l’app
|
| 558 |
-
timer.start()
|
| 559 |
|
| 560 |
def toggle_register(state):
|
| 561 |
"""Toggle between login and register forms"""
|
|
@@ -569,9 +550,5 @@ def toggle_register(state):
|
|
| 569 |
state.register_password = ""
|
| 570 |
state.confirm_password = ""
|
| 571 |
|
| 572 |
-
#
|
| 573 |
-
|
| 574 |
-
state.has_toggle_register = False
|
| 575 |
-
timer = threading.Timer(2.0, reset_flag)
|
| 576 |
-
timer.daemon = True # permet de ne pas bloquer la fermeture de l’app
|
| 577 |
-
timer.start()
|
|
|
|
| 7 |
import datetime
|
| 8 |
from line_db import DatabaseManager
|
| 9 |
from urllib.parse import urlencode
|
| 10 |
+
from taipy.gui import navigate, invoke_long_callback # type: ignore
|
| 11 |
from gradio_client import Client
|
| 12 |
import pandas as pd
|
| 13 |
from requests_oauthlib import OAuth2Session
|
|
|
|
| 270 |
except Exception as e:
|
| 271 |
print(f"❌ Error in add_scheduling: {e}", flush=True)
|
| 272 |
|
| 273 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 274 |
+
invoke_long_callback(state, reset_flag, ["add_scheduling", 2.0])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
def planning():
|
| 277 |
df = db_manager.fetch_schedule_table()
|
|
|
|
| 314 |
resp = requests.post(url, headers=headers, json=body)
|
| 315 |
print([resp.status_code, resp.text],flush = True)
|
| 316 |
|
| 317 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 318 |
+
invoke_long_callback(state, reset_flag, ["post_publishing", 2.0])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
def post_generation(state) :
|
| 321 |
if not state.has_post_generation:
|
|
|
|
| 325 |
api_name="/poster_linkedin"
|
| 326 |
)
|
| 327 |
|
| 328 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 329 |
+
invoke_long_callback(state, reset_flag, ["post_generation", 2.0])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
|
| 331 |
def authen(state) :
|
| 332 |
if not state.has_authen:
|
|
|
|
| 338 |
)
|
| 339 |
navigate(state, state.urlss)
|
| 340 |
|
| 341 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 342 |
+
invoke_long_callback(state, reset_flag, ["authen", 2.0])
|
| 343 |
+
|
| 344 |
+
def reset_flag(state, action, delay):
|
| 345 |
+
time.sleep(delay)
|
| 346 |
+
if action == "on_my_clicking":
|
| 347 |
+
state.has_on_my_clicking = False
|
| 348 |
+
elif action == "add_source":
|
| 349 |
+
state.has_add_source = False
|
| 350 |
+
elif action == "authen":
|
| 351 |
+
state.has_authen = False
|
| 352 |
+
elif action == "post_generation":
|
| 353 |
+
state.has_post_generation = False
|
| 354 |
+
elif action == "post_publishing":
|
| 355 |
+
state.has_post_publishing = False
|
| 356 |
+
elif action == "add_scheduling":
|
| 357 |
+
state.has_add_scheduling = False
|
| 358 |
+
elif action == "on_login":
|
| 359 |
+
state.has_on_login = False
|
| 360 |
+
elif action == "on_register":
|
| 361 |
+
state.has_on_register = False
|
| 362 |
+
elif action == "on_logout":
|
| 363 |
+
state.has_on_logout = False
|
| 364 |
+
elif action == "toggle_register":
|
| 365 |
+
state.has_toggle_register = False
|
| 366 |
+
elif action == "delete_account":
|
| 367 |
+
state.has_delete_account = False
|
| 368 |
+
elif action == "delete_schedule":
|
| 369 |
+
state.has_delete_schedule = False
|
| 370 |
+
elif action == "delete_source":
|
| 371 |
+
state.has_delete_source = False
|
| 372 |
|
| 373 |
def on_my_clicking(state, action, payload) :
|
| 374 |
if not state.has_on_my_clicking:
|
|
|
|
| 378 |
if payload["args"][0] == "Accueil" :
|
| 379 |
on_logout(state)
|
| 380 |
navigate(state, payload["args"][0])
|
| 381 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 382 |
+
invoke_long_callback(state, reset_flag, ["on_my_clicking", 2.0])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
return " "
|
| 385 |
|
|
|
|
| 396 |
data = db_manager.fetch_source_table(state.user_inf.user.id)
|
| 397 |
state.Source_table = pd.DataFrame(data)
|
| 398 |
|
| 399 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 400 |
+
invoke_long_callback(state, reset_flag, ["add_source", 2.0])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
|
| 402 |
def delete_source(state, var_name: str, payload: dict) :
|
| 403 |
if not state.has_delete_source:
|
|
|
|
| 411 |
valeurs = diff['id'].tolist()
|
| 412 |
db_manager.delete_from_table("Source",valeurs)
|
| 413 |
|
| 414 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 415 |
+
invoke_long_callback(state, reset_flag, ["delete_source", 2.0])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
|
| 417 |
def delete_account(state, var_name: str, payload: dict) :
|
| 418 |
if not state.has_delete_account:
|
|
|
|
| 426 |
valeurs = diff['id'].tolist()
|
| 427 |
db_manager.delete_from_table("Social_network",valeurs)
|
| 428 |
|
| 429 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 430 |
+
invoke_long_callback(state, reset_flag, ["delete_account", 2.0])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
|
| 432 |
def delete_schedule(state, var_name: str, payload: dict) :
|
| 433 |
if not state.has_delete_schedule:
|
|
|
|
| 441 |
valeurs = diff['id'].tolist()
|
| 442 |
db_manager.delete_from_table("Scheduling",valeurs)
|
| 443 |
|
| 444 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 445 |
+
invoke_long_callback(state, reset_flag, ["delete_schedule", 2.0])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
def on_login(state, payload):
|
| 448 |
"""Handle login form submission"""
|
|
|
|
| 484 |
else:
|
| 485 |
state.message = "Email ou mot de passe incorrect."
|
| 486 |
|
| 487 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 488 |
+
invoke_long_callback(state, reset_flag, ["on_login", 2.0])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 489 |
|
| 490 |
def on_register(state):
|
| 491 |
"""Handle registration form submission"""
|
|
|
|
| 520 |
else:
|
| 521 |
state.message = message or "Erreur lors de l'inscription. Veuillez réessayer."
|
| 522 |
|
| 523 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 524 |
+
invoke_long_callback(state, reset_flag, ["on_register", 2.0])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
|
| 526 |
def on_logout(state):
|
| 527 |
if not state.has_on_logout:
|
|
|
|
| 535 |
state.login_email = ""
|
| 536 |
state.login_password = ""
|
| 537 |
|
| 538 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 539 |
+
invoke_long_callback(state, reset_flag, ["on_logout", 2.0])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
|
| 541 |
def toggle_register(state):
|
| 542 |
"""Toggle between login and register forms"""
|
|
|
|
| 550 |
state.register_password = ""
|
| 551 |
state.confirm_password = ""
|
| 552 |
|
| 553 |
+
# Utiliser invoke_long_callback pour la réinitialisation du flag
|
| 554 |
+
invoke_long_callback(state, reset_flag, ["toggle_register", 2.0])
|
|
|
|
|
|
|
|
|
|
|
|