0

I would like to have a periodic task which renders an html file and uploads it to s3 via boto.

The problem with this is that since the task is outside of an endpoint function (i.e. decorated by app.route), there is no Flask context. So, when my task executes and render_template is called, there is an exception due to there being no context:

Traceback ........ File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 126, in render_template ctx.app.update_template_context(context) AttributeError: 'NoneType' object has no attribute 'app' 

My task is initialized something like this, where I pass in the function I want to execute periodically:

HtmlUploader.new( lambda: render_template('something.html', value=get_value()) ).start() 

Is there any way I can call render_template outside of an app endpoint function?

1
  • I've come up with a solution that works, but may not be ideal. ctx = app.test_request_context(''), and then I can use ctx in a with statement to give me the context I need to call render_template. Commented Jul 4, 2014 at 18:21

1 Answer 1

3

Rendering a template with render_template() requires a request context.

You can easily create one for just the batch process:

def render_with_context(template, _url='/', **kw): with app.test_request_context(url): return render_template(template, **kw) 

This produces a 'test' request for a given URL (defaults to /). You can then use this as:

HtmlUploader.new( lambda: render_with_context('something.html', value=get_value()) ).start() 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this is what I ended up discovering. I wasn't sure if this was the best solution, though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.