Skip to content

Commit a8d4955

Browse files
author
David Cain
committed
Install and configure a basic assets pipeline
- Use Flask assets to bundle different JavaScript & fingerprint - Use Bower + hard-coded assets for simple JS dependency management For now, just specify Angular as a dependency. We'll expand upon this later.
1 parent 84326e3 commit a8d4955

File tree

9 files changed

+49
-4
lines changed

9 files changed

+49
-4
lines changed

.bowerrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"directory" : "todo/static/bower_components"
3+
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/.eggs
22
/todo_env
33
*.db
4+
/todo/static/gen/
5+
/todo/static/.webassets-cache/
6+
bower_components

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
# Installation guide
22

3+
## Retrieve assets with Bower
4+
5+
npm install -g bower
6+
bower install
7+
8+
## Set up a virtual environment
9+
310
python3 -m venv todo_env
411
source todo_env/bin/activate
512
pip3 install --editable .
13+
14+
## Run server
15+
616
export FLASK_APP=todo
717
flask run

bower.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "todo",
3+
"authors": [
4+
"David Cain <[email protected]>"
5+
],
6+
"description": "A simple Angular-based 'TODO' application",
7+
"dependencies": {
8+
"angular": ">=1.5.0"
9+
}
10+
}

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
include_package_data=True,
88
install_requires=[
99
'flask ~= 0.12',
10+
'flask-assets ~= 0.12',
1011
'alembic',
1112
'sqlalchemy ~= 1.0',
1213
],

todo/static/css/base.css

Whitespace-only changes.

todo/static/js/hello.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hi");

todo/templates/base.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
<meta charset="utf-8">
77

88
<title>TODO</title>
9-
<link href="/static/dist/styles.css" rel="stylesheet">
10-
{% block headscripting %}{% endblock %}
9+
{% assets "base_css" %}
10+
<link href="{{ ASSET_URL }}" rel="stylesheet">
11+
{% endassets %}
12+
<script src="/static/bower_components/angular/angular.js"></script>
1113
</head>
1214
<body {% block ng_app %}{% endblock %}>
1315
<div class="container" id="main-block">
14-
{% block page_header %}{% endblock %}
1516
{% block content %}
1617
{% endblock %}
1718
</div>
1819

19-
{% block scripting %}{% endblock %}
20+
{% assets "main_js" %}
21+
<script src="{{ ASSET_URL }}"></script>
22+
{% endassets %}
2023
</body>
2124
</html>

todo/todo.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33

44
from flask import Flask, g, render_template
5+
from flask_assets import Environment, Bundle
56

67

78
app = Flask(__name__)
@@ -37,6 +38,19 @@ def close_db(error):
3738
g.sqlite_db.close()
3839

3940

41+
# set up asset management
42+
assets = Environment(app)
43+
a_s = {
44+
'base_css': Bundle('css/base.css', output='gen/base.css'),
45+
46+
# all the JS for our single-page application
47+
'main_js': Bundle('js/hello.js', output='gen/main.js'),
48+
}
49+
50+
for ast, bun in a_s.items():
51+
assets.register(ast, bun)
52+
53+
4054
@app.route('/')
4155
def hello_world():
4256
return render_template('base.html')

0 commit comments

Comments
 (0)