Py.Cafe

petar-qb/

issue-1084-1

Interactive Analysis of Judicial Data by Circunscription

DocsPricing
  • app.py
  • 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
135
136
137
138
139
140
141
142
### Code/Examples

import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro.actions import filter_interaction
from vizro.tables import dash_ag_grid

import pandas as pd

# 1) Original DataFrame
data_fija = [
    ("Trelew",   "con acusación", 60),
    ("Trelew",   "sin acusación", 45),
    ("Trelew",   "queja"        , 15),
    ("Trelew",   "consulta STJ" ,  7),

    ("Comodoro", "con acusación", 80),
    ("Comodoro", "sin acusación", 66),
    ("Comodoro", "queja",         10),
    ("Comodoro", "consulta STJ",  9),

    ("Rawson",   "con acusación",   50),
    ("Rawson",   "sin acusación",   35),
    ("Rawson",   "queja",           3),
    ("Rawson",   "consulta STJ",    7),
]

df = pd.DataFrame(
    data_fija,
    columns=["Circunscripción", "descItem", "Valor"]
)

# 2) Grouping by circunscripción and descItem
df_group = (
    df
    .groupby(["Circunscripción", "descItem"], as_index=False)
    .agg({"Valor": "sum"})
)

# 3) Creating the "Total" row
df_total = (
    df
    .groupby(["descItem"], as_index=False)
    .agg({"Valor": "sum"})
)
df_total["Circunscripción"] = "Total"

# 4) Merging real circunscripciones with "Total"
df_table = pd.concat([df_group, df_total], ignore_index=True)

# 5) Summing by circunscripción for the bar chart
df_chart = (
    df_table
    .groupby("Circunscripción", as_index=False)
    .agg({"Valor": "sum"})
)

# 6) Setting up the bar chart with custom_data=["Circunscripción"]
fig_bar = px.bar(
    df_group,
    x="Circunscripción",
    y="Valor",
    title="Circunscripciones",
    custom_data=["Circunscripción"]
)

from vizro.models.types import capture


@capture("action")
def overwrite_filter_model(circunscripcion_click_data):
    """Set AgGrid filterModel equals value."""
    extracted_circunscripcion_value = circunscripcion_click_data["points"][0]["customdata"][0]

    return {
         "Circunscripción": {
             "type": "equals",
             "filter": extracted_circunscripcion_value
         }
    }


graph_circunscripcion = vm.Graph(
    id="fig_bar",
    figure=fig_bar,
    actions=[
        vm.Action(
            function=overwrite_filter_model(),
            inputs=["fig_bar.clickData"],
            outputs=["underlying_mi_tabla_aggrid.filterModel"],
        )
    ]
)

# 7) Defining columnDefs and hiding "Circunscripción" so it's still filterable
columnDefs = [
    {
        "field": "Circunscripción",
        "hide": True
    },
    {
        "field": "descItem"
    },
    {
        "field": "Valor"
    },
]

# 8) Applying a default filterModel to show only "Total" on load
tabla_aggrid = vm.AgGrid(
    id="mi_tabla_aggrid",
    figure=dash_ag_grid(
        id="underlying_mi_tabla_aggrid",   # <--- ID added here
        data_frame=df_table,
        columnDefs=columnDefs,
        filterModel={
             "Circunscripción": {
                 "type": "equals",
                 "filter": "Total"
             }
        }
    )
)

# 9) Building the page
page = vm.Page(
    title="Total + Circunscripciones (Filter on click)",
    components=[
        graph_circunscripcion,
        tabla_aggrid,
    ],
)

# 10) Creating the dashboard and running the app
dashboard = vm.Dashboard(
    title="Example with 'Total' row and initial filter in the table",
    pages=[page]
)

Vizro().build(dashboard).run()