Skip to content

Weaving flask's render_template function #26

Open
@N00bAdrian

Description

@N00bAdrian

I am working on building software product lines for Flask applications and I am trying to use an aspect to make Flask's render_template function render from another HTML file:

from flask import render_template, Blueprint, url_for, redirect, request
from flask.views import MethodView
...

from aspectlib import Aspect, Proceed, weave

...

bp = Blueprint('home', __name__, template_folder='../templates')

class MessageForm(Form):
    ...

@Aspect
def render_author(*args, **kwargs):
	print("Rendering page with author information...")
	yield Proceed('homeWithAuthor.html', **kwargs)

class HomeView(MethodView):

    def get(self):
        with weave(render_template, render_author):
            form = MessageForm(request.form)
            messages = db.session.execute(db.select(Message)).scalars()
            return render_template(
                "home.html", 
                form=form,
                messages=messages
            )

    def post(self):
        ...
    
bp.add_url_rule("/", view_func=HomeView.as_view("home"))

The app would render from the original file instead of the stated file in the aspect though. There were no errors in weaving and running the app. Upon inspection with a debugger, the aspect has not been called at all. I have tried other arguments in the weaver including

weave('render_template', render_author)
weave('flask.render_template', render_author)
weave(flask.render_template, render_author)
weave('flask', render_author, methods='render_template')

and they all have the same result. The weaver works with builtin functions and class methods so I think it is an issue of me not weaving the aspect correctly.

Any help is appreciated, thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions