Py.Cafe

jragh./

dash-toronto-communicable-diseases-analysis

Toronto Communicable Diseases Analysis

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
# check out https://dash.plotly.com/ for documentation
# And check out https://py.cafe/maartenbreddels for more examples
from dash import Dash, Input, Output, callback, dcc, html
import plotly
import pandas as pd
import plotly.express as px

## Pandas Intake ##
external_link = 'https://raw.githubusercontent.com/jragh/plotlymeetup/refs/heads/main/October%202025/Toronto_Communicable_Diseases_2022_2024.csv'

raw_df = pd.read_csv(external_link, thousands = ',')

raw_df['Cases'] = pd.to_numeric(raw_df['Cases'])

print(raw_df.head(400))



## Assuming Toronto Population of 3,025,650, Across all three Years
toronto_pop = 3025647

## Create a Months Mapping Dictionary for ordering later on ##
months_mapping = {
    'January': 1,
    'February': 2,
    'March': 3,
    'April': 4,
    'May': 5,
    'June': 6,
    'July': 7,
    'August': 8,
    'September': 9,
    'October': 10,
    'November': 11,
    'December': 12
}

## Map the Month mapping dictionary into a new column
raw_df['Month Mapping'] = raw_df['Month'].map(months_mapping)

## New Column for Data ##
raw_df['Cases Per 100k'] = (raw_df['Cases'] / toronto_pop) * 100000

print(raw_df.columns)


## Basic Example Group By for Figure ##
disease_grouping_annual = raw_df.loc[(raw_df['Disease Category'] == 'Sexually Transmitted and Bloodborne')].groupby(['Disease', 'Year'])['Cases'].sum().reset_index()
print(disease_grouping_annual.head(25))

## Basic Figure for Displaying our Group By Data ##
disease_grouping_annual['Year'] = disease_grouping_annual['Year'].astype(str)

# basic_fig = px.bar(
#     disease_grouping_annual,
#     y='Disease',
#     x='Cases',
#     orientation='h',
#     color='Year',
#     barmode='group',
#     category_orders = {'Year': ['2022', '2023', '2024']}
# )


## Basic Fig Enhanced ##
## Figure Set Up ##
basic_fig = px.bar(
    disease_grouping_annual,
    y='Disease',
    x='Cases',
    orientation='h',
    color='Year',
    barmode='group',
    category_orders = {'Year': ['2022', '2023', '2024']},
    color_discrete_map = {'2022': '#71bdff', '2023': '#4281c0', '2024': '#0e4984'},
    text='Cases'
)

basic_fig.update_xaxes(showgrid=True, zeroline=False, showline=False, showticklabels=True, tickwidth=2, gridcolor="rgba(30, 63, 102, 0.15)", type="-")

basic_fig.update_yaxes(linewidth=2.5, showgrid=False, linecolor='rgb(180, 180, 180)')

basic_fig.update_layout(legend={
            'orientation':'h',
            'yanchor':"bottom",
            'y':1.02,
            'xanchor': 'center',
            'x': 0.5,
            'font': {'size': 12}}, 
            legend_title = {'text': 'Year', 'font': {'weight': 'bold', 'size': 12}},
            xaxis_title={'text':"Reported Cases", 'font': {'size': 12}},
            yaxis_title={'text':"Disease Listed", 'font': {'size': 12}},
            yaxis_tickfont={'size': 10},
            xaxis_tickfont={'size': 10},
            margin={'l':10, 'r': 10, 't': 10, 'b': 10},
            plot_bgcolor='#fff', paper_bgcolor="#fff",
            hovermode='y unified')

basic_fig.update_traces(textfont_size=8.5, textposition='outside', marker={"cornerradius":3})

app = Dash(__name__)

app.layout = (
    html.Div([
        dcc.Graph(figure=basic_fig)
    ])
)

app.layout = html.Div(

    html.Div([
        html.H3('Sexually Transmitted Diseases Report', style={'marginBottom': '0.25rem', 'marginTop': '0.5rem', 'font-family': "sans-serif", 'marginLeft': "0.4rem"}),
        html.Small('Cases, Toronto, 2022 - 2024', style={'color': 'grey', 'font-family': "sans-serif", "marginLeft": "0.4rem"}),
        dcc.Graph(figure=basic_fig, style={
            'height': 'calc(100% - 3rem)',
            'marginBottom': '0.35rem',
        })
    ], style={
        'width': '90vw', 
        'height': '80vh', 
        'borderRadius': '15px', 
        'borderColor': 'lightgrey', 
        'borderWidth': '1.35px',
        'padding': '0.5rem',
        'backgroundColor': '#fff',
        'borderStyle': 'solid',
        'boxShadow': '0 2px 12px rgba(0, 0, 0, 0.12)'})
    
, style={'height': '100vh', 'width': '100vw', 'background-color': '#fafafa', 'display': 'flex', 'justifyContent': 'center'})