Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -16,7 +16,7 @@ ddb.sql(f"""create or replace table ods_ins_rp2020_individus_dict \
|
|
16 |
as SELECT * FROM read_csv_auto("https://static.data.gouv.fr/resources/recensement-de-la-population-fichiers-detail-individus-localises-au-canton-ou-ville-2020-1/20231025-082910/dictionnaire-variables-indcvi-2020.csv")""");
|
17 |
|
18 |
# Normalisation des données pour une interrogation par indicateur
|
19 |
-
def prepare_data( filtre_territoire="
|
20 |
# Subset sans les mineurs + territoire sélectionné
|
21 |
ddb.sql(f""" \
|
22 |
create or replace table selected_data as (select * from ods_ins_rp2020_individus where iris like '{filtre_territoire}%' and AGER20 not in ('02','05','10','14','17') ); -- restriction aux adultes
|
@@ -144,14 +144,14 @@ def analyse_comparaison_territoires(table_territoire = "selected_data_stats_prop
|
|
144 |
return ddb.sql("select * from agr_comparaison_territoires order by 3, 5").to_df()
|
145 |
|
146 |
# Diagramme simple
|
147 |
-
def plot_analyse_histogramme(code_variable = "
|
148 |
histo = ddb.sql(f"""select lib_territoire territoire, code_variable, variable, modalite, proportion_locale "Pourcentage de population estimé (%)" from agr_comparaison_territoires where code_variable = '{code_variable}' \
|
149 |
/*union select 'National' territoire,code_variable, variable, modalite, proportion_reference proportion from agr_comparaison_territoires where code_variable = '{code_variable}' */
|
150 |
order by 5 """).to_df().hvplot.barh( x='modalite', y="Pourcentage de population estimé (%)", legend='bottom_right', height=400, width=800 , group_label=None)
|
151 |
return histo
|
152 |
|
153 |
# Fonction principale
|
154 |
-
def run_territoire_histo(filtre_territoire="
|
155 |
prepare_data(filtre_territoire,"<Territoire>")
|
156 |
agr_comparaison_territoires = analyse_comparaison_territoires()
|
157 |
return plot_analyse_histogramme(code_variable)
|
@@ -167,9 +167,9 @@ options_com_dict = dict(zip(options_com["nom_commune"], options_com["code_commun
|
|
167 |
|
168 |
|
169 |
# Widgets
|
170 |
-
variable_widget = pn.widgets.Select(name="code_variable", options=options_vars_dict)
|
171 |
#territoire_widget = pn.widgets.TextInput(name="filtre_territoire")
|
172 |
-
territoire_widget = pn.widgets.Select(name="filtre_territoire", options=options_com_dict)
|
173 |
|
174 |
# Bindings
|
175 |
bound_plot_histogramme = pn.bind(run_territoire_histo, code_variable=variable_widget, filtre_territoire=territoire_widget)
|
|
|
16 |
as SELECT * FROM read_csv_auto("https://static.data.gouv.fr/resources/recensement-de-la-population-fichiers-detail-individus-localises-au-canton-ou-ville-2020-1/20231025-082910/dictionnaire-variables-indcvi-2020.csv")""");
|
17 |
|
18 |
# Normalisation des données pour une interrogation par indicateur
|
19 |
+
def prepare_data( filtre_territoire="95018", lib_territoire="", persistant_table_name="selected_data_stats_proportions"):
|
20 |
# Subset sans les mineurs + territoire sélectionné
|
21 |
ddb.sql(f""" \
|
22 |
create or replace table selected_data as (select * from ods_ins_rp2020_individus where iris like '{filtre_territoire}%' and AGER20 not in ('02','05','10','14','17') ); -- restriction aux adultes
|
|
|
144 |
return ddb.sql("select * from agr_comparaison_territoires order by 3, 5").to_df()
|
145 |
|
146 |
# Diagramme simple
|
147 |
+
def plot_analyse_histogramme(code_variable = "TYPL"):
|
148 |
histo = ddb.sql(f"""select lib_territoire territoire, code_variable, variable, modalite, proportion_locale "Pourcentage de population estimé (%)" from agr_comparaison_territoires where code_variable = '{code_variable}' \
|
149 |
/*union select 'National' territoire,code_variable, variable, modalite, proportion_reference proportion from agr_comparaison_territoires where code_variable = '{code_variable}' */
|
150 |
order by 5 """).to_df().hvplot.barh( x='modalite', y="Pourcentage de population estimé (%)", legend='bottom_right', height=400, width=800 , group_label=None)
|
151 |
return histo
|
152 |
|
153 |
# Fonction principale
|
154 |
+
def run_territoire_histo(filtre_territoire="95018", code_variable="TYPL"):
|
155 |
prepare_data(filtre_territoire,"<Territoire>")
|
156 |
agr_comparaison_territoires = analyse_comparaison_territoires()
|
157 |
return plot_analyse_histogramme(code_variable)
|
|
|
167 |
|
168 |
|
169 |
# Widgets
|
170 |
+
variable_widget = pn.widgets.Select(name="code_variable", options=options_vars_dict, value="TYPL")
|
171 |
#territoire_widget = pn.widgets.TextInput(name="filtre_territoire")
|
172 |
+
territoire_widget = pn.widgets.Select(name="filtre_territoire", options=options_com_dict, value='95018')
|
173 |
|
174 |
# Bindings
|
175 |
bound_plot_histogramme = pn.bind(run_territoire_histo, code_variable=variable_widget, filtre_territoire=territoire_widget)
|