-
Hello, I am having a hard time wrapping my head around on how to best extend an existing extension. So I want to use this extension (https://github.com/helloflask/bootstrap-flask) as the basis for our theming, for our different flask apps, so they have all have a common look, with same basic built in routes / templates. So, for templates, it seems to work, but everything falls apart when I try to add url routes. In my extension (ksul-bootstrap-flask) I have
import flask
from flask_bootstrap import Bootstrap4
from flask import Blueprint, current_app, url_for
class KSULBootstrap4(Bootstrap4):
static_folder = 'bootstrap4'
def __init__(self, app=None):
super().__init__(app)
def init_app(self, app):
if not hasattr(app, 'extensions'):
app.extensions = {}
app.extensions['ksul-bootstrap'] = self
blueprint = Blueprint('ksul-bootstrap', __name__, static_folder=f'static/{self.static_folder}',
static_url_path=f'/ksul{app.static_url_path}',
template_folder='templates')
# blueprint.add_url_rule('/favicon.ico',
# redirect_to=url_for('static', filename='favicon.ico'))
super().init_app(app=app)
app.register_blueprint(blueprint) In my application I have the following from flask import Flask
from ksul_bootstrap_flask import KSULBootstrap4
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
import pymysql
pymysql.install_as_MySQLdb()
app = Flask(__name__)
bootstrap = KSULBootstrap4(app)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
from app import routes, models When I uncomment the add_url_rule lines above, I get the following error:
I've tried adding I'm using Flask 2.2.3 Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
You can't call with app.app_context():
setup stuff that needs a context |
Beta Was this translation helpful? Give feedback.
-
@app.route('/favicon.ico')
def favicon():
print(app.static_path)
return send_from_directory(os.path.join('ksul-bootstrap.static'),
'img/favicon.ico', mimetype='image/vnd.microsoft.icon') This is getting be closer, but I can't seem to find out the path that will give me the static_path for the extension itself (installed via pip in a venv). If I do |
Beta Was this translation helpful? Give feedback.
-
I got it working the following @app.route('/favicon.ico')
def favicon():
return redirect(url_for('ksul-bootstrap.static', filename='img/favicon.ico')) import flask
from flask_bootstrap import Bootstrap4
from flask import Blueprint, current_app, url_for, redirect
class KSULBootstrap4(Bootstrap4):
# bootstrap_version = '4.6.2'
# jquery_version = '3.5.1'
# popper_version = '1.16.1'
# bootstrap_css_integrity = 'sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N'
# bootstrap_js_integrity = 'sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+'
# jquery_integrity = 'sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj'
# popper_integrity = 'sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN'
# popper_name = 'popper.js'
static_folder = 'bootstrap4'
def __init__(self, app=None):
super().__init__(app)
def init_app(self, app):
if not hasattr(app, 'extensions'):
app.extensions = {}
app.extensions['ksul-bootstrap'] = self
@app.route('/favicon.ico')
def favicon():
return redirect(url_for('ksul-bootstrap.static', filename='img/favicon.ico'))
blueprint = Blueprint('ksul-bootstrap', __name__, static_folder=f'static/{self.static_folder}',
static_url_path=f'/ksul{app.static_url_path}',
template_folder='templates')
super().init_app(app=app)
app.register_blueprint(blueprint) |
Beta Was this translation helpful? Give feedback.
I got it working the following