Py.Cafe

ttinahooo/

dash-squirrel-census-insights

Squirrel Census Insights

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
# check out https://dash.plotly.com/ for Dash documentation
# check out https://plotly.com/python for Plotly documentation
# check out Plotly High Level refernece docs: https://plotly.com/python-api-reference/plotly.express.html

# Plotly Studio sign-up: https://docs.google.com/spreadsheets/d/1Z4DVKKXIeFOpe6mOfBGN5QAdYeJrW4X2mKT1UlxeYDE/edit?usp=sharing
# Adam's LinkedIn: https://www.linkedin.com/in/charming-data/

from dash import Dash, Input, Output, callback, dcc, html
import pandas as pd
import plotly.express as px

# Download data (Github): https://github.com/plotly/datasets/blob/master/2018_Central_Park_Squirrel_Census_-_Squirrel_Data_20250721.csv
# Downlaod data (NYC portal): https://data.cityofnewyork.us/Environment/2018-Central-Park-Squirrel-Census-Squirrel-Data/vfnx-vebw/about_data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/refs/heads/master/2018_Central_Park_Squirrel_Census_-_Squirrel_Data_20250721.csv')

"""
Use this sectiont to slice, clean, filter data to prepare it for the figure
"""
daily_counts = df.groupby("Shift").size().reset_index(name="Count")


# Build the figure
fig1 = px.bar(
    daily_counts,
    x="Shift",
    y="Count",
    title="",
    labels={"Count": "Number of Sightings", "Date": "Date"}
)


import numpy as np  # Required for np.where()

# Define color mapping (e.g., for a column named "Fur_Color")
color_map = {
    "Gray": "grey",
    "Cinnamon": "#D2691E",  # Hex code for cinnamon
    "Black": "black"
}

# Create the map with conditional size and custom colors
fig2 = px.scatter_map(
    df,
    lat="Y",
    lon="X",
    color="Primary Fur Color",  # Replace with your categorical column
    color_discrete_map=color_map,
    size=np.where(df["Age"] == "Adult", 15, 5),  # Smaller if "PM", else larger
    size_max=15,
    zoom=10,height=700)

app = Dash(__name__)
app.layout = html.Div(
    children=[
        # assign the fig to the 'figure' property to view
        dcc.Graph(figure=fig1),
        dcc.Graph(figure=fig2)
    ]
)

# App layout
# app.layout = [
#     html.Div(children='My First App with Data and a Graph'),
#     dash_table.DataTable(data=df.to_dict('records'), page_size=10),
#     dcc.Graph(figure=px.histogram(df, x='continent', y='lifeExp', histfunc='avg'))
# ]

# # Run the app
# if __name__ == '__main__':
#     app.run(debug=True)

# Plotly Studio sample -- https://centralparksquirrels.plotly.app