Skip to content

Commit 2594bd6

Browse files
committed
Moved data logic out of dashboard.
1 parent 03cfb6d commit 2594bd6

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

plotlydash_flask_tutorial/plotlydash/dashboard.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import dash_table
66
import dash_html_components as html
77
import dash_core_components as dcc
8+
from .data import create_dataframe
89
from .layout import html_layout
910

1011

@@ -19,13 +20,8 @@ def create_dashboard(server):
1920
]
2021
)
2122

22-
# Prepare a DataFrame
23-
df = pd.read_csv('data/311-calls.csv', parse_dates=['created'])
24-
df['created'] = df['created'].dt.date
25-
df.drop(columns=['incident_zip'], inplace=True)
26-
num_complaints = df['complaint_type'].value_counts()
27-
to_remove = num_complaints[num_complaints <= 30].index
28-
df.replace(to_remove, np.nan, inplace=True)
23+
# Load DataFrame
24+
df = create_dataframe()
2925

3026
# Custom HTML layout
3127
dash_app.index_string = html_layout
@@ -35,23 +31,21 @@ def create_dashboard(server):
3531
children=[dcc.Graph(
3632
id='histogram-graph',
3733
figure={
38-
'data': [
39-
{
40-
'x': df['complaint_type'],
41-
'text': df['complaint_type'],
42-
'customdata': df['key'],
43-
'name': '311 Calls by region.',
44-
'type': 'histogram'
45-
}
46-
],
34+
'data': [{
35+
'x': df['complaint_type'],
36+
'text': df['complaint_type'],
37+
'customdata': df['key'],
38+
'name': '311 Calls by region.',
39+
'type': 'histogram'
40+
}],
4741
'layout': {
4842
'title': 'NYC 311 Calls category.',
4943
'height': 500,
5044
'padding': 150
5145
}
5246
}),
53-
create_data_table(df)
54-
],
47+
create_data_table(df)
48+
],
5549
id='dash-container'
5650
)
5751
return dash_app.server
@@ -68,3 +62,4 @@ def create_data_table(df):
6862
page_size=300
6963
)
7064
return table
65+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pandas as pd
2+
3+
4+
def create_dataframe():
5+
"""Create Pandas DataFrame from local CSV."""
6+
df = pd.read_csv('data/311-calls.csv', parse_dates=['created'])
7+
df['created'] = df['created'].dt.date
8+
df.drop(columns=['incident_zip'], inplace=True)
9+
num_complaints = df['complaint_type'].value_counts()
10+
to_remove = num_complaints[num_complaints <= 30].index
11+
df.replace(to_remove, np.nan, inplace=True)
12+
return df

0 commit comments

Comments
 (0)