generated from dataforgoodfr/d4g-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0299a24
commit 3a8f0f0
Showing
6 changed files
with
201 additions
and
424 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,153 +1,40 @@ | ||
from dash import Dash, html, dcc, Output, Input | ||
import plotly.express as px | ||
import plotly.graph_objects as go | ||
import pandas as pd | ||
from pathlib import Path | ||
import sys | ||
chemin_dossier_parent = Path(__file__).parent.parent | ||
sys.path.append(str(chemin_dossier_parent)) | ||
from my_data.db_connect import get_session | ||
from my_data.datasets import get_environment_data, get_lichen_data, get_lichen_species_data, get_observation_data, get_table_data, get_tree_data, get_tree_species, get_lichen_ecology | ||
|
||
# Source : https://discuss.streamlit.io/t/develop-a-dashboard-app-with-streamlit-using-plotly/37148/4 | ||
# Dash version | ||
# run with : python Dashboard/demo_dash.py | ||
|
||
# Load data function | ||
def load_data(): | ||
return pd.read_csv("https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv") | ||
|
||
# Initialize Dash app | ||
app = Dash(__name__) | ||
|
||
# Load data | ||
lichen_df = get_lichen_data() | ||
lichen_species_df = get_lichen_species_data() | ||
observation_df = get_observation_data() | ||
table_df = get_table_data() | ||
tree_species_df = get_tree_species() | ||
ecology_df = get_lichen_ecology() | ||
|
||
# Fonction pour calculer la fréquence des valeurs E, N, O, S | ||
def calculate_frequency(column): | ||
return column.apply(lambda x: sum(1 for char in x if char in ['E', 'N', 'O', 'S'])) | ||
|
||
# Calculer la fréquence | ||
table_df['freq'] = ( | ||
table_df['sq1'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + | ||
table_df['sq2'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + | ||
table_df['sq3'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + | ||
table_df['sq4'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + | ||
table_df['sq5'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) | ||
) | ||
|
||
# Joindre table avec lichen et observation | ||
merged_df = table_df.merge(lichen_df, left_on='lichen_id', right_on='id', suffixes=('', '_l')) | ||
merged_df = merged_df.merge(lichen_species_df, left_on='species_id', right_on='id', suffixes=('', '_ls')) | ||
merged_df = merged_df.merge(observation_df, left_on='observation_id', right_on='id', suffixes=('', '_o')) | ||
|
||
# Grouper par 'species' et 'observation_id' et additionner les fréquences | ||
grouped_df = merged_df.groupby(['name', 'observation_id'])['freq'].sum().reset_index() | ||
|
||
# Regrouper les deux tables afficher les données écologiques | ||
grouped_df = grouped_df.merge(ecology_df, left_on='name', right_on='cleaned_taxon', suffixes=('', '_e')) | ||
|
||
# ajustement des noms finaux | ||
grouped_df = grouped_df[['observation_id', 'name', 'freq','pH','eutrophication', 'poleotolerance']] | ||
grouped_df = grouped_df.rename( | ||
columns={ | ||
'observation_id': 'id', | ||
'name': 'lichen', | ||
'freq': 'freq', | ||
'pH': 'ph', | ||
'eutrophication': 'eutrophication', | ||
'poleotolerance': 'poleotolerance' | ||
}) | ||
|
||
# Calcul du degrés d'artificialisation | ||
def deg_artif(my_input: int): | ||
global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() | ||
base_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['poleotolerance'] == 'resistant')]['freq'].sum() | ||
|
||
return round((base_freq / global_freq) * 100, 2) | ||
|
||
# Calcul de la pollution acidé | ||
def pollution_acide(my_input: int): | ||
global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() | ||
acid_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['ph'] == 'acidophilous')]['freq'].sum() | ||
|
||
return round((acid_freq / global_freq) * 100, 2) | ||
|
||
def pollution_azote(my_input: int): | ||
global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() | ||
azote_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['eutrophication'] == 'eutrophic')]['freq'].sum() | ||
|
||
return round((azote_freq / global_freq) * 100, 2) | ||
df = load_data() | ||
|
||
# Define app layout with a hidden input | ||
app.layout = html.Div([ | ||
html.H1("Gauge bar Dash", style={'textAlign': 'center'}), | ||
dcc.Dropdown(grouped_df["id"].unique(), 419, id='dropdown-selection', style={'width': '50%', 'margin': 'auto'}), | ||
html.Div([ | ||
dcc.Graph(id='graph-content1', style={'flex': '1', 'margin': '10px'}), | ||
dcc.Graph(id='graph-content2', style={'flex': '1', 'margin': '10px'}), | ||
dcc.Graph(id='graph-content3', style={'flex': '1', 'margin': '10px'}) | ||
], style={'display': 'flex', 'justify-content': 'space-around', 'width': '100%'}) | ||
html.H1("Air Travel Time Series Plot", style={'textAlign': 'center'}), | ||
html.Div("This chart shows the number of air passengers traveled in each month from 1949 to 1960", style={'textAlign': 'center'}), | ||
dcc.Graph(id='air-travel-graph'), | ||
html.Div(id='dummy-div', style={'display': 'none'}) # Hidden div acting as a placeholder | ||
]) | ||
|
||
|
||
# Define callback to update graph | ||
@app.callback( | ||
[ | ||
Output('graph-content1', 'figure'), | ||
Output('graph-content2', 'figure'), | ||
Output('graph-content3', 'figure') | ||
], | ||
Input('dropdown-selection', 'value') | ||
Output('air-travel-graph', 'figure'), | ||
Input('dummy-div', 'children') # Use the hidden div as input | ||
) | ||
|
||
def update_graph(value): | ||
fig1 = go.Figure(go.Indicator( | ||
domain = {'x': [0, 1], 'y': [0, 1]}, | ||
value = deg_artif(value), | ||
mode = "gauge+number", | ||
title = {'text': "Degré d'artificialisation"}, | ||
gauge = {'axis': {'range': [0, 100], 'dtick': 25}, | ||
'bar': {'color': "#000000"}, | ||
'steps' : [ | ||
{'range': [0, 25], 'color': "green"}, | ||
{'range': [25, 50], 'color': "yellow"}, | ||
{'range': [50, 75], 'color': "orange"}, | ||
{'range': [75, 100], 'color': "red"} | ||
], | ||
'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': deg_artif(value)} | ||
})) | ||
|
||
fig2 = go.Figure(go.Indicator( | ||
domain = {'x': [0, 1], 'y': [0, 1]}, | ||
value = pollution_acide(value), | ||
mode = "gauge+number", | ||
title = {'text': "Degré d'artificialisation"}, | ||
gauge = {'axis': {'range': [0, 100], 'dtick': 25}, | ||
'bar': {'color': "#000000"}, | ||
'steps' : [ | ||
{'range': [0, 25], 'color': "green"}, | ||
{'range': [25, 50], 'color': "yellow"}, | ||
{'range': [50, 75], 'color': "orange"}, | ||
{'range': [75, 100], 'color': "red"} | ||
], | ||
'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': pollution_acide(value)} | ||
})) | ||
|
||
fig3 = go.Figure(go.Indicator( | ||
domain = {'x': [0, 1], 'y': [0, 1]}, | ||
value = pollution_azote(value), | ||
mode = "gauge+number", | ||
title = {'text': "Degré d'artificialisation"}, | ||
gauge = {'axis': {'range': [0, 100], 'dtick': 25}, | ||
'bar': {'color': "#000000"}, | ||
'steps' : [ | ||
{'range': [0, 25], 'color': "green"}, | ||
{'range': [25, 50], 'color': "yellow"}, | ||
{'range': [50, 75], 'color': "orange"}, | ||
{'range': [75, 100], 'color': "red"} | ||
], | ||
'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': pollution_azote(value)} | ||
})) | ||
return fig1, fig2, fig3 | ||
fig = px.line(df, x="Month", y=df.columns[1:], title="Air Passenger Travel") | ||
return fig | ||
|
||
|
||
# Run the app | ||
if __name__ == '__main__': | ||
app.run_server(debug=True) | ||
app.run_server(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,134 +1,20 @@ | ||
import streamlit as st | ||
import pandas as pd | ||
import my_data.datasets as df | ||
import plotly.graph_objects as go | ||
import numpy as np | ||
|
||
from pathlib import Path | ||
import sys | ||
chemin_dossier_parent = Path(__file__).parent.parent | ||
sys.path.append(str(chemin_dossier_parent)) | ||
from my_data.db_connect import get_session | ||
from my_data.datasets import get_environment_data, get_lichen_data, get_lichen_species_data, get_observation_data, get_table_data, get_tree_data, get_tree_species, get_lichen_ecology | ||
import plotly.express as px | ||
|
||
# Source : https://discuss.streamlit.io/t/develop-a-dashboard-app-with-streamlit-using-plotly/37148/4 | ||
# run with : streamlit run Dashboards/demo_streamlit.py | ||
|
||
# Récupération des datasets | ||
environment_df = get_environment_data() | ||
lichen_df = get_lichen_data() | ||
lichen_species_df = get_lichen_species_data() | ||
observation_df = get_observation_data() | ||
table_df = get_table_data() | ||
tree_df = get_tree_data() | ||
tree_species_df = get_tree_species() | ||
ecology_df = get_lichen_ecology() | ||
|
||
# Fonction pour calculer la fréquence des valeurs E, N, O, S | ||
def calculate_frequency(column): | ||
return column.apply(lambda x: sum(1 for char in x if char in ['E', 'N', 'O', 'S'])) | ||
|
||
# Calculer la fréquence | ||
table_df['freq'] = ( | ||
table_df['sq1'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + | ||
table_df['sq2'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + | ||
table_df['sq3'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + | ||
table_df['sq4'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + | ||
table_df['sq5'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) | ||
) | ||
|
||
# Joindre table avec lichen et observation | ||
merged_df = table_df.merge(lichen_df, left_on='lichen_id', right_on='id', suffixes=('', '_l')) | ||
merged_df = merged_df.merge(lichen_species_df, left_on='species_id', right_on='id', suffixes=('', '_ls')) | ||
merged_df = merged_df.merge(observation_df, left_on='observation_id', right_on='id', suffixes=('', '_o')) | ||
|
||
# Grouper par 'species' et 'observation_id' et additionner les fréquences | ||
grouped_df = merged_df.groupby(['name', 'observation_id'])['freq'].sum().reset_index() | ||
|
||
# Regrouper les deux tables afficher les données écologiques | ||
grouped_df = grouped_df.merge(ecology_df, left_on='name', right_on='cleaned_taxon', suffixes=('', '_e')) | ||
|
||
# ajustement des noms finaux | ||
grouped_df = grouped_df[['observation_id', 'name', 'freq','pH','eutrophication', 'poleotolerance']] | ||
grouped_df = grouped_df.rename( | ||
columns={ | ||
'observation_id': 'id', | ||
'name': 'lichen', | ||
'freq': 'freq', | ||
'pH': 'ph', | ||
'eutrophication': 'eutrophication', | ||
'poleotolerance': 'poleotolerance' | ||
}) | ||
|
||
# Calcul du degrés d'artificialisation | ||
def deg_artif(my_input: int): | ||
global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() | ||
base_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['poleotolerance'] == 'resistant')]['freq'].sum() | ||
|
||
return round((base_freq / global_freq) * 100, 2) | ||
|
||
# Calcul de la pollution acidé | ||
def pollution_acide(my_input: int): | ||
global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() | ||
acid_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['ph'] == 'acidophilous')]['freq'].sum() | ||
|
||
return round((acid_freq / global_freq) * 100, 2) | ||
|
||
def pollution_azote(my_input: int): | ||
global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() | ||
azote_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['eutrophication'] == 'eutrophic')]['freq'].sum() | ||
|
||
return round((azote_freq / global_freq) * 100, 2) | ||
# run with : streamlit run Dashboard/demo_streamlit.py | ||
|
||
# Sélection du site | ||
id_site = st.selectbox( | ||
"Sur quel site voulez-vous ?", | ||
grouped_df["id"].unique(), | ||
index=None, | ||
placeholder="site n°", | ||
) | ||
def load_data(): | ||
return pd.read_csv("https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv") | ||
|
||
# Sélection metrique | ||
metrics = st.selectbox( | ||
"Quelle métrique voulez-vous ?", | ||
("Degré d'artificialisation", "Pollution acide", "Pollution azote"), | ||
index=None, | ||
placeholder="Je sélectionne la métrique...", | ||
) | ||
# Load data | ||
df = load_data() | ||
|
||
# Affichage des éléments | ||
if id_site: | ||
if metrics == "Degré d'artificialisation": | ||
artificialisation_proportions = deg_artif(id_site) | ||
elif metrics == "Pollution acide": | ||
artificialisation_proportions = pollution_acide(id_site) | ||
elif metrics == "Pollution azote": | ||
artificialisation_proportions = pollution_azote(id_site) | ||
# Display some information | ||
st.title("Air Travel Time Series Plot") | ||
st.write("This chart shows the number of air passenger traveled in each month from 1949 to 1960") | ||
|
||
# Dataviz charts | ||
if id_site and metrics: | ||
########################## | ||
# Affichage du graphique # | ||
########################## | ||
st.write("# Gauge bar") | ||
st.write(f"Site : {id_site}") | ||
st.write(f"Lichen : {metrics}") | ||
fig1 = go.Figure(go.Indicator( | ||
domain = {'x': [0, 1], 'y': [0, 1]}, | ||
value = artificialisation_proportions, | ||
mode = "gauge+number", | ||
title = {'text': "Degré d'artificialisation"}, | ||
gauge = {'axis': {'range': [0, 100], 'dtick': 25}, | ||
'bar': {'color': "#000000"}, | ||
'steps' : [ | ||
{'range': [0, 25], 'color': "green"}, | ||
{'range': [25, 50], 'color': "yellow"}, | ||
{'range': [50, 75], 'color': "orange"}, | ||
{'range': [75, 100], 'color': "red"} | ||
], | ||
'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': artificialisation_proportions} | ||
})) | ||
# Afficher les donnée dans streamlit | ||
st.plotly_chart(fig1) | ||
else: | ||
st.write("Veuillez sélectionner un site et une métrique") | ||
# Plotly figure | ||
fig = px.line(df, x="Month", y=df.columns[1:], title="Air Passenger Travel") | ||
st.plotly_chart(fig) |
Oops, something went wrong.