如何将jinja2输出呈现给Python中的文件而不是浏览器

时间:2021-01-09 20:45:43

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.

我有一个jinja2模板(。我想要呈现的(用我的py文件中的值替换令牌)。但是,与其将呈现的结果发送到浏览器,不如将其写入一个新的.html文件中。我可以想象,对于django模板,解决方案也是类似的。

How can I do this?

我该怎么做呢?

4 个解决方案

#1


89  

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", "wb") as fh:
    fh.write(output_from_parsed_template)

test.html

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.

如果您正在使用一个框架,例如Flask,那么您可以在返回之前,在视图的底部进行此操作。

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

#2


8  

You can dump a template stream to file as follows:

您可以将模板流转储到以下文件:

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

Ref: http://jinja.pocoo.org/docs/dev/api/#jinja2.environment.TemplateStream.dump

裁判:http://jinja.pocoo.org/docs/dev/api/ # jinja2.environment.TemplateStream.dump

#3


3  

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'.

在加载模板之后,调用render,然后将输出写到文件中。“with”语句是一个上下文管理器。在缩进中有一个像'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)

#4


1  

The answers above from sberry and aychedee work, i only had to add f.close(). Otherwise you might not find your file in your directory.

上面来自sberry和aychedee的答案,我只需要添加f.close()。否则,您可能无法在您的目录中找到您的文件。

#1


89  

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", "wb") as fh:
    fh.write(output_from_parsed_template)

test.html

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.

如果您正在使用一个框架,例如Flask,那么您可以在返回之前,在视图的底部进行此操作。

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

#2


8  

You can dump a template stream to file as follows:

您可以将模板流转储到以下文件:

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

Ref: http://jinja.pocoo.org/docs/dev/api/#jinja2.environment.TemplateStream.dump

裁判:http://jinja.pocoo.org/docs/dev/api/ # jinja2.environment.TemplateStream.dump

#3


3  

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'.

在加载模板之后,调用render,然后将输出写到文件中。“with”语句是一个上下文管理器。在缩进中有一个像'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)

#4


1  

The answers above from sberry and aychedee work, i only had to add f.close(). Otherwise you might not find your file in your directory.

上面来自sberry和aychedee的答案,我只需要添加f.close()。否则,您可能无法在您的目录中找到您的文件。