solución a Jj 'TemplateNotFound'

This commit is contained in:
fenix 2024-02-12 16:27:19 +01:00
parent 7bda9ca408
commit ee87a0715a
4 changed files with 34 additions and 36 deletions

View File

@ -1,27 +0,0 @@
# good_messages.py
from jinja2 import Environment, FileSystemLoader
MAX_SCORE = 100
TEST_NAME = "Python Challenge"
students = [
{"name" : "Willow", "score":100},
{"name" : "Cordelia", "score":85},
{"name" : "Oz", "score":95},
]
env = Environment(loader=FileSystemLoader("templates/"))
template = env.get_template("good.txt")
for student in students:
name = student["name"]
filename = f"output/message_{name.lower()}.txt"
content = template.render(
student,
max_score=MAX_SCORE,
test_name=TEST_NAME
)
with open(filename, mode="w", encoding="utf-8") as output:
output.write(content)
print("... wrote", filename)

View File

@ -1,9 +0,0 @@
{# templates/good.txt #}
Hello {{ name }},
I'm happy to inform you that you did very well on today's {{ test_name }}.
You achieved {{ score }} out of { max_score }} points !
see you tomorrow, nos vemos :-)
Ms. Hypyatia

9
templates/message.text Normal file
View File

@ -0,0 +1,9 @@
{# templates/message.txt #}
HOLA! {{ name }},
Estoy happy to inform you that you did very well on today's {{ test_name }}.
HAs alcanzado esta puntuación : {{ score }} de { max_score }} PUNTOS !
see you tomorrow, nos vemos :-)
Mr. JokerPy

25
write_messages.py Normal file
View File

@ -0,0 +1,25 @@
# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment(loader=FileSystemLoader("templates/"))
template = environment.get_template("message.txt")
for student in students:
filename = f"message_{student['name'].lower()}.txt"
content = template.render(
student,
max_score=max_score,
test_name=test_name
)
with open(filename, mode="w", encoding="utf-8") as message:
message.write(content)
print(f"... wrote {filename}")