107

I have a jinja2 template (.html file) that I want to render (replace the tokens with values from my py file). Instead of sending the rendered result to a browser, however, I want to write it to a new .html file. I would imagine the solution would also be similar for a django template.

How can I do this?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Bill G.
  • 1,417
  • 2
  • 12
  • 18

3 Answers3

162

How about something like this?

from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template('test.html')
output_from_parsed_template = template.render(foo='Hello World!')
print(output_from_parsed_template)

# to save the results
with open("my_new_file.html", "w") as fh:
    fh.write(output_from_parsed_template)

test.html

<h1>{{ foo }}</h1>

output

<h1>Hello World!</h1>

If you are using a framework, such as Flask, then you could do this at the bottom of your view, before you return.

output_from_parsed_template = render_template('test.html', foo="Hello World!")
with open("some_new_file.html", "wb") as f:
    f.write(output_from_parsed_template)
return output_from_parsed_template
cjohnson318
  • 3,154
  • 30
  • 33
sberry
  • 128,281
  • 18
  • 138
  • 165
  • Thank you for the speedy response. If I am understanding correctly, then to your first snippit: from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('templates')) template = env.get_template('test.html') output_from_parsed_template = template.render(foo='Hello World!') print output_from_parsed_template I could replace the print line with some kind of a file write line. Is that correct? What might such a line to write to a file look like? Re. Flask, this is a small part of a larger app, so I don't know if I would be able to use a framework. – Bill G. Aug 08 '12 at 04:38
  • Thanks for the clarification. I finally had a chance to try this out. Initially I got an error "No such file or directory: 'my_new_file.html'". Apparently the file already has to exist. I then copied the template file and renamed it to 'my_new_file.html'. Now I get an error: IOError: File not open for writing. Could this be because I am developing in Google App Engine? – Bill G. Aug 21 '12 at 13:54
  • @BillG. No, it was an error on my part. Try the change above: changed `rb` to `wb`. – sberry Aug 21 '12 at 15:41
  • Thanks for the quick response. I changed the rb to wb and now I get the following error: IOError: invalid mode: wb – Bill G. Aug 21 '12 at 16:05
  • There's a missing `)` on the end of the first line in the bottom code section. I was trying to add it, but SO requires edits to be >6 characters (stupid limitation).. – egeland Apr 05 '15 at 02:50
  • @egeland Ah, indeed there is. Fixed. Thanks for pointing it out. It's only been like that for 2.5 years! :-) – sberry Apr 05 '15 at 04:02
65

You can dump a template stream to file as follows:

Template('Hello {{ name }}!').stream(name='foo').dump('hello.html')

Ref: https://jinja.palletsprojects.com/en/2.9.x/api/#jinja2.environment.TemplateStream.dump

shuuji3
  • 1,233
  • 13
  • 20
Lim H.
  • 9,870
  • 9
  • 48
  • 74
11

So after you've loaded the template, you call render and then write the output to a file. The 'with' statement is a context manager. Inside the indentation you have an open file like object called 'f'.

template = jinja_environment.get_template('CommentCreate.html')     
output = template.render(template_values)) 

with open('my_new_html_file.html', 'w') as f:
    f.write(output)
aychedee
  • 24,871
  • 8
  • 79
  • 83
  • So, would this look something like: TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'templates') jinja_environment = \ jinja2.Environment(autoescape=False, loader=jinja2.FileSystemLoader(TEMPLATE_DIR)) template = jinja_environment.get_template('CommentCreate.html') self.response.out.write(template.render(template_values)) with open('my_new_html_file.html', 'w') as f: f.write(response.content) where template_values where already populated. Please correct as needed. Thanks. – Bill G. Aug 08 '12 at 04:41
  • Thanks for the clarification. I finally had a chance to try this out. Initially I got an error "No such file or directory: 'my_new_file.html'". Apparently the file already has to exist. I then copied the template file and renamed it to 'my_new_file.html'. Now I get an error: IOError: File not open for writing. Could this be because I am developing in Google App Engine? – Bill G. Aug 21 '12 at 13:55