Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added bootswatch themes support #88

Merged
merged 4 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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