Py.Cafe

jragh./

dash-consulting-expenses-analysis

Consulting Expenses Analysis for City Agencies

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
# check out https://dash.plotly.com/ for documentation
# And check out https://py.cafe/maartenbreddels for more examples

## Toronto Meetup PYthon Script ##
from dash import Dash, Input, Output, callback, dcc, html
import pandas as pd
import plotly.express as px

external_link = 'https://raw.githubusercontent.com/jragh/plotlymeetup/refs/heads/main/August_2025/2022%20Consulting%20Expenses.csv'

df = pd.read_csv(external_link)

df = df.copy().rename(columns={'EXPENSE\nCATEGORY': 'EXPENSE CATEGORY', ' EXPENDITURE': 'EXPENDITURE'})

# Cast 'EXPENDITURE' to integer
## Data Cleaning from PDF: Handling whitespace, commas, internal spaces ##
df['EXPENDITURE'] = df['EXPENDITURE'].str.strip()
df['EXPENDITURE'] = df['EXPENDITURE'].str.replace(',', '')
df['EXPENDITURE'] = df['EXPENDITURE'].str.replace(' ', '')
df['EXPENDITURE'] = pd.to_numeric(df['EXPENDITURE'])

## First Bar Chart: Grouping ##
df_agency_expense_group = df.groupby(['CITY AGENCY', 'EXPENSE CATEGORY'])['EXPENDITURE'].sum().reset_index()

## Adding in discrete color mapping ##
# discrete_color_map = {
#     'Creative Communications': '#233d4d',
#     'Information Technology': '#FE7F2D',
#     'Legal ': '#FCCA46',
#     'Management / Research & Development': '#A1C181',
#     'Technical ': '#619B8A'
# }


## Definition of Plotly Express Barchart ##
fig = px.bar(df_agency_expense_group,
    y='CITY AGENCY', 
    color='EXPENSE CATEGORY', 
    x='EXPENDITURE',
    orientation='h'
    ##,color_discrete_map = discrete_color_map
    )

## Inbuilt styling options ##
# fig.update_layout(
#     barcornerradius = '15%'
# )


app = Dash(__name__)


app.layout = html.Div(
    children=[
        
        dcc.Graph(figure=fig, style={'height': '75vh', 'width': '100vw'})

    ]
)