Py.Cafe

AlisaAnny/

plotly-squirrel-sightings

Squirrel Sightings in Central Park

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
# 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 dash_ag_grid as dag
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_shift = df.groupby("Shift").size().reset_index(name="Count")



# Build the figure
fig_pie = px.pie(
    daily_counts_shift,
    names="Shift",
    values="Count",
    title="Daily Sightings by Shift",
    labels={"Count": "Number of Sightings", "Shift": "Shift"}
)

df['Date'] = pd.to_datetime(df['Date'].astype(str), format='%m%d%Y')

grid = dag.AgGrid(
    rowData=df.to_dict("records"),
    columnDefs=[{"field": i, 'filter': True, 'sortable': True} for i in df.columns],
    dashGridOptions={"pagination": True},
    # columnSize="sizeToFit"
)


daily_count = df.groupby("Date").size().reset_index(name="Count")

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

app = Dash()

app.layout = html.Div([
    grid,
    dcc.Graph(figure=fig_pie),
    dcc.Graph(figure=fig_bar)
])


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