Skip to content

Commit 1a7545b

Browse files
committed
Refactor.
1 parent ae4dfa8 commit 1a7545b

File tree

38 files changed

+648
-100
lines changed

38 files changed

+648
-100
lines changed

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FLASK_APP=wsgi.py
2+
FLASK_ENV=development
3+
SECRET_KEY=randomstringofcharacters
4+
LESS_BIN=/usr/local/bin/lessc
5+
ASSETS_DEBUG=False
6+
LESS_RUN_IN_DEBUG=False
7+
COMPRESSOR_DEBUG=True

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,21 @@ $ pipenv update
4040
$ flask run
4141
```
4242

43-
## Configuration
43+
## Usage
4444

45-
Configuration is handled by creating a **.env** file. This should contain the following variables (replace the values with your own):
45+
Replace the values in **.env.example** with your values and rename this file to **.env**:
46+
47+
* `FLASK_APP`: Entry point of your application (should be `wsgi.py`).
48+
* `FLASK_ENV`: The environment to run your app in (either `development` or `production`).
49+
* `SECRET_KEY`: Randomly generated string of characters used to encrypt your app's data.
50+
* `LESS_BIN`: Path to your local LESS installation via `which lessc` (optional for static assets).
51+
* `ASSETS_DEBUG`: Debug asset creation and bundling in `development` (optional).
52+
* `LESS_RUN_IN_DEBUG`: Debug LESS while in `development` (optional).
53+
* `COMPRESSOR_DEBUG`: Debug asset compression while in `development` (optional).
54+
55+
56+
*Remember never to commit secrets saved in .env files to Github.*
4657

47-
```.env
48-
SECRET_KEY="YOURSECRETKEY"
49-
FLASK_ENV="production"
50-
BEST_BUY_API_KEY="YOURBESTBUYAPIKEY"
51-
```
5258
-----
5359

5460
**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

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

application/home/home.py

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

application/static/.DS_Store

-6 KB
Binary file not shown.

application/static/dist/.DS_Store

-6 KB
Binary file not shown.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ def fetch_products(app):
1313
"totalPages": 1,
1414
"sort": "customerReviewAverage.dsc"}
1515
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}
16-
r = requests.get(endpoint, params=params, headers=headers)
17-
products = r.json()['products']
16+
req = requests.get(endpoint, params=params, headers=headers)
17+
products = req.json()['products']
1818
return products

flask_blueprint_tutorial/assets.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
"""
8+
Create stylesheet bundles.
9+
10+
:param assets: Flask-Assets Environment
11+
:type assets: Environment
12+
"""
13+
assets.auto_build = True
14+
assets.debug = False
15+
common_less_bundle = Bundle(
16+
'src/less/*.less',
17+
filters='less,cssmin',
18+
output='dist/css/style.css',
19+
extra={'rel': 'stylesheet/less'}
20+
)
21+
home_less_bundle = Bundle(
22+
'home_bp/less/home.less',
23+
filters='less,cssmin',
24+
output='dist/css/home.css',
25+
extra={'rel': 'stylesheet/less'}
26+
)
27+
profile_less_bundle = Bundle(
28+
'profile_bp/less/profile.less',
29+
filters='less,cssmin',
30+
output='dist/css/profile.css',
31+
extra={'rel': 'stylesheet/less'}
32+
)
33+
product_less_bundle = Bundle(
34+
'products_bp/less/products.less',
35+
filters='less,cssmin',
36+
output='dist/css/products.css',
37+
extra={'rel': 'stylesheet/less'}
38+
)
39+
assets.register('common_less_bundle', common_less_bundle)
40+
assets.register('home_less_bundle', home_less_bundle)
41+
assets.register('profile_less_bundle', profile_less_bundle)
42+
assets.register('product_less_bundle', product_less_bundle)
43+
if app.config['FLASK_ENV'] == 'development': # Only rebuild in development
44+
common_less_bundle.build()
45+
home_less_bundle.build()
46+
profile_less_bundle.build()
47+
product_less_bundle.build()
48+
return assets
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""General page routes."""
2+
from flask import Blueprint, render_template
3+
from flask import current_app as app
4+
from flask_blueprint_tutorial.api import fetch_products
5+
6+
7+
# Blueprint Configuration
8+
home_bp = Blueprint(
9+
'home_bp', __name__,
10+
template_folder='templates',
11+
static_folder='static'
12+
)
13+
14+
15+
@home_bp.route('/', methods=['GET'])
16+
def home():
17+
"""Homepage."""
18+
products = fetch_products(app)
19+
return render_template(
20+
'index.jinja2',
21+
title='Flask Blueprint Demo',
22+
subtitle='Demonstration of Flask blueprints in action.',
23+
template='home-template',
24+
products=products
25+
)
26+
27+
28+
@home_bp.route('/about', methods=['GET'])
29+
def about():
30+
"""About page."""
31+
return render_template(
32+
'index.jinja2',
33+
title="About",
34+
subtitle='This is an example about page.',
35+
template='home-template page'
36+
)
37+
38+
39+
@home_bp.route('/contact', methods=['GET'])
40+
def contact():
41+
"""Contact page."""
42+
return render_template(
43+
'index.jinja2',
44+
title="Contact",
45+
subtitle='This is an example contact page.',
46+
template='home-template page'
47+
)

0 commit comments

Comments
 (0)