Skip to content

Commit

Permalink
Merge pull request #88 from TitaniumHocker/master
Browse files Browse the repository at this point in the history
Added bootswatch themes support
  • Loading branch information
greyli authored Aug 10, 2020
2 parents daaf1c5 + 0b553bc commit e181156
Show file tree
Hide file tree
Showing 25 changed files with 314 additions and 13 deletions.
20 changes: 11 additions & 9 deletions docs/basic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,16 @@ Go to the :doc:`macros` page to see the detailed usage for these macros.
Configurations
---------------

+---------------------------+--------------------------------+-----------------------------------------------------------------------------+
| Configuration Variable | Default Value | Description |
+===========================+================================+=============================================================================+
| BOOTSTRAP_SERVE_LOCAL | ``False`` | If set to ``True``, local resources will be used for ``load_*`` methods. |
+---------------------------+--------------------------------+-----------------------------------------------------------------------------+
| BOOTSTRAP_BTN_STYLE | ``'secondary'`` | Default form button style, will change to ``primary`` in next major release |
+---------------------------+--------------------------------+-----------------------------------------------------------------------------+
| BOOTSTRAP_BTN_SIZE | ``'md'`` | Default form button size |
+---------------------------+--------------------------------+-----------------------------------------------------------------------------+
+---------------------------+--------------------------------+----------------------------------------------------------------------------------------------+
| Configuration Variable | Default Value | Description |
+===========================+================================+==============================================================================================+
| BOOTSTRAP_SERVE_LOCAL | ``False`` | If set to ``True``, local resources will be used for ``load_*`` methods. |
+---------------------------+--------------------------------+----------------------------------------------------------------------------------------------+
| BOOTSTRAP_BTN_STYLE | ``'secondary'`` | Default form button style, will change to ``primary`` in next major release |
+---------------------------+--------------------------------+----------------------------------------------------------------------------------------------+
| BOOTSTRAP_BTN_SIZE | ``'md'`` | Default form button size |
+---------------------------+--------------------------------+----------------------------------------------------------------------------------------------+
| BOOTSTRAP_BOOTSWATCH_THEME| ``None`` | Bootswatch theme to use, see available themes at `bootswatch.com <https://bootswatch.com>`_ |
+---------------------------+--------------------------------+----------------------------------------------------------------------------------------------+

.. tip:: See :ref:`button_customizatoin` to learn how to customize form buttons.
21 changes: 17 additions & 4 deletions flask_bootstrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def init_app(self, app):
app.config.setdefault('BOOTSTRAP_SERVE_LOCAL', False)
app.config.setdefault('BOOTSTRAP_BTN_STYLE', 'secondary')
app.config.setdefault('BOOTSTRAP_BTN_SIZE', 'md')
app.config.setdefault('BOOTSTRAP_BOOTSWATCH_THEME', None)

@staticmethod
def load_css(version=VERSION_BOOTSTRAP):
Expand All @@ -74,13 +75,25 @@ def load_css(version=VERSION_BOOTSTRAP):
"""
css_filename = 'bootstrap.min.css'
serve_local = current_app.config['BOOTSTRAP_SERVE_LOCAL']
bootswatch_theme = current_app.config['BOOTSTRAP_BOOTSWATCH_THEME']

if serve_local:
css = '<link rel="stylesheet" href="%s" type="text/css">' % \
url_for('bootstrap.static', filename='css/' + css_filename)
if not bootswatch_theme:
css = '<link rel="stylesheet" href="%s" type="text/css">' % \
url_for('bootstrap.static', filename='css/' + css_filename)
else:
css = '<link rel="stylesheet" href="%s" type="text/css"' % \
url_for(
'bootstrap.static',
filename='css/swatch/%s/%s' % (bootswatch_theme.lower(), css_filename)
)
else:
css = '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@%s/dist/css/%s"' \
' type="text/css">' % (version, css_filename)
if not bootswatch_theme:
css = '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@%s/dist/css/%s"' \
' type="text/css">' % (version, css_filename)
else:
css = '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootswatch@%s/dist/%s/%s"' \
' type="text/css"' % (version, bootswatch_theme.lower(), css_filename)
return Markup(css)

@staticmethod
Expand Down
12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/cerulean/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/cosmo/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/cyborg/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/darkly/bootstrap.min.css

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions flask_bootstrap/static/css/swatch/default/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/flatly/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/journal/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/litera/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/lumen/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/lux/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/materia/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/minty/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/pulse/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/sandstone/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/simplex/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/sketchy/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/slate/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/solar/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/spacelab/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/superhero/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/united/bootstrap.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions flask_bootstrap/static/css/swatch/yeti/bootstrap.min.css

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions test_bootstrap_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def setUp(self):
self.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.app.secret_key = 'for test'
self.bootstrap = Bootstrap(self.app) # noqa
self.bootswatch_themes = [
'cerulean', 'cosmo', 'cyborg', 'darkly', 'default', 'flatly', 'journal', 'litera',
'lumen', 'lux', 'materia', 'minty', 'pulse', 'sandstone', 'simplex', 'sketchy', 'slate',
'solar', 'spacelab', 'superhero', 'united', 'yeti'
]

@self.app.route('/')
def index():
Expand Down Expand Up @@ -86,6 +91,28 @@ def test_cdn_resources(self):
self.assertIn('https://cdn.jsdelivr.net/npm/bootstrap', css_rv)
self.assertIn('https://cdn.jsdelivr.net/npm/bootstrap', js_rv)

def test_bootswatch_local(self):
current_app.config['BOOTSTRAP_SERVE_LOCAL'] = True

for theme in self.bootswatch_themes:
current_app.config['BOOTSTRAP_BOOTSWATCH_THEME'] = theme
data = self.client.get('/').get_data(as_text=True)
self.assertNotIn('https://cdn.jsdelivr.net/npm/bootswatch', data)
self.assertIn('swatch/%s/bootstrap.min.css' % theme, data)
with self.client.get('/bootstrap/static/css/swatch/%s/bootstrap.min.css' % theme) as css_response:
self.assertNotEqual(css_response.status_code, 404)

def test_bootswatch_cdn(self):
current_app.config['BOOTSTRAP_SERVE_LOCAL'] = False

for theme in self.bootswatch_themes:
current_app.config['BOOTSTRAP_BOOTSWATCH_THEME'] = theme
data = self.client.get('/').get_data(as_text=True)
self.assertIn('https://cdn.jsdelivr.net/npm/bootswatch', data)
css_rv = self.bootstrap.load_css()
self.assertNotIn('/bootstrap/static/css/swatch/%s/bootstrap.min.css' % theme, data)
self.assertIn('https://cdn.jsdelivr.net/npm/bootswatch', css_rv)

def test_render_field(self):
@self.app.route('/field')
def test():
Expand Down

0 comments on commit e181156

Please sign in to comment.