Skip to content

Commit a54f1d6

Browse files
authored
Make bootstrap available in extension resources (#7)
1 parent 2909071 commit a54f1d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+67166
-3
lines changed

example/app.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from flask import Flask
2+
3+
from bootlace.extension import Bootlace
4+
5+
6+
app = Flask(__name__)
7+
bootlace = Bootlace(app)

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ exclude_lines = [
7171
"raise NotImplementedError",
7272
"if 0:",
7373
"except BaseException:",
74-
": \\.\\.\\.",
74+
"\\.\\.\\.",
75+
"if app is not None:",
7576
]
7677
omit = ["src/bootlace/_version.py", "src/bootlace/testing/*"]
7778

src/bootlace/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
# SPDX-License-Identifier: MIT
44
from . import __about__
55
from ._version import __version__
6+
from .extension import Bootlace
67
from .util import _monkey_patch_dominate
78
from .util import as_tag
89
from .util import render
910

10-
__all__ = ["__version__", "__about__", "as_tag", "render"]
11+
__all__ = ["__version__", "__about__", "as_tag", "render", "Bootlace"]
1112

1213

1314
_monkey_patch_dominate()

src/bootlace/extension.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from flask import Blueprint
2+
from flask import current_app
3+
from flask import Flask
4+
from flask import url_for
5+
6+
7+
class Bootlace:
8+
"""Flask extension for bootlace"""
9+
10+
def __init__(self, app: Flask | None = None) -> None:
11+
if app is not None:
12+
self.init_app(app)
13+
14+
def init_app(self, app: Flask) -> None:
15+
"""Initialize the extension with the Flask app"""
16+
17+
app.extensions["bootlace"] = self
18+
app.jinja_env.globals["bootlace"] = self
19+
20+
name = app.config.setdefault("BOOTLACE_BLUEPRINT_NAME", "bootlace")
21+
22+
blueprint = Blueprint(
23+
name,
24+
__name__,
25+
template_folder="templates",
26+
static_folder="static",
27+
static_url_path="/static/bootstrap",
28+
)
29+
30+
app.register_blueprint(blueprint)
31+
32+
@property
33+
def static_view(self) -> str:
34+
bp = current_app.config["BOOTLACE_BLUEPRINT_NAME"]
35+
return f"{bp}.static"
36+
37+
@property
38+
def icons(self) -> str:
39+
"""The URL for the SVG source for the icons"""
40+
return url_for(self.static_view, filename="icons/bootstrap-icons.svg")
41+
42+
@property
43+
def css(self) -> str:
44+
"""The URL for the Bootstrap CSS file"""
45+
return url_for(self.static_view, filename="css/bootstrap.min.css")
46+
47+
@property
48+
def js(self) -> str:
49+
"""The URL for the Bootstrap JS file"""
50+
return url_for(self.static_view, filename="js/bootstrap.min.js")

src/bootlace/icon.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Icon:
1717
"""
1818

1919
#: Endpoint name for getting the Bootstrap Icon SVG file
20-
endpoint: ClassVar[str] = "static"
20+
endpoint: ClassVar[str] = "bootlace.static"
2121

2222
#: Filename for the Bootstrap Icon SVG file
2323
filename: ClassVar[str] = "icons/bootstrap-icons.svg"

0 commit comments

Comments
 (0)