Compare commits

...

2 Commits

Author SHA1 Message Date
fenix ef3490b8f5 AÑADIMOS LOGICA al TPL - Plantilla 2024-02-12 17:20:41 +01:00
fenix ee87a0715a solución a Jj 'TemplateNotFound' 2024-02-12 16:27:19 +01:00
10 changed files with 96 additions and 36 deletions

View File

@ -9,3 +9,7 @@ Doc.: https://python-docs-es.readthedocs.io/es/3.10/library/venv.html
* Python ~ tutorial Jinja templates
https://realpython.com/primer-on-jinja-templating/
* Control flow - Jinja tpl 's
MUSICA INSTRUMENTAL : Wim Mertens

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)

9
message_frieda.txt Normal file
View File

@ -0,0 +1,9 @@
HOLA! Frieda,
Estoy happy to inform you that you did very well on today's Python Challenge.
HAs alcanzado esta puntuacion : 92 de { max_score }} PUNTOS !
see you tomorrow, nos vemos :-)
Mr. JokerPy

9
message_fritz.txt Normal file
View File

@ -0,0 +1,9 @@
HOLA! Fritz,
Estoy happy to inform you that you did very well on today's Python Challenge.
HAs alcanzado esta puntuacion : 40 de { max_score }} PUNTOS !
see you tomorrow, nos vemos :-)
Mr. JokerPy

9
message_gergeley.txt Normal file
View File

@ -0,0 +1,9 @@
HOLA! Gergeley,
Estoy happy to inform you that you did very well on today's Python Challenge.
HAs alcanzado esta puntuacion : 87 de { max_score }} PUNTOS !
see you tomorrow, nos vemos :-)
Mr. JokerPy

9
message_sandrine.txt Normal file
View File

@ -0,0 +1,9 @@
HOLA! Sandrine,
Estoy happy to inform you that you did very well on today's Python Challenge.
HAs alcanzado esta puntuacion : 100 de { max_score }} PUNTOS !
see you tomorrow, nos vemos :-)
Mr. JokerPy

9
message_sirius.txt Normal file
View File

@ -0,0 +1,9 @@
HOLA! Sirius,
Estoy happy to inform you that you did very well on today's Python Challenge.
HAs alcanzado esta puntuacion : 75 de { max_score }} PUNTOS !
see you tomorrow, nos vemos :-)
Mr. JokerPy

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

20
templates/message.txt Normal file
View File

@ -0,0 +1,20 @@
{# templates/message.txt #}
HOLA! {{ name }},
{% if score > 80 %}
( Acá Informamos tan sólo en caso de una cierta puntuación, por encima de un umbral )
Estoy happy to inform you that you did very well on today's {{ test_name }}.
HAs alcanzado esta puntuacion : {{ score }} de { max_score }} PUNTOS ! ;-)
{% else %}
Vaya! Siento informarte de que tu puntuacuón : {{ score }} es más que mejorable ... {{ test_name }} :-|
{% endif %}
see you tomorrow, nos vemos :-)
Mr. JokerPy

27
write_messages.py Normal file
View File

@ -0,0 +1,27 @@
# 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},
{"name": "Fritz", "score": 40},
{"name": "Sirius", "score": 75},
]
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}")