Spaces:
Sleeping
Sleeping
| # Example taken from here - https://shinylive.io/py/examples/#map | |
| import os | |
| import io | |
| from shiny import reactive, render, ui, App | |
| from plot_utils import show_one_plot, show_two_plots | |
| from data_utils import prepare_data, process_data_trees_to_temp, process_data_temp_to_trees | |
| import pandas as pd | |
| import geopandas as gpd | |
| import numpy as np | |
| og_file = "cleaned_dataframe.csv" | |
| training_file = "training_dataframe.csv" | |
| original_df, df, X, Y, T = prepare_data(og_file, training_file) | |
| app_ui = ui.page_fluid( | |
| ui.input_radio_buttons("tab_choice", "Seleccionar p谩gina:", ["Efecto de los 谩rboles en la temperatura", "Efecto de la temperatura en los 谩rboles"]), | |
| ui.output_ui("tab_slider"), | |
| ui.tooltip( | |
| ui.input_radio_buttons( | |
| "split_filter", | |
| "驴Aplicar filtro?", | |
| choices=["Sin filtro", "Filtro"], | |
| selected="Sin filtro" | |
| ), | |
| "Indica exceso de cobertura: marr贸n = l铆mite por construcci贸n, rojo = m谩s del 100%.", | |
| placement="right" | |
| ), | |
| ui.output_ui("page_ui"), | |
| ui.download_button("download_csv", "Descargar simulaci贸n (CSV)") | |
| ) | |
| def server(input, output, session): | |
| def tab_slider(): | |
| if input.tab_choice() == "Efecto de los 谩rboles en la temperatura": | |
| return ui.input_slider("tree_pct", "Aumento de cobertura arb贸rea (%)", min=0, max=100, value=40, step=5) | |
| else: # "Effect of temperature on trees" | |
| return ui.input_slider("temp_goal", "Disminuci贸n de la temperatura (掳C)", min=0, max=2, value=0.5, step=0.1) | |
| def get_gdf_trees(): | |
| value_trees = input.tree_pct() | |
| print(f"value inputed is {value_trees}") | |
| return process_data_trees_to_temp(value_trees, original_df, df, X, Y, T) | |
| def get_gdf_temp(): | |
| value_temp = input.temp_goal() | |
| print(f"value inputed is {value_temp}") | |
| return process_data_temp_to_trees(value_temp, original_df, df, X, Y, T) | |
| def page_ui(): | |
| if input.tab_choice() == "Efecto de los 谩rboles en la temperatura": | |
| return ui.div( | |
| ui.h2("Visualizador de simulaci贸n: Aumento de la cobertura arb贸rea", class_="text-center"), | |
| ui.div(ui.output_plot("temp_after_treatment"), style="display: flex; justify-content: center;"), | |
| ui.div(ui.output_plot("temp_change"), style="display: flex; justify-content: center;"), | |
| ) | |
| else: #Effect of temperature on trees | |
| return ui.div( | |
| ui.h2("Visualizador de simulaci贸n: disminuir la temperatura", class_="text-center"), | |
| ui.div(ui.output_plot("trees_after_decrease"), style="display: flex; justify-content: center;"), | |
| ui.div(ui.output_plot("tree_coverage_change"), style="display: flex; justify-content: center;"), | |
| ) | |
| def temp_after_treatment(): | |
| gdf_trees = get_gdf_trees() | |
| value_trees = input.tree_pct() | |
| apply_filter = input.split_filter() | |
| if apply_filter == "Filtro": | |
| split = f'%total_trees_treatment_{value_trees}%' | |
| else: | |
| split = False | |
| min_temp = 15 | |
| max_temp = 35 | |
| try: | |
| fig = show_two_plots(gdf_trees, f'simulated_temp_{value_trees}%', 'LST', | |
| min_temp, max_temp, 'coolwarm',f"Temperatura estimada despu茅s del aumento de cobertura arb贸rea (+{value_trees}% )", | |
| "Temperatura antes",'Temperatura (掳C)',split=split) | |
| return fig | |
| except Exception as e: | |
| print("Plotting error:", e) | |
| raise e | |
| def temp_change(): | |
| gdf_trees = get_gdf_trees() | |
| value_trees = input.tree_pct() | |
| most_temp_change = -2 | |
| no_temp_change = 0 | |
| fig = show_one_plot(gdf_trees, f'treatment_effect_{value_trees}%', most_temp_change, no_temp_change, | |
| 'PuBu_r', f"Disminuci贸n de la temperatura por el tratamiento (+{value_trees}% cobertura arb贸rea)", | |
| "Cambio de temperatura (掳C)") | |
| return fig | |
| def trees_after_decrease(): | |
| gdf_temp = get_gdf_temp() | |
| value_temp = input.temp_goal() | |
| apply_filter = input.split_filter() | |
| if apply_filter == "Filtro": | |
| split = f'%total_trees_needed_for_{value_temp}C' | |
| else: | |
| split = False | |
| min_cobertura_veg = 5 | |
| max_cobertura = 100 | |
| try: | |
| fig = show_two_plots(gdf_temp, f'%total_trees_needed_for_{value_temp}C', '%CoberturaVeg', | |
| min_cobertura_veg, max_cobertura, 'YlGn', | |
| f"Estimaci贸n de cobertura arb贸rea necesaria para una disminuci贸n de {value_temp}掳C","Cobertura arb贸rea antes", | |
| "Cobertura arb贸rea (%)", split=split) | |
| return fig | |
| except Exception as e: | |
| print("Plotting error:", e) | |
| raise e | |
| def tree_coverage_change(): | |
| gdf_temp = get_gdf_temp() | |
| value_temp = input.temp_goal() | |
| min_tree_increase = 0 | |
| max_tree_increase = 90 | |
| fig = show_one_plot(gdf_temp, f'pp_trees_increase_for_{value_temp}C', min_tree_increase, | |
| max_tree_increase, 'Greens', | |
| f"Aumento de cobertura arb贸rea por disminuci贸n de {value_temp}掳C", | |
| "Cambio en cobertura arb贸rea (%pt)") | |
| return fig | |
| def download_csv(): | |
| if input.tab_choice() == "Efecto de los 谩rboles en la temperatura": | |
| df = get_gdf_trees() | |
| else: | |
| df = get_gdf_temp() | |
| if not hasattr(df, "to_csv"): | |
| raise ValueError("Expected a DataFrame or GeoDataFrame") | |
| df = df.drop(columns="geometry", errors="ignore") | |
| buffer = io.StringIO() | |
| df.to_csv(buffer, index=False) | |
| buffer.seek(0) | |
| return buffer | |
| app = App(app_ui, server) | |