Py.Cafe

rhamon-garcia/

solara-button-click-tracker

Solara Button Click Tracker

DocsPricing
  • solara-geemap/
  • app.py
  • esa_stats_ucs_rj.csv
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import ee
import geemap
import plotly.express as px
import pandas as pd
import solara
import dataclasses
from typing import Callable, cast

df = pd.read_csv('esa_stats_ucs_rj.csv')
columns = list(df.columns)
#df = px.data.iris()

@dataclasses.dataclass
class ClickPoint:
    row_index: int
    x_column: str
    y_column: str

def find_row_index(fig, click_data):
    # goes from trace index and point index to row index in a dataframe
    # requires passing df.index as to custom_data
    trace_index = click_data["points"]["trace_indexes"][0]
    point_index = click_data["points"]["point_indexes"][0]
    trace = fig.data[trace_index]
    return trace.customdata[point_index][0]




class Map(geemap.Map):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.add_ee_data()
        self.add("layer_manager")
        self.add("inspector")

    def add_ee_data(self):
        # Add Earth Engine dataset

        esa = ee.ImageCollection('ESA/WorldCover/v200').first()

        ucs = ee.FeatureCollection("projects/ee-curso-gee-rhamon/assets/ucs_estaduais_rj")

       
        esa = esa.clipToCollection(ucs)



        # Set visualization parameters.

        visualization = {
             "bands": ['Map'],
        }   


        fc_vis_params = {
            "color": "000000",
            "width": 1,
            "Opacity": 0.8,
        }

        # Add Earth Engine layers to Map
        
        self.centerObject(ucs, zoom=8)
        self.addLayer(ucs, fc_vis_params, "UCs Estaduais RJ")
        self.addLayer(esa, visualization, "Landcover")
        self.add_legend(title="Land Cover Type", builtin_legend='ESA_WorldCover', position="bottomright")


@solara.component
def Page():
    with solara.Column(style={"min-width": "500px"}):
        Map.element(
            center=[40, -100],
            zoom=4,
            height="600px",
        )
        fig = px.pie(df,values='Class_10', names="nome", title='Cobertura Florestal UCs Estaduais RJ')
        #fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
        #solara.FigurePlotly(fig)

@solara.component
def ClickScatter(df, x, click_row, on_click: Callable[[ClickPoint], None]):
    x, set_x = solara.use_state(x)
    #y, set_y = solara.use_state(y)
    #fig = px.scatter(df, x, y, color=color, custom_data=[df.index])
    fig = px.pie(df,values= x, names='nome', title='Cobertura Florestal UCs Estaduais RJ')

    def on_click_trace(click_data):
        # sanity checks
        assert click_data["event_type"] == "plotly_click"
        row_index = find_row_index(fig, click_data)
        on_click(ClickPoint(row_index, x))

    if click_row:
        click_x = df[x].values[click_row]
        #fig.add_trace(px.scatter(x=, y=[click_y], text=["⭐️"]).data[0])
        fig.add_trace(px.pie(values= [click_x], names='nome', title='Cobertura Florestal UCs Estaduais RJ').data[0])
    # make the figure a bit smaller
    fig.update_layout(width=1340,height=800)
    with solara.Column(style={"width": "1540px"}) as main:
        solara.FigurePlotly(fig, on_click=on_click_trace)
        solara.Select(label="X-axis", value=x, values=columns, on_value=set_x)
    return main

@solara.component
def Page():
     with solara.Column(style={"min-width": "500px"}):
        Map.element(
            center=[-22, -43],
            zoom=8,
            height="600px",
        )


        click_point, set_click_point = solara.use_state(cast(ClickPoint, None))
        if click_point:
            clicked_row = click_point.row_index
        else:
            clicked_row = None

            with solara.Row(justify="center", style={"flex-wrap": "wrap"}):
                ClickScatter(df, 'nome', clicked_row, on_click=set_click_point)
            if click_point is not None:
                clicked_row = click_point.row_index
                solara.Success(f"Clicked on row {clicked_row}. Which is highlighted in the both plots.")
                solara.Markdown(
                    f"""
                ```python
                row_data = {df.iloc[clicked_row].to_dict()}
                ```"""
                )
            else:
                solara.Info("Click to select a point")