Py.Cafe

ptan6997aaa/

vizro-iris-visualizer

Insightful Iris Data Visualizer

DocsPricing
  • Planisware-Idea-Details-Data.csv
  • 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
############ Imports ##############
import vizro.models as vm
import vizro.figures as vf
from vizro.models.types import capture
from vizro import Vizro
import pandas as pd
from vizro.managers import data_manager
import plotly.graph_objects as go
import re
from datetime import datetime


####### Helper Functions for Data Preprocessing ######
def parse_planisware_date(value) -> pd.Timestamp:
    """Parse mixed Planisware date formats into a pandas Timestamp (or NaT)."""
    if pd.isna(value):
        return pd.NaT
    s = str(value).strip()
    if not s or s.lower() == 'nan':
        return pd.NaT

    # Examples: 121825123125 (MMDDYYhhmmss)
    if re.fullmatch(r'\d{12}', s):
        try:
            return pd.Timestamp(datetime.strptime(s, '%m%d%y%H%M%S'))
        except ValueError:
            return pd.NaT

    # Examples: 070125 (MMDDYY)
    if re.fullmatch(r'\d{6}', s):
        try:
            return pd.Timestamp(datetime.strptime(s, '%m%d%y'))
        except ValueError:
            return pd.NaT

    # Fallback for standard formats
    return pd.to_datetime(s, errors='coerce')


def normalize_dim(series: pd.Series, unknown_label: str = 'Unknown') -> pd.Series:
    """Strip whitespace and replace blanks/NaN with 'Unknown'."""
    s = series.fillna('').astype(str).str.strip()
    s = s.replace({'': unknown_label, 'nan': unknown_label, 'None': unknown_label})
    return s


def preprocess_planisware_data(df):
    """Clean and preprocess the Planisware data."""
    # Handle ID column
    id_col = 'qa_id'
    df[id_col] = normalize_dim(df.get(id_col, pd.Series([None] * len(df))), unknown_label='')
    
    # If IDs are blank, fallback to index
    mask_blank_id = df[id_col].eq('')
    df.loc[mask_blank_id, id_col] = df.loc[mask_blank_id].index.astype(str)

    # Normalize dimensions
    cols_to_normalize = [
        'Submitted by', 'Business/Project Owner', 'Global Approver', 
        'Projected Timeline', 'Idea status', 'Project Location'
    ]
    for col in cols_to_normalize:
        if col in df.columns:
            df[col] = normalize_dim(df[col])

    # Date Parsing
    if 'Date Created' in df.columns:
        df['_created_dt'] = df['Date Created'].apply(parse_planisware_date)
        # Create YYYY-MM string for grouping
        df['Created YM'] = df['_created_dt'].dt.to_period('M').astype(str)
        df['Created YM'] = df['Created YM'].replace({'NaT': 'Unknown'}).fillna('Unknown')
    
    return df


####### Custom Chart Functions ######
@capture("graph")
def submitter_bar_chart(data_frame):
    """Horizontal bar chart showing top 15 submitters by project count."""
    # Aggregate data
    data = (
        data_frame.groupby("Submitted by")["qa_id"]
        .nunique()
        .reset_index(name="Projects")
        .sort_values("Projects", ascending=False)
        .head(15)
        .sort_values("Projects", ascending=True)
    )

    # Create figure
    fig = go.Figure()
    fig.add_trace(
        go.Bar(
            x=data["Projects"],
            y=data["Submitted by"],
            orientation="h",
            marker=dict(color="#636EFA"),
        )
    )

    fig.update_layout(
        xaxis_title="Number of Projects",
        yaxis_title="",
        height=400,
        margin=dict(l=20, r=20, t=10, b=20),
        plot_bgcolor="rgba(0,0,0,0)",
        paper_bgcolor="rgba(0,0,0,0)",
    )

    return fig


@capture("graph")
def monthly_trend_chart(data_frame):
    """Bar chart showing projects created by month."""
    # Aggregate data by month
    data = (
        data_frame[data_frame["Created YM"] != "Unknown"]
        .groupby("Created YM")["qa_id"]
        .nunique()
        .reset_index(name="Projects")
        .sort_values("Created YM")
    )

    # Create figure
    fig = go.Figure()
    fig.add_trace(
        go.Bar(x=data["Created YM"], y=data["Projects"], marker=dict(color="#636EFA"))
    )

    fig.update_layout(
        xaxis_title="Month",
        yaxis_title="Number of Projects",
        height=400,
        margin=dict(l=20, r=20, t=10, b=20),
        plot_bgcolor="rgba(0,0,0,0)",
        paper_bgcolor="rgba(0,0,0,0)",
    )

    fig.update_xaxes(tickangle=-45)

    return fig


@capture("graph")
def timeline_donut_chart(data_frame):
    """Donut chart showing projects by projected timeline."""
    # Aggregate data
    data = (
        data_frame.groupby("Projected Timeline")["qa_id"]
        .nunique()
        .reset_index(name="Projects")
        .sort_values("Projects", ascending=False)
    )

    # Create figure
    fig = go.Figure()
    fig.add_trace(
        go.Pie(
            labels=data["Projected Timeline"],
            values=data["Projects"],
            hole=0.5,
            textposition="inside",
            textinfo="percent+label",
        )
    )

    fig.update_layout(
        height=400,
        margin=dict(l=20, r=20, t=10, b=20),
        showlegend=False,
        paper_bgcolor="rgba(0,0,0,0)",
    )

    return fig


