Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,18 +12,6 @@ st.title("Base de données des pesticides de l'UE")
|
|
| 12 |
def get_products():
|
| 13 |
url = "https://api.datalake.sante.service.ec.europa.eu/sante/pesticides/pesticide_residues_products"
|
| 14 |
params = {"format": "json", "language": "FR", "api-version": "v2.0"}
|
| 15 |
-
headers = {
|
| 16 |
-
"User-Agent": "StreamlitApp/1.0" # Identifie votre application
|
| 17 |
-
}
|
| 18 |
-
response = requests.get(url, params=params, headers=headers)
|
| 19 |
-
response.raise_for_status()
|
| 20 |
-
return response.json()
|
| 21 |
-
|
| 22 |
-
# Fonction pour récupérer les LMR par produit
|
| 23 |
-
@st.cache_data
|
| 24 |
-
def get_mrls(product_id):
|
| 25 |
-
url = "https://api.datalake.sante.service.ec.europa.eu/sante/pesticides/pesticide_residues_mrls"
|
| 26 |
-
params = {"format": "json", "product_id": product_id, "api-version": "v2.0"}
|
| 27 |
headers = {
|
| 28 |
"User-Agent": "StreamlitApp/1.0"
|
| 29 |
}
|
|
@@ -31,21 +19,17 @@ def get_mrls(product_id):
|
|
| 31 |
response.raise_for_status()
|
| 32 |
return response.json()
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
@st.cache_data
|
| 36 |
-
def get_substance_info(substance_id):
|
| 37 |
-
url = "https://api.datalake.sante.service.ec.europa.eu/sante/pesticides/pesticide_residues"
|
| 38 |
-
params = {"format": "json", "pesticide_residue_id": substance_id, "api-version": "v2.0"}
|
| 39 |
-
headers = {
|
| 40 |
-
"User-Agent": "StreamlitApp/1.0"
|
| 41 |
-
}
|
| 42 |
-
response = requests.get(url, params=params, headers=headers)
|
| 43 |
-
response.raise_for_status()
|
| 44 |
-
return response.json()
|
| 45 |
-
|
| 46 |
-
# Interface utilisateur
|
| 47 |
products = get_products()
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
selected_product = st.selectbox("Sélectionnez un produit agricole", product_names)
|
| 50 |
|
| 51 |
# Filtrage par période
|
|
@@ -53,52 +37,53 @@ period = st.radio("Filtrer par période", ["Toutes dates", "Dernière semaine",
|
|
| 53 |
|
| 54 |
# Bouton pour lancer l'analyse
|
| 55 |
if st.button("Analyser les données"):
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 12 |
def get_products():
|
| 13 |
url = "https://api.datalake.sante.service.ec.europa.eu/sante/pesticides/pesticide_residues_products"
|
| 14 |
params = {"format": "json", "language": "FR", "api-version": "v2.0"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
headers = {
|
| 16 |
"User-Agent": "StreamlitApp/1.0"
|
| 17 |
}
|
|
|
|
| 19 |
response.raise_for_status()
|
| 20 |
return response.json()
|
| 21 |
|
| 22 |
+
# Affichage temporaire pour vérifier la structure de la réponse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
products = get_products()
|
| 24 |
+
st.write("Structure de la réponse de l'API :", products)
|
| 25 |
+
|
| 26 |
+
# Vérifiez que 'products' est une liste de dictionnaires
|
| 27 |
+
if isinstance(products, list) and all(isinstance(product, dict) for product in products):
|
| 28 |
+
product_names = [product['name'] for product in products]
|
| 29 |
+
else:
|
| 30 |
+
st.error("La structure de la réponse de l'API n'est pas celle attendue.")
|
| 31 |
+
product_names = []
|
| 32 |
+
|
| 33 |
selected_product = st.selectbox("Sélectionnez un produit agricole", product_names)
|
| 34 |
|
| 35 |
# Filtrage par période
|
|
|
|
| 37 |
|
| 38 |
# Bouton pour lancer l'analyse
|
| 39 |
if st.button("Analyser les données"):
|
| 40 |
+
if product_names: # Vérifiez que la liste n'est pas vide
|
| 41 |
+
product_id = next(product['id'] for product in products if product['name'] == selected_product)
|
| 42 |
+
mrls = get_mrls(product_id)
|
| 43 |
|
| 44 |
+
# Filtrer les données par période
|
| 45 |
+
today = datetime.today()
|
| 46 |
+
if period == "Dernière semaine":
|
| 47 |
+
start_date = today - timedelta(days=7)
|
| 48 |
+
elif period == "Dernier mois":
|
| 49 |
+
start_date = today - timedelta(days=30)
|
| 50 |
+
elif period == "6 prochains mois":
|
| 51 |
+
start_date = today + timedelta(days=180)
|
| 52 |
+
else:
|
| 53 |
+
start_date = datetime.min
|
| 54 |
|
| 55 |
+
filtered_mrls = [mrl for mrl in mrls if datetime.strptime(mrl['date'], "%Y-%m-%d") >= start_date]
|
| 56 |
|
| 57 |
+
# Afficher les résultats dans un tableau
|
| 58 |
+
mrl_data = []
|
| 59 |
+
for mrl in filtered_mrls:
|
| 60 |
+
substance_info = get_substance_info(mrl['substance_id'])
|
| 61 |
+
mrl_data.append({
|
| 62 |
+
"Substance": substance_info['name'],
|
| 63 |
+
"Valeur LMR": mrl['value'],
|
| 64 |
+
"Date d'application": mrl['date'],
|
| 65 |
+
"Règlement": f"[Lien]({mrl['regulation_url']})"
|
| 66 |
+
})
|
| 67 |
|
| 68 |
+
df = pd.DataFrame(mrl_data)
|
| 69 |
+
st.write(df)
|
| 70 |
|
| 71 |
+
# Visualisation graphique
|
| 72 |
+
plt.figure(figsize=(10, 6))
|
| 73 |
+
for substance in df['Substance'].unique()[:10]: # Limiter aux 10 substances les plus fréquentes
|
| 74 |
+
subset = df[df['Substance'] == substance]
|
| 75 |
+
plt.plot(subset['Date d\'application'], subset['Valeur LMR'], label=substance)
|
| 76 |
+
plt.xlabel("Date d'application")
|
| 77 |
+
plt.ylabel("Valeur LMR")
|
| 78 |
+
plt.title("Évolution des LMR dans le temps")
|
| 79 |
+
plt.legend()
|
| 80 |
+
st.pyplot(plt)
|
| 81 |
|
| 82 |
+
# Téléchargement CSV
|
| 83 |
+
csv = df.to_csv(index=False)
|
| 84 |
+
st.download_button(
|
| 85 |
+
label="Télécharger les données en CSV",
|
| 86 |
+
data=csv,
|
| 87 |
+
file_name="lmr_data.csv",
|
| 88 |
+
mime="text/csv",
|
| 89 |
+
)
|