Skip to content

Commit 8b8331e

Browse files
committed
Example table fix and switching Bootswatch
1 parent 00293e4 commit 8b8331e

File tree

10 files changed

+142
-7
lines changed

10 files changed

+142
-7
lines changed

examples/bootstrap4/app.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# set default button sytle and size, will be overwritten by macro parameters
1616
app.config['BOOTSTRAP_BTN_STYLE'] = 'primary'
1717
app.config['BOOTSTRAP_BTN_SIZE'] = 'sm'
18-
# app.config['BOOTSTRAP_BOOTSWATCH_THEME'] = 'lumen' # uncomment this line to test bootswatch theme
1918

2019
# set default icon title of table actions
2120
app.config['BOOTSTRAP_TABLE_VIEW_TITLE'] = 'Read'
@@ -89,6 +88,36 @@ class ContactForm(FlaskForm):
8988
im_accounts = FieldList(FormField(IMForm), min_entries=2)
9089

9190

91+
class BootswatchForm(FlaskForm):
92+
"""Form to test Bootswatch."""
93+
#DO NOT EDIT! Use list-bootswatch.py te generate the Radiofield below.
94+
render = RadioField(default='default',
95+
choices=[('default', 'none'),
96+
('cerulean', 'Cerulean 4.6.1'),
97+
('cosmo', 'Cosmo 4.6.1'),
98+
('cyborg', 'Cyborg 4.6.1'),
99+
('darkly', 'Darkly 4.6.1'),
100+
('flatly', 'Flatly 4.6.1'),
101+
('journal', 'Journal 4.6.1'),
102+
('litera', 'Litera 4.6.1'),
103+
('lumen', 'Lumen 4.6.1'),
104+
('lux', 'Lux 4.6.1'),
105+
('materia', 'Materia 4.6.1'),
106+
('minty', 'Minty 4.6.1'),
107+
('pulse', 'Pulse 4.6.1'),
108+
('sandstone', 'Sandstone 4.6.1'),
109+
('simplex', 'Simplex 4.6.1'),
110+
('sketchy', 'Sketchy 4.6.1'),
111+
('slate', 'Slate 4.6.1'),
112+
('solar', 'Solar 4.6.1'),
113+
('spacelab', 'Spacelab 4.6.1'),
114+
('superhero', 'Superhero 4.6.1'),
115+
('united', 'United 4.6.1'),
116+
('yeti', 'Yeti 4.6.1'),
117+
])
118+
submit = SubmitField()
119+
120+
92121
class Message(db.Model):
93122
id = db.Column(db.Integer, primary_key=True)
94123
text = db.Column(db.Text, nullable=False)
@@ -155,10 +184,26 @@ def test_nav():
155184
return render_template('nav.html')
156185

157186

187+
@app.route('/bootswatch', methods=['GET', 'POST'])
188+
def test_bootswatch():
189+
form = BootswatchForm()
190+
if form.validate_on_submit():
191+
print('FFFFF', app.config['BOOTSTRAP_BOOTSWATCH_THEME'])
192+
if form.render.data == 'default':
193+
app.config['BOOTSTRAP_BOOTSWATCH_THEME'] = None
194+
else:
195+
app.config['BOOTSTRAP_BOOTSWATCH_THEME'] = form.render.data
196+
flash(f'Render style has been set to {form.render.data}.')
197+
else:
198+
if app.config['BOOTSTRAP_BOOTSWATCH_THEME'] != None:
199+
form.render.data = app.config['BOOTSTRAP_BOOTSWATCH_THEME']
200+
return render_template('bootswatch.html', form=form)
201+
202+
158203
@app.route('/pagination', methods=['GET', 'POST'])
159204
def test_pagination():
160205
page = request.args.get('page', 1, type=int)
161-
pagination = Message.query.paginate(page, per_page=10)
206+
pagination = Message.query.paginate(page=page, per_page=10)
162207
messages = pagination.items
163208
return render_template('pagination.html', pagination=pagination, messages=messages)
164209

@@ -181,7 +226,7 @@ def test_flash():
181226
@app.route('/table')
182227
def test_table():
183228
page = request.args.get('page', 1, type=int)
184-
pagination = Message.query.paginate(page, per_page=10)
229+
pagination = Message.query.paginate(page=page, per_page=10)
185230
messages = pagination.items
186231
titles = [('id', '#'), ('text', 'Message'), ('author', 'Author'), ('category', 'Category'), ('draft', 'Draft'), ('create_time', 'Create Time')]
187232
data = []

