Skip to content

Commit

Permalink
Make bootstrap available in extension resources (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrudy authored Apr 6, 2024
1 parent 2909071 commit a54f1d6
Show file tree
Hide file tree
Showing 55 changed files with 67,166 additions and 3 deletions.
7 changes: 7 additions & 0 deletions example/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from flask import Flask

from bootlace.extension import Bootlace


app = Flask(__name__)
bootlace = Bootlace(app)
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ exclude_lines = [
"raise NotImplementedError",
"if 0:",
"except BaseException:",
": \\.\\.\\.",
"\\.\\.\\.",
"if app is not None:",
]
omit = ["src/bootlace/_version.py", "src/bootlace/testing/*"]

Expand Down
3 changes: 2 additions & 1 deletion src/bootlace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# SPDX-License-Identifier: MIT
from . import __about__
from ._version import __version__
from .extension import Bootlace
from .util import _monkey_patch_dominate
from .util import as_tag
from .util import render

__all__ = ["__version__", "__about__", "as_tag", "render"]
__all__ = ["__version__", "__about__", "as_tag", "render", "Bootlace"]


_monkey_patch_dominate()
50 changes: 50 additions & 0 deletions src/bootlace/extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from flask import Blueprint
from flask import current_app
from flask import Flask
from flask import url_for


class Bootlace:
"""Flask extension for bootlace"""

def __init__(self, app: Flask | None = None) -> None:
if app is not None:
self.init_app(app)

def init_app(self, app: Flask) -> None:
"""Initialize the extension with the Flask app"""

app.extensions["bootlace"] = self
app.jinja_env.globals["bootlace"] = self

name = app.config.setdefault("BOOTLACE_BLUEPRINT_NAME", "bootlace")

blueprint = Blueprint(
name,
__name__,
template_folder="templates",
static_folder="static",
static_url_path="/static/bootstrap",
)

app.register_blueprint(blueprint)

@property
def static_view(self) -> str:
bp = current_app.config["BOOTLACE_BLUEPRINT_NAME"]
return f"{bp}.static"

@property
def icons(self) -> str:
"""The URL for the SVG source for the icons"""
return url_for(self.static_view, filename="icons/bootstrap-icons.svg")

@property
def css(self) -> str:
"""The URL for the Bootstrap CSS file"""
return url_for(self.static_view, filename="css/bootstrap.min.css")

@property
def js(self) -> str:
"""The URL for the Bootstrap JS file"""
return url_for(self.static_view, filename="js/bootstrap.min.js")
2 changes: 1 addition & 1 deletion src/bootlace/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Icon:
"""

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

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

0 comments on commit a54f1d6

Please sign in to comment.