# 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'})