Update app.py
Browse files
app.py
CHANGED
|
@@ -64,10 +64,23 @@ class PesticideDataFetcher:
|
|
| 64 |
return {"error": "Max retries exceeded"}
|
| 65 |
|
| 66 |
def get_products(self) -> List[Dict]:
|
| 67 |
-
"""Récupère la liste des produits."""
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
def get_mrls(self, product_id: int) -> List[Dict]:
|
| 73 |
"""Récupère les LMR pour un produit spécifique."""
|
|
@@ -75,10 +88,18 @@ class PesticideDataFetcher:
|
|
| 75 |
response = self.fetch_data(url)
|
| 76 |
return response.get("value", [])
|
| 77 |
|
| 78 |
-
def get_substance_details(self,
|
| 79 |
-
"""Récupère les détails d'une substance."""
|
| 80 |
-
url = f"{self.BASE_URL}/
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
class PesticideInterface:
|
| 84 |
"""Classe pour gérer l'interface utilisateur Gradio."""
|
|
@@ -138,7 +159,7 @@ class PesticideInterface:
|
|
| 138 |
return f'<a href="{regulation_url}" target="_blank">{regulation_number}</a>'
|
| 139 |
|
| 140 |
def get_product_details(self, product_name: str, period: str, show_only_changes: bool) -> pd.DataFrame:
|
| 141 |
-
"""Récupère et formate les détails des MRLs pour un produit donné."""
|
| 142 |
try:
|
| 143 |
if not product_name:
|
| 144 |
return pd.DataFrame({"Message": ["Veuillez sélectionner un produit"]})
|
|
@@ -162,6 +183,22 @@ class PesticideInterface:
|
|
| 162 |
if not mrls:
|
| 163 |
return pd.DataFrame({"Message": ["Aucune donnée trouvée pour la période sélectionnée"]})
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
# Créer le DataFrame
|
| 166 |
df = pd.DataFrame(mrls)
|
| 167 |
|
|
|
|
| 64 |
return {"error": "Max retries exceeded"}
|
| 65 |
|
| 66 |
def get_products(self) -> List[Dict]:
|
| 67 |
+
"""Récupère la liste complète des produits avec pagination."""
|
| 68 |
+
all_products = []
|
| 69 |
+
base_url = f"{self.BASE_URL}/pesticide_residues_products?format=json&language=FR&api-version=v2.0"
|
| 70 |
+
url = base_url
|
| 71 |
+
|
| 72 |
+
while url:
|
| 73 |
+
response = self.fetch_data(url)
|
| 74 |
+
if not response or "value" not in response:
|
| 75 |
+
break
|
| 76 |
+
|
| 77 |
+
all_products.extend(response["value"])
|
| 78 |
+
url = response.get("@odata.nextLink")
|
| 79 |
+
if url and not url.startswith("http"):
|
| 80 |
+
url = f"{self.BASE_URL}/{url}"
|
| 81 |
+
|
| 82 |
+
logger.info(f"Récupéré {len(all_products)} produits au total")
|
| 83 |
+
return all_products
|
| 84 |
|
| 85 |
def get_mrls(self, product_id: int) -> List[Dict]:
|
| 86 |
"""Récupère les LMR pour un produit spécifique."""
|
|
|
|
| 88 |
response = self.fetch_data(url)
|
| 89 |
return response.get("value", [])
|
| 90 |
|
| 91 |
+
def get_substance_details(self, pesticide_residue_id: int) -> Dict:
|
| 92 |
+
"""Récupère les détails d'une substance à partir de son ID."""
|
| 93 |
+
url = f"{self.BASE_URL}/pesticide_residues/{pesticide_residue_id}?format=json&api-version=v2.0"
|
| 94 |
+
response = self.fetch_data(url)
|
| 95 |
+
|
| 96 |
+
if not response or "value" not in response:
|
| 97 |
+
logger.warning(f"Pas de détails trouvés pour la substance {pesticide_residue_id}")
|
| 98 |
+
return {}
|
| 99 |
+
|
| 100 |
+
substance_data = response.get("value", [{}])[0]
|
| 101 |
+
substance_name = substance_data.get("substance_name", f"Substance {pesticide_residue_id}")
|
| 102 |
+
return {"substance_name": substance_name}
|
| 103 |
|
| 104 |
class PesticideInterface:
|
| 105 |
"""Classe pour gérer l'interface utilisateur Gradio."""
|
|
|
|
| 159 |
return f'<a href="{regulation_url}" target="_blank">{regulation_number}</a>'
|
| 160 |
|
| 161 |
def get_product_details(self, product_name: str, period: str, show_only_changes: bool) -> pd.DataFrame:
|
| 162 |
+
"""Récupère et formate les détails des MRLs pour un produit donné avec les noms des substances."""
|
| 163 |
try:
|
| 164 |
if not product_name:
|
| 165 |
return pd.DataFrame({"Message": ["Veuillez sélectionner un produit"]})
|
|
|
|
| 183 |
if not mrls:
|
| 184 |
return pd.DataFrame({"Message": ["Aucune donnée trouvée pour la période sélectionnée"]})
|
| 185 |
|
| 186 |
+
# Récupérer les noms des substances en parallèle
|
| 187 |
+
with ThreadPoolExecutor(max_workers=10) as executor:
|
| 188 |
+
futures = {
|
| 189 |
+
executor.submit(self.fetcher.get_substance_details, mrl["pesticide_residue_id"]): mrl
|
| 190 |
+
for mrl in mrls
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
for future in futures:
|
| 194 |
+
mrl = futures[future]
|
| 195 |
+
try:
|
| 196 |
+
substance_details = future.result()
|
| 197 |
+
mrl["substance_name"] = substance_details.get("substance_name", f"Substance {mrl['pesticide_residue_id']}")
|
| 198 |
+
except Exception as e:
|
| 199 |
+
logger.error(f"Erreur lors de la récupération des détails de la substance: {e}")
|
| 200 |
+
mrl["substance_name"] = f"Substance {mrl['pesticide_residue_id']}"
|
| 201 |
+
|
| 202 |
# Créer le DataFrame
|
| 203 |
df = pd.DataFrame(mrls)
|
| 204 |
|