@capture("graph")
def status_donut_chart(data_frame):
    """Donut chart showing projects by status."""
    # Aggregate data
    data = (
        data_frame.groupby("Idea status")["qa_id"]
        .nunique()
        .reset_index(name="Projects")
        .sort_values("Projects", ascending=False)
    )

    # Create figure
    fig = go.Figure()
    fig.add_trace(
        go.Pie(
            labels=data["Idea status"],
            values=data["Projects"],
            hole=0.5,
            textposition="inside",
            textinfo="percent+label",
        )
    )

    fig.update_layout(
        height=400,
        margin=dict(l=20, r=20, t=10, b=20),
        showlegend=False,
        paper_bgcolor="rgba(0,0,0,0)",
    )

    return fig


@capture("graph")
def location_bar_chart(data_frame):
    """Horizontal bar chart showing top 15 locations by project count."""
    # Aggregate data
    data = (
        data_frame.groupby("Project Location")["qa_id"]
        .nunique()
        .reset_index(name="Projects")
        .sort_values("Projects", ascending=False)
        .head(15)
        .sort_values("Projects", ascending=True)
    )

    # Create figure
    fig = go.Figure()
    fig.add_trace(
        go.Bar(
            x=data["Projects"],
            y=data["Project Location"],
            orientation="h",
            marker=dict(color="#636EFA"),
        )
    )

    fig.update_layout(
        xaxis_title="Number of Projects",
        yaxis_title="",
        height=400,
        margin=dict(l=20, r=20, t=10, b=20),
        plot_bgcolor="rgba(0,0,0,0)",
        paper_bgcolor="rgba(0,0,0,0)",
    )

    return fig


####### Data Manager Settings #####
# Load and preprocess data
df = pd.read_csv("Planisware-Idea-Details-Data.csv")
df = preprocess_planisware_data(df)
data_manager["planisware_data"] = df


########### Dashboard Model ############
dashboard = vm.Dashboard(
    pages=[
        vm.Page(
            components=[
                # Row 1: KPI Cards
                vm.Container(
                    components=[
                        vm.Figure(
                            id="kpi_total_projects",
                            figure=vf.kpi_card(
                                data_frame="planisware_data",
                                value_column="qa_id",
                                value_format="{value:,.0f}",
                                agg_func="nunique",
                                title="Total Projects",
                                icon="folder",
                            ),
                        ),
                        vm.Figure(
                            id="kpi_total_submitter",
                            figure=vf.kpi_card(
                                data_frame="planisware_data",
                                value_column="Submitted by",
                                value_format="{value:,.0f}",
                                agg_func="nunique",
                                title="Total Submitters",
                                icon="person",
                            ),
                        ),
                        vm.Figure(
                            id="kpi_total_owner",
                            figure=vf.kpi_card(
                                data_frame="planisware_data",
                                value_column="Business/Project Owner",
                                value_format="{value:,.0f}",
                                agg_func="nunique",
                                title="Total Owners",
                                icon="business_center",
                            ),
                        ),
                        vm.Figure(
                            id="kpi_total_approver",
                            figure=vf.kpi_card(
                                data_frame="planisware_data",
                                value_column="Global Approver",
                                value_format="{value:,.0f}",
                                agg_func="nunique",
                                title="Total Approvers",
                                icon="check_circle",
                            ),
                        ),
                    ],
                    layout=vm.Flex(direction="row", gap="160px", wrap=False),
                ),
                # Row 2: Submitter and Monthly Trend Charts
                vm.Container(
                    components=[
                        vm.Graph(
                            id="submitter_chart",
                            figure=submitter_bar_chart(data_frame="planisware_data"),
                            title="Total Projects by Submitter",
                        ),
                        vm.Graph(
                            id="monthly_trend_chart",
                            figure=monthly_trend_chart(data_frame="planisware_data"),
                            title="Total Projects Created by Month",
                        ),
                    ],
                    layout=vm.Flex(direction="row", gap="16px", wrap=False),
                ),
                # Row 3: Timeline, Status, and Location Charts
                vm.Container(
                    components=[
                        vm.Graph(
                            id="timeline_chart",
                            figure=timeline_donut_chart(data_frame="planisware_data"),
                            title="Total Projects by Timeline",
                        ),
                        vm.Graph(
                            id="status_chart",
                            figure=status_donut_chart(data_frame="planisware_data"),
                            title="Total Projects by Status",
                        ),
                        vm.Graph(
                            id="location_chart",
                            figure=location_bar_chart(data_frame="planisware_data"),
                            title="Total Projects by Location",
                        ),
                    ],
                    layout=vm.Flex(direction="row", gap="16px", wrap=False),
                ),
            ],
            title="Planisware Project Overview",
            layout=vm.Flex(direction="column", gap="24px"),
        )
    ],
    theme="vizro_light",
    title="Planisware Dashboard",
)

# Build and run the app
app = Vizro().build(dashboard)

if __name__ == "__main__":
    app.run(debug=True, port=8050)