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

Make bootstrap available in extension resources #8

Merged
merged 1 commit into from
Apr 9, 2024
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
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
Loading