3

I'm trying out the Python runtime. I understand that it's using Flask.

I'm using templates in my function but
1. I don't see the templates folder in the uploaded source. 2. render_template throws a TemplateNotFound error

Actually it doesn't seem to be uploading any folders. Is this a bug or can I do anything about this?

3
  • I assume you were developing the function in local, by your description. The thing is that you can't upload templates to use in Functions. This kind of code is more suited for App Engine Standard. Commented Aug 15, 2018 at 8:54
  • Actually, turns out that it was uploading the templates folder. It was just not showing in the source tab and I got it working defining the jinga env and rending it inside the function. WITHOUT using flask Commented Aug 16, 2018 at 14:04
  • 1
    Sounds interesting! Would you mind posting what you did, code and so on, as an answer! Python on GCF is extremely new, so, your contribution to the community on this topic sounds interesting! Commented Aug 16, 2018 at 15:26

2 Answers 2

3

Turns out the templates folder is getting uploaded but it doesn't show in the source tab.

And I got the templates rendering working by using jinja2 directly instead of using Flask's render_template

Solution:

from jinja2 import Environment, FileSystemLoader, select_autoescape

context_data = {
    'var1': 'val1',
    'var2': 'val2',
}

env = Environment(
    loader=FileSystemLoader('./templates'),
    autoescape=select_autoescape(['html', 'xml'])
)

template = env.get_template('template.html')
html = template.render(**context_data)
Sign up to request clarification or add additional context in comments.

Comments

2

This article details how to do it with Flask:

https://dev.to/googlecloud/html-templates-with-google-cloud-functions-29bc

Structure things as such -- and it magically works.

├── main.py
├── requirements.txt
└── templates
    └── hello.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.