examples/bootstrap4/templates/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
{{ render_nav_item('index', 'Home') }}
3333
{{ render_nav_item('test_form', 'Form') }}
3434
{{ render_nav_item('test_nav', 'Nav') }}
35+
{{ render_nav_item('test_bootswatch', 'Bootswatch') }}
3536
{{ render_nav_item('test_pagination', 'Pagination') }}
3637
{{ render_nav_item('test_flash', 'Flash Messages') }}
3738
{{ render_nav_item('test_table', 'Table') }}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% extends 'base.html' %}
2+
{% from 'bootstrap4/form.html' import render_form %}
3+
4+
{% block content %}
5+
6+
<h2>Bootswatch</h2>
7+
{{ render_form(form) }}
8+
{% endblock %}

examples/bootstrap4/templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ <h1>Bootstrap-Flask Demo Application</h1>
66
<ul>
77
<li><a href="{{ url_for('test_form') }}">Form</a></li>
88
<li><a href="{{ url_for('test_nav') }}">Nav</a></li>
9+
<li><a href="{{ url_for('test_bootswatch') }}">Bootswatch</a></li>
910
<li><a href="{{ url_for('test_pagination') }}">Pagination</a></li>
1011
<li><a href="{{ url_for('test_flash') }}">Flash Messages</a></li>
1112
<li><a href="{{ url_for('test_table') }}">Table</a></li>

examples/bootstrap5/app.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# set default button sytle and size, will be overwritten by macro parameters
1616
app.config['BOOTSTRAP_BTN_STYLE'] = 'primary'
1717
app.config['BOOTSTRAP_BTN_SIZE'] = 'sm'
18-
# app.config['BOOTSTRAP_BOOTSWATCH_THEME'] = 'lumen' # uncomment this line to test bootswatch theme
1918

2019
# set default icon title of table actions
2120
app.config['BOOTSTRAP_TABLE_VIEW_TITLE'] = 'Read'
@@ -89,6 +88,40 @@ class ContactForm(FlaskForm):
8988
im_accounts = FieldList(FormField(IMForm), min_entries=2)
9089

9190

91+
class BootswatchForm(FlaskForm):
92+
"""Form to test Bootswatch."""
93+
#DO NOT EDIT! Use list-bootswatch.py te generate the Radiofield below.
94+
render = RadioField(default='default',
95+
choices=[('default', 'none'),
96+
('cerulean', 'Cerulean 5.1.3'),
97+
('cosmo', 'Cosmo 5.1.3'),
98+
('cyborg', 'Cyborg 5.1.3'),
99+
('darkly', 'Darkly 5.1.3'),
100+
('flatly', 'Flatly 5.1.3'),
101+
('journal', 'Journal 5.1.3'),
102+
('litera', 'Litera 5.1.3'),
103+
('lumen', 'Lumen 5.1.3'),
104+
('lux', 'Lux 5.1.3'),
105+
('materia', 'Materia 5.1.3'),
106+
('minty', 'Minty 5.1.3'),
107+
('morph', 'Morph 5.1.3'),
108+
('pulse', 'Pulse 5.1.3'),
109+
('quartz', 'Quartz 5.1.3'),
110+
('sandstone', 'Sandstone 5.1.3'),
111+
('simplex', 'Simplex 5.1.3'),
112+
('sketchy', 'Sketchy 5.1.3'),
113+
('slate', 'Slate 5.1.3'),
114+
('solar', 'Solar 5.1.3'),
115+
('spacelab', 'Spacelab 5.1.3'),
116+
('superhero', 'Superhero 5.1.3'),
117+
('united', 'United 5.1.3'),
118+
('vapor', 'Vapor 5.1.3'),
119+
('yeti', 'Yeti 5.1.3'),
120+
('zephyr', 'Zephyr 5.1.3'),
121+
])
122+
submit = SubmitField()
123+
124+
92125
class Message(db.Model):
93126
id = db.Column(db.Integer, primary_key=True)
94127
text = db.Column(db.Text, nullable=False)
@@ -155,10 +188,26 @@ def test_nav():
155188
return render_template('nav.html')
156189

157190

