2

This is probably a very simple question but I did not find any solution until now and I have been around this hours

I have one HTML file "teste.html" I Use jinja 2 to change my HTML

env = Environment(loader=FileSystemLoader('.'))
template = env.get_template("teste.html")

template_vars = {"Client" : Name}
    
html_out = template.render(template_vars)

type(html_out)

finish with a html_out that is s string

type(html_out)
Out[60]: str

Now I want to save this html_out in a HTML document

How can I do that?

1
  • What's the problem? Writing a string to a file is the same no matter where it came from. Commented Aug 27, 2020 at 16:02

2 Answers 2

12

You can just write the HTML string you have to a file with .html extension.

For example:

htmlstring = ... # Your HTML data
with open("yourhtmlfile.html", "w") as file:
    file.write(htmlstring)
Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks, Challe!! You first "open" as a document and after save them?
The with statement takes care of closing the file
2

It sounds like you want to save your str to a file. Saving a file in python can be done like:

with open('teste_rendered.html', 'w') as file:
    file.write(html_out)

Note: The location where the file actually saves depends on how you run your python script.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.