Issue
Is there a way to assign the "template_name_or_list" parameter in render_template via a variable when freezing a Flask app via Frozen Flask? I can't work out how it might be acheived.
The below is an abbreviation of the working code that specifies the 'index.html' template by name:
from flask import Flask, render_template
from flask_frozen import Freezer
app = Flask(__name__)
path = 'C:\\Users\\Foo\\Bar\\'
@app.route('/')
def build_page():
div_list = path + 'index' + '.pickle'
return render_template('index.html', div_list=div_list)
def save_page():
freezer = Freezer(app)
freezer.freeze()
if __name__ == '__main__':
save_page()
This creates the index.html file in the "build" folder as expected.
What I'd like to do is pass a template name on creation, for example:
save_page('index')
and have this feed through as a variable in build_page to pick up the pickle file to import and name the page in render_template.
In this case there would have to be a template of whatever name was passed of course, but if there might also be the option of keeping the index.html as the template, but saving it in Flask Frozen's "build" folder as the passed name that would be even better.
I checked out the Freezer class parameters and https://flask.palletsprojects.com/en/1.0.x/api/#flask.render_template but being fairly new to flask and frozen flask, I didnt quite grasp whether this might be possible.
Thanks vm.
Solution
In the end I couldn't find a way to do this in the way I had planned.
Essentially the question was about efficiently building different html pages with the same template (with content added via jinja2).
I looked through standard static site generators that use jinja2 (such as Pelican) but they all seemed to be focued on blogs and adding loads of optionality I didnt need.
I settled on building my own using jinja template inheritance explained at https://zetcode.com/python/jinja/
from jinja2 import Environment, FileSystemLoader
content = 'This is about page'
file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)
template = env.get_template('about.html')
output = template.render(content=content)
print(output)
Answered By - kowpow
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.