Skip to content

Commit a95eab7

Browse files
committed
Refactor.
1 parent 8c54351 commit a95eab7

28 files changed

+509
-93
lines changed

.env.example

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FLASK_ENV=development
2+
FLASK_APP=wsgi.py
3+
FLASK_DEBUG=True
4+
LESS_BIN=/usr/local/bin/lessc

Pipfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ url = "https://pypi.org/simple"
44
verify_ssl = true
55

66
[dev-packages]
7-
Lesscpy = "*"
8-
CSSMin = "*"
9-
JSMin = "*"
107

118
[packages]
129
Flask = "*"
1310
Flask-Assets = "*"
1411
Python-Dotenv = "*"
12+
Lesscpy = "*"
13+
CSSMin = "*"
14+
JSMin = "*"
1515

1616
[requires]
1717
python_version = "3.8"

README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Flask-Assets Tutorial
22

3-
![Python](https://img.shields.io/badge/Python-v^3.8-blue.svg?logo=python&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
4-
![Flask](https://img.shields.io/badge/Flask-v1.1.2-blue.svg?longCache=true&logo=flask&style=flat-square&logoColor=white&colorB=5e81ac&colorA=4c566a)
3+
![Python](https://img.shields.io/badge/Python-v3.8-blue.svg?logo=python&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
4+
![Flask](https://img.shields.io/badge/Flask-v1.1.1-blue.svg?longCache=true&logo=flask&style=flat-square&logoColor=white&colorB=5e81ac&colorA=4c566a)
55
![Flask-Assets](https://img.shields.io/badge/Flask--Assets-v2.0-blue.svg?longCache=true&logo=flask&style=flat-square&logoColor=white&colorB=5e81ac&colorA=4c566a)
66
![GitHub Last Commit](https://img.shields.io/github/last-commit/google/skia.svg?style=flat-square&colorA=4c566a&colorB=a3be8c&logo=GitHub)
77
[![GitHub Issues](https://img.shields.io/github/issues/hackersandslackers/flask-assets-tutorial.svg?style=flat-square&colorA=4c566a&logo=GitHub&colorB=ebcb8b)](https://github.com/hackersandslackers/flask-assets-tutorial/issues)
8-
[![GitHub Stars](https://img.shields.io/github/stars/hackersandslackers/flask-assets-tutorial.svg?style=flat-square8&colorA=4c566a&logo=GitHub&colorB=ebcb8b)](https://github.com/hackersandslackers/flask-assets-tutorial/stargazers)
8+
[![GitHub Stars](https://img.shields.io/github/stars/hackersandslackers/flask-assets-tutorial.svg?style=flat-square&colorA=4c566a&logo=GitHub&colorB=ebcb8b)](https://github.com/hackersandslackers/flask-assets-tutorial/stargazers)
99
[![GitHub Forks](https://img.shields.io/github/forks/hackersandslackers/flask-assets-tutorial.svg?style=flat-square&colorA=4c566a&logo=GitHub&colorB=ebcb8b)](https://github.com/hackersandslackers/flask-assets-tutorial/network)
1010

1111
![Flask Assets Tutorial](https://github.com/hackersandslackers/flask-assets-tutorial/blob/master/.github/[email protected]?raw=true)
@@ -34,10 +34,18 @@ $ pipenv shell
3434
$ pipenv update
3535
$ flask run
3636
```
37-
-----
3837

39-
**Hackers and Slackers** tutorials are free of charge. If you found this tutorial helpful, a [small donation](https://www.buymeacoffee.com/hackersslackers) would be greatly appreciated to keep us in business. All proceeds go towards coffee, and all coffee goes towards more content.
40-
-----
38+
## Usage
39+
40+
Replace the values in **.env.example** with your values and rename this file to **.env**:
4141

42-
**Hackers and Slackers** tutorials are free of charge. If you found this tutorial helpful, a [small donation](https://www.buymeacoffee.com/hackersslackers) would be greatly appreciated to keep us in business. All proceeds go towards coffee, and all coffee goes towards more content.
42+
* `FLASK_APP`: Entry point of your application (should be `wsgi.py`).
43+
* `FLASK_ENV`: The environment to run your app in (either `development` or `production`).
44+
* `SECRET_KEY`: Randomly generated string of characters used to encrypt your app's data.
45+
* `LESS_BIN`: Path to your local LESS installation via `which lessc`.
46+
47+
*Remember never to commit secrets saved in .env files to Github.*
48+
49+
-----
4350

51+
**Hackers and Slackers** tutorials are free of charge. If you found this tutorial helpful, a [small donation](https://www.buymeacoffee.com/hackersslackers) would be greatly appreciated to keep us in business. All proceeds go towards coffee, and all coffee goes towards more content.

application/assets.py

-28
This file was deleted.

application/main/main_routes.py

-35
This file was deleted.

config.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
77

88

99
class Config:
10+
"""Set Flask configuration from .env file."""
1011

11-
# General
12+
# General Config
13+
SECRET_KEY = environ.get('SECRET_KEY')
14+
FLASK_APP = environ.get('FLASK_APP')
1215
FLASK_ENV = environ.get('FLASK_ENV')
13-
FLASK_DEBUG = 'wsgi.py'
14-
STATIC_FOLDER = 'static'
15-
TEMPLATES_FOLDER = 'templates'
1616

1717
# Flask-Assets
18-
LESS_BIN = '/usr/local/bin/lessc'
19-
ASSETS_DEBUG = False
20-
ASSETS_AUTO_BUILD = True
18+
LESS_BIN = environ.get('LESS_BIN')
19+
ASSETS_DEBUG = True
20+
LESS_RUN_IN_DEBUG = True
21+
22+
# Static Assets
23+
STATIC_FOLDER = 'static'
24+
TEMPLATES_FOLDER = 'templates'
25+
COMPRESSOR_DEBUG = True
2126

application/__init__.py renamed to flask_assets_tutorial/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
"""Initialize app."""
1+
"""Initialize Flask app."""
22
from flask import Flask
33
from flask_assets import Environment
44

55

66
def create_app():
7-
"""Construct the core application."""
7+
"""Construct core Flask app."""
88
app = Flask(__name__, instance_relative_config=False)
99
app.config.from_object('config.Config')
1010
assets = Environment()
@@ -14,7 +14,7 @@ def create_app():
1414
assets.init_app(app)
1515

1616
with app.app_context():
17-
# Import parts of our application
17+
# Import parts of our flask_assets_tutorial
1818
from .admin import admin_routes
1919
from .main import main_routes
2020
from .assets import compile_static_assets

application/admin/admin_routes.py renamed to flask_assets_tutorial/admin/admin_routes.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
__name__,
88
template_folder='templates',
99
static_folder='static'
10-
) # Blueprint Configuration
10+
)
1111

1212

1313
@admin_bp.route('/dashboard', methods=['GET'])
1414
def dashboard():
1515
"""Admin dashboard route."""
16-
return render_template('dashboard.jinja2',
17-
title='Admin Dashboard | Flask-Blueprint Tutorial',
18-
template='dashboard-static account',
19-
body="Account")
16+
return render_template(
17+
'dashboard.jinja2',
18+
title='Admin Dashboard | Flask-Blueprint Tutorial',
19+
template='dashboard-static account',
20+
body="Account"
21+
)

flask_assets_tutorial/assets.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Compile static assets."""
2+
from flask import current_app as app
3+
from flask_assets import Bundle
4+
5+
6+
def compile_static_assets(assets):
7+
"""Configure and build asset bundles."""
8+
main_style_bundle = Bundle(
9+
'src/less/*.less',
10+
'main_bp/homepage.less',
11+
filters='less,cssmin',
12+
output='dist/css/landing.css',
13+
extra={'rel': 'stylesheet/css'}
14+
) # Main Stylesheets Bundle
15+
main_js_bundle = Bundle(
16+
'src/js/main.js',
17+
filters='jsmin',
18+
output='dist/js/main.min.js'
19+
) # Main JavaScript Bundle
20+
admin_style_bundle = Bundle(
21+
'src/less/*.less',
22+
'admin_bp/admin.less',
23+
filters='less,cssmin',
24+
output='dist/css/account.css',
25+
extra={'rel': 'stylesheet/css'}
26+
) # Admin Stylesheets Bundle
27+
assets.register('main_styles', main_style_bundle)
28+
assets.register('main_js', main_js_bundle)
29+
assets.register('admin_styles', admin_style_bundle)
30+
if app.config['FLASK_ENV'] == 'development': # Only rebuild bundles in development
31+
main_style_bundle.build()
32+
main_js_bundle.build()
33+
admin_style_bundle.build()
34+
return assets
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Routes for main pages."""
2+
from flask import Blueprint, render_template
3+
4+
5+
main_bp = Blueprint(
6+
'main_bp',
7+
__name__,
8+
template_folder='templates',
9+
static_folder='static'
10+
)
11+
12+
13+
@main_bp.route('/', methods=['GET'])
14+
def home():
15+
"""Homepage route."""
16+
return render_template(
17+
'index.jinja2',
18+
title='Home | Flask-Blueprint Tutorial',
19+
template='home-static main',
20+
body="Home"
21+
)
22+
23+
24+
@main_bp.route('/about', methods=['GET'])
25+
def about():
26+
"""About page route."""
27+
return render_template(
28+
'index.jinja2',
29+
title='About | Flask-Blueprint Tutorial',
30+
template='about-static main',
31+
body="About"
32+
)
33+
34+
35+
@main_bp.route('/etc', methods=['GET'])
36+
def etc():
37+
"""Etc page route."""
38+
return render_template(
39+
'index.jinja2',
40+
title='Etc | Flask-Blueprint Tutorial',
41+
template='etc-static main',
42+
body="Etc"
43+
)

0 commit comments

Comments
 (0)