191+
@app.route('/bootswatch', methods=['GET', 'POST'])
192+
def test_bootswatch():
193+
form = BootswatchForm()
194+
if form.validate_on_submit():
195+
print('FFFFF', app.config['BOOTSTRAP_BOOTSWATCH_THEME'])
196+
if form.render.data == 'default':
197+
app.config['BOOTSTRAP_BOOTSWATCH_THEME'] = None
198+
else:
199+
app.config['BOOTSTRAP_BOOTSWATCH_THEME'] = form.render.data
200+
flash(f'Render style has been set to {form.render.data}.')
201+
else:
202+
if app.config['BOOTSTRAP_BOOTSWATCH_THEME'] != None:
203+
form.render.data = app.config['BOOTSTRAP_BOOTSWATCH_THEME']
204+
return render_template('bootswatch.html', form=form)
205+
206+
158207
@app.route('/pagination', methods=['GET', 'POST'])
159208
def test_pagination():
160209
page = request.args.get('page', 1, type=int)
161-
pagination = Message.query.paginate(page, per_page=10)
210+
pagination = Message.query.paginate(page=page, per_page=10)
162211
messages = pagination.items
163212
return render_template('pagination.html', pagination=pagination, messages=messages)
164213

@@ -181,7 +230,7 @@ def test_flash():
181230
@app.route('/table')
182231
def test_table():
183232
page = request.args.get('page', 1, type=int)
184-
pagination = Message.query.paginate(page, per_page=10)
233+
pagination = Message.query.paginate(page=page, per_page=10)
185234
messages = pagination.items
186235
titles = [('id', '#'), ('text', 'Message'), ('author', 'Author'), ('category', 'Category'), ('draft', 'Draft'), ('create_time', 'Create Time')]
187236
data = []

examples/bootstrap5/templates/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
{{ render_nav_item('index', 'Home') }}
3434
{{ render_nav_item('test_form', 'Form') }}
3535
{{ render_nav_item('test_nav', 'Nav') }}
36+
{{ render_nav_item('test_bootswatch', 'Bootswatch') }}
3637
{{ render_nav_item('test_pagination', 'Pagination') }}
3738
{{ render_nav_item('test_flash', 'Flash Messages') }}
3839
{{ render_nav_item('test_table', 'Table') }}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% extends 'base.html' %}
2+
{% from 'bootstrap5/form.html' import render_form %}
3+
4+
{% block content %}
5+
6+
<h2>Bootswatch</h2>
7+
{{ render_form(form) }}
8+
{% endblock %}

examples/bootstrap5/templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ <h1>Bootstrap-Flask Demo Application</h1>
66
<ul>
77
<li><a href="{{ url_for('test_form') }}">Form</a></li>
88
<li><a href="{{ url_for('test_nav') }}">Nav</a></li>
9+
<li><a href="{{ url_for('test_bootswatch') }}">Bootswatch</a></li>
910
<li><a href="{{ url_for('test_pagination') }}">Pagination</a></li>
1011
<li><a href="{{ url_for('test_flash') }}">Flash Messages</a></li>
1112
<li><a href="{{ url_for('test_table') }}">Table</a></li>

examples/list-bootswatch.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''List Bootstrap themes RadioField.'''
2+
3+
from os import listdir
4+
5+
def list(version):
6+
'''List template file names.'''
7+
print(f"To add to bootstrap{version}/app.py")
8+
print(" render = RadioField(default='default',")
9+
print(" choices=[('default', 'none'),")
10+
base = f'../flask_bootstrap/static/bootstrap{version}/css/bootswatch'
11+
name = ''
12+
for directory in sorted(listdir(base)):
13+
with open(f'{base}/{directory}/_bootswatch.scss') as scss:
14+
for line in scss:
15+
name = line.strip()[3:]
16+
break
17+
print(f" ('{directory}', '{name}'),")
18+
print(" ])")
19+
20+
for value in (4, 5):
21+
list(value)

examples/update-icons.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def parse(filename):
1313

1414
def generate(version):
1515
'''Write the HTML template file.'''
16-
head = f'''<!-- DO NOT EDIT! Use generate.py for updating this file. -->
16+
head = f'''<!-- DO NOT EDIT! Use update-icons.py for updating this file. -->
1717
{{% extends 'base.html' %}}
1818
{{% from 'bootstrap{version}/utils.html' import render_icon %}}
1919

0 commit comments

Comments
 (0)