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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 | class indicator_area:
"""
Title
-----
Indicator area object
Description
-----------
Indicator area objects contain a list of Indicator objects for a single
indicator service area in a single local authority.
Examples
--------
>>> data = indicator.example_indicator()
>>> output = indicator_area(data = [data], id = "area")
>>> output
>>> output.data
>>> output.id
"""
def __init__(self, x: list[indicator], id: str):
"""
Parameters
----------
x: list
A list of indicator objects.
id: str
A string Id for indicator area object.
"""
self._data = deepcopy(x)
self._id = id
self._validate()
def __str__(self):
output = f"""
Indicator area object
- id: {self.id}
- N indicators: {len(self.data)}
"""
return output
def __repr__(self):
output = f"""
Indicator area object
- id: {self.id}
- N indicators: {len(self.data)}
"""
return output
def _validate(self):
assert isinstance(self.id, str), "id must be a string"
assert isinstance(self.data, list), "data is not list"
assert all([isinstance(i, indicator) for i in self.data]), (
"indicator area objects must only contain indicator objects"
)
def __setattr__(self, name, value):
old_value = getattr(self, name, None)
super().__setattr__(name, value)
if not name.startswith("_"):
try:
self._validate()
except Exception as e:
super().__setattr__(name, old_value)
raise e
@property
def data(self):
return self._data
@data.setter
def data(self, value: list[indicator]):
"""
Title
-----
Set data attribute for indicator object.
Parameters
----------
value: list
A list of indicator objects.
"""
self._data = deepcopy(value)
@property
def id(self):
return self._id
@id.setter
def id(self, value: str):
"""
Title
-----
Set id attribute for indicator area object.
Parameters
----------
value: string
A string id.
"""
self._id = deepcopy(value)
@staticmethod
@module.ui
def mod_ui(object: indicator_area):
authorities = [i.authority() for i in object.data]
return ui.nav_panel(
object.id,
ui.card(
ui.card_header(object.id),
tags.h6(
"""
Indicator data for a selected local authority is
visualised as a series of interactive line plots.
Data are stratified into categories; 'Performance'
'Financial' and 'Satisfaction', which are displayed
in independent boxes. Each box contains a menu
allowing navigation between different indicator data
sets.
"""
),
tags.h6(
"The ",
ui.a(
"LGBF",
href="https://www.improvementservice.org.uk/benchmarking/home",
target="_blank",
),
"""
describes the process of how these metrics have been
developed and how councils have been organised into 'family
groups'. This essentially means that similar councils in
terms of levels of deprivation and population are compared
to each other. Indicator data is visualised as a series of
interactive plots and tables showing the indicator value for
a selected local authority alongside the indicator value
summarisied by council family group and nationally across
Scotland. The values used to derive the indicator metric,
if applicable, are also presented.
""",
),
ui.input_select(
id="select_authority",
label="Select authority",
choices=authorities,
selected=authorities[0],
),
ui.output_ui("indicator_boxes"),
),
)
@staticmethod
@module.server
def mod_server(input, output, session, object: indicator_area, is_dark):
@render.ui
def indicator_boxes():
req(input.select_authority)
selected_authority = input.select_authority()
logger.info(f"Selected {selected_authority}")
data = object._split_by_category(selected_authority)
output = []
for key, value in data.items():
indicators = [ind for ind in value["data"].tolist()]
output += [
ui.navset_card_tab(
ui.nav_spacer(),
ui.nav_menu(
"Select indicator",
*[
ind.mod_ui(clean_id(ind.id()), ind)
for ind in indicators
],
),
title=key,
)
]
[ind.mod_server(clean_id(ind.id()), ind, is_dark) for ind in indicators]
return ui.div(*output)
def _split_by_category(self, authority: str):
output = [i for i in self.data if i.authority() == authority]
output = pd.DataFrame(
{"category": [i.category() for i in output], "data": output}
)
output = {name: group for name, group in output.groupby("category")}
return output
@staticmethod
def example_indicator_area():
metadata = example_lgbf_metadata()
data = example_lgbf_data()
indicator_data = data.merge(
metadata, on="Indicators_Information_Code", how="inner"
)
grps = ["Indicators_Information_Code", "LA_Information_LocalAuthority"]
output = indicator_area(
x=[indicator(group) for _, group in indicator_data.groupby(grps)],
id="service_area",
)
return output
|