Skip to content

Commit a7cddd5

Browse files
committed
Improved Dash demo, updated naming conventions.
1 parent dffad65 commit a7cddd5

File tree

20 files changed

+8112
-1255
lines changed

20 files changed

+8112
-1255
lines changed

Pipfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ verify_ssl = true
66
[dev-packages]
77
lesscpy = "*"
88
cssmin = "*"
9-
jsmin = "*"
109

1110
[packages]
1211
Flask = "*"
1312
Flask_assets = "*"
14-
Pathlib = "*"
1513
Dash = "*"
1614
Dash_Table = "*"
1715
Dash_core_components = "*"

Pipfile.lock

Lines changed: 1 addition & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

application/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def create_app():
1515
app.register_blueprint(routes.main_bp)
1616

1717
# Import Dash application
18-
from application.dash_application.dash_example import Add_Dash
19-
app = Add_Dash(app)
18+
from application.plotlydash.dashboard import create_dashboard
19+
app = create_dashboard(app)
2020

2121
# Compile assets
2222
from application.assets import compile_assets

application/assets.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ def compile_assets(app):
1010
filters='less,cssmin',
1111
output='dist/css/styles.css',
1212
extra={'rel': 'stylesheet/less'})
13-
js_bundle = Bundle('js/*.js',
14-
filters='jsmin',
15-
output='dist/js/main.js')
1613
assets.register('less_all', less_bundle)
17-
assets.register('js_all', js_bundle)
1814
if app.config['FLASK_ENV'] == 'development':
1915
less_bundle.build(force=True)
20-
js_bundle.build()

application/dash_application/dash_example.py

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""Create a Dash app within a Flask app."""
2+
from pathlib import Path
3+
import dash
4+
import dash_table
5+
import dash_html_components as html
6+
import dash_core_components as dcc
7+
import plotly.graph_objects as go
8+
import pandas as pd
9+
import numpy as np
10+
from .layout import html_layout
11+
12+
13+
def create_dashboard(server):
14+
"""Create a Dash app."""
15+
external_stylesheets = ['/static/dist/css/styles.css',
16+
'https://fonts.googleapis.com/css?family=Lato',
17+
'https://use.fontawesome.com/releases/v5.8.1/css/all.css']
18+
external_scripts = ['/static/dist/js/includes/jquery.min.js',
19+
'/static/dist/js/main.js']
20+
dash_app = dash.Dash(server=server,
21+
external_stylesheets=external_stylesheets,
22+
external_scripts=external_scripts,
23+
routes_pathname_prefix='/dashapp/')
24+
25+
# Prepare a DataFrame
26+
df = pd.read_csv('data/311-calls.csv')
27+
num_complaints = df['complaint_type'].value_counts()
28+
to_remove = num_complaints[num_complaints <= 20].index
29+
df.replace(to_remove, np.nan, inplace=True)
30+
31+
# Override the underlying HTML template
32+
dash_app.index_string = html_layout
33+
34+
# Create Dash Layout comprised of Data Tables
35+
dash_app.layout = html.Div(
36+
children=[dcc.Graph(
37+
id='histogram-graph',
38+
figure={
39+
'data': [
40+
{
41+
'x': df['complaint_type'],
42+
'text': df['complaint_type'],
43+
'customdata': df['unique_key'],
44+
'name': '311 Calls by region.',
45+
'type': 'histogram'
46+
}
47+
],
48+
'layout': {
49+
'title': 'NYC 311 Calls category.',
50+
'height': 600,
51+
'padding': 150
52+
}
53+
}
54+
),
55+
create_data_table(df)],
56+
id='dash-container'
57+
)
58+
59+
return dash_app.server
60+
61+
62+
def create_data_table(df):
63+
"""Create table from Pandas DataFrame."""
64+
table_preview = dash_table.DataTable(
65+
id='database-table',
66+
columns=[{"name": i, "id": i} for i in df.columns],
67+
data=df.to_dict('records'),
68+
sort_action="native",
69+
sort_mode='native',
70+
page_size=300
71+
)
72+
return table_preview

application/dash_application/layout.py renamed to application/plotlydash/layout.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
<h1>Plotly Dash Flask Tutorial</h1>
1515
</a>
1616
<nav>
17-
<a href="/dashapp/"><i class="fas fa-chart-line"></i> Embdedded Plotly Dash</a>
1817
</nav>
1918
</div>
2019
</header>

application/static/dist/css/styles.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

application/static/js/main.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

application/static/less/header.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ header {
1919
}
2020

2121
.logo {
22-
width: 50px;
2322
display: inline-block;
23+
width: 40px;
2424
}
2525

2626
a {

0 commit comments

Comments
 (0)