Skip to content

Commit 455d79b

Browse files
committed
Refactored Flask-Assets logic.
1 parent 2f0f261 commit 455d79b

File tree

6 files changed

+61
-46
lines changed

6 files changed

+61
-46
lines changed

application/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
"""Initialize Flask app."""
22
from flask import Flask
3+
from flask_assets import Environment
34

45

56
def create_app():
67
"""Construct core Flask application with embedded Dash app."""
78
app = Flask(__name__, instance_relative_config=False)
89
app.config.from_object('config.Config')
10+
assets = Environment()
11+
assets.init_app(app)
912

1013
with app.app_context():
11-
# Import Flask routes
12-
from application import routes
14+
# Import parts of our core Flask app
15+
from . import routes
16+
from .assets import compile_static_assets
1317

1418
# Import Dash application
15-
from application.plotlydash.dashboard import create_dashboard
19+
from .plotlydash.dashboard import create_dashboard
1620
app = create_dashboard(app)
1721

18-
# Compile CSS
19-
if app.config['FLASK_ENV'] == 'development':
20-
from application.assets import compile_assets
21-
compile_assets(app)
22+
# Compile static assets
23+
compile_static_assets(assets)
2224

2325
return app

application/assets.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
from flask_assets import Environment, Bundle
1+
"""Compile static assets."""
2+
from flask import current_app as app
3+
from flask_assets import Bundle
24

35

4-
def compile_assets(app):
5-
"""Compile stylesheets if `app` is running in development mode."""
6-
assets = Environment(app)
7-
Environment.auto_build = True
8-
Environment.debug = False
6+
def compile_static_assets(assets):
7+
"""
8+
Compile stylesheets if in development mode.
9+
10+
:param assets: Flask-Assets Environment
11+
:type assets: Environment
12+
"""
13+
assets.auto_build = True
14+
assets.debug = False
915
less_bundle = Bundle('less/*.less',
1016
filters='less,cssmin',
1117
output='dist/css/styles.css',
1218
extra={'rel': 'stylesheet/less'})
1319
assets.register('less_all', less_bundle)
14-
less_bundle.build(force=True)
20+
if app.config['FLASK_ENV'] == 'development':
21+
less_bundle.build()
22+
return assets

application/plotlydash/dashboard.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ def create_dashboard(server):
1212
"""Create a Plotly Dash dashboard."""
1313
dash_app = dash.Dash(server=server,
1414
routes_pathname_prefix='/dashapp/',
15-
external_stylesheets=['/static/dist/css/styles.css',
16-
'https://fonts.googleapis.com/css?family=Lato']
15+
external_stylesheets=[
16+
'/static/dist/css/styles.css',
17+
'https://fonts.googleapis.com/css?family=Lato'
18+
]
1719
)
1820

1921
# Prepare a DataFrame

application/plotlydash/layout.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
html_layout = '''<!DOCTYPE html>
2-
<html>
3-
<head>
4-
{%metas%}
5-
<title>{%title%}</title>
6-
{%favicon%}
7-
{%css%}
8-
</head>
9-
<body class="dash-template">
10-
<header>
11-
<div class="nav-wrapper">
12-
<a href="/">
13-
<img src="/static/img/logo.png" class="logo" />
14-
<h1>Plotly Dash Flask Tutorial</h1>
15-
</a>
16-
<nav>
17-
</nav>
18-
</div>
19-
</header>
20-
{%app_entry%}
21-
<footer>
22-
{%config%}
23-
{%scripts%}
24-
{%renderer%}
25-
</footer>
26-
</body>
27-
</html>'''
1+
"""Plotly Dash HTML layout override."""
2+
3+
html_layout = '''
4+
<!DOCTYPE html>
5+
<html>
6+
<head>
7+
{%metas%}
8+
<title>{%title%}</title>
9+
{%favicon%}
10+
{%css%}
11+
</head>
12+
<body class="dash-template">
13+
<header>
14+
<div class="nav-wrapper">
15+
<a href="/">
16+
<img src="/static/img/logo.png" class="logo" />
17+
<h1>Plotly Dash Flask Tutorial</h1>
18+
</a>
19+
<nav>
20+
</nav>
21+
</div>
22+
</header>
23+
{%app_entry%}
24+
<footer>
25+
{%config%}
26+
{%scripts%}
27+
{%renderer%}
28+
</footer>
29+
</body>
30+
</html>
31+
'''

application/static/less/table.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ table {
203203
padding: 15px 10px !important;
204204
overflow: hidden !important;
205205
border: 0 !important;
206-
font-size: 0.73em !important;
206+
font-size: 0.65em !important;
207207
line-height: 1.25 !important;
208208
text-align: center !important;
209209
}

config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
"""App config."""
1+
"""Flask config."""
22
from os import environ, path
33
from dotenv import load_dotenv
44

5-
65
basedir = path.abspath(path.dirname(__file__))
76
load_dotenv(path.join(basedir, '.env'))
87

0 commit comments

Comments
 (0)