Last active
September 28, 2023 14:10
-
-
Save Xophe92/14b46eab057d62b4cc0966c85fe2f400 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jinja2 | |
import weasyprint as wp | |
import os | |
data = { | |
"categories" : [ | |
{ | |
"title": "Test du chocolat et de la chantilly sur les voitures.", | |
"description" : "je suis la description de la catégorie 1.", | |
"tests" : [ | |
{ | |
"name" : "01a", | |
"description" : "Nous rapprochons le prix de la voiture du prix du chocolat qu'elle peut contenir", | |
"résultat" : {"testés" : 14, "failed" : 1, "succès" : "90%"} | |
}, | |
{ | |
"name" : "01b", | |
"description" : "Nous rapprochons le prix de la voiture de la chantilly qu'elle peut contenir", | |
"résultat" : {"testés" : 14, "failed" : 7, "succès" : "50%"} | |
}, | |
], | |
"comments":"Nous avons analysé les sources des anomalies et il semble que la date depéremption du chocolat n'est pas correctement renseignée dans la base." | |
}, | |
{ | |
"title": "Test du chocolat et de la chantilly sur les motos.", | |
"description" : "je suis la description de la catégorie 2.", | |
"tests" : [ | |
{ | |
"name" : "02a", | |
"description" : "Nous rapprochons le prix de la moto du prix du chocolat qu'elle peut contenir", | |
"résultat" : {"testés" : 21, "failed" : 7, "succès" : "66%"} | |
}, | |
{ | |
"name" : "02b", | |
"description" : "Nous rapprochons le prix de la moto de la chantilly qu'elle peut contenir", | |
"résultat" : {"testés" : 21, "failed" : 0, "succès" : "100%"} | |
}, | |
], | |
"comments":"Le bon niveau de performance s'expique par une remise à plat récente de l'ensemble des règles de gestion concernant les produits laitiers" | |
}, | |
] | |
} | |
environment = jinja2.Environment() | |
template = environment.from_string(""" | |
Hello, Laure! | |
============================ | |
Nous allons dans un premier temps itérer sur des carégories | |
{% for categorie in categories %} | |
Catégorie en cours : {{categorie.title}} | |
description de la carégorie : {{categorie.description}} | |
{% for test in categorie.tests %}{{test.name}} - {{test.description}} - failed {{test.résultat.failed}}/{{test.résultat.testés}} - success rate {{test.résultat.succès}} | |
{% endfor %} | |
{% endfor %} | |
""") | |
#print(template.render(data)) | |
template_html = environment.from_string(""" | |
<style> | |
@page { | |
size:A4; | |
margin: 2cm 2cm 3cm 2cm; | |
@bottom-center { | |
content: 'Page ' counter(page) ' of ' counter(pages); | |
} | |
} | |
header, footer { | |
position: fixed; | |
left: 0; | |
right: 0; | |
} | |
header { | |
/* subtract @page margin */ | |
top: -2cm; | |
} | |
header img { | |
height: 1cm | |
} | |
footer { | |
/* subtract @page margin */ | |
bottom: 0cm; | |
height: 3cm; | |
} | |
h1, h2, h3 { | |
color: #273479; | |
} | |
h2 { | |
counter-increment: h2; | |
counter-reset: h3; | |
} | |
h2 { | |
counter-reset: h3; | |
} | |
h2::before { | |
content: counter(h2)" - "; | |
} | |
h3::before { | |
counter-increment: h3; | |
content: counter(h2)"."counter(h3) ". "; | |
} | |
p{ | |
text-justify: auto; | |
} | |
.styled-table { | |
border-collapse: collapse; | |
margin: 25px 0; | |
font-size: 0.9em; | |
font-family: sans-serif; | |
min-width: 400px; | |
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15); | |
} | |
.styled-table thead tr { | |
background-color: #0070CE; | |
color: #ffffff; | |
text-align: left; | |
} | |
.styled-table th, | |
.styled-table td { | |
padding: 12px 15px; | |
} | |
.styled-table tbody tr { | |
border-bottom: 1px solid #dddddd; | |
} | |
.styled-table tbody tr:nth-of-type(even) { | |
background-color: #f3f3f3; | |
} | |
.styled-table tbody tr:last-of-type { | |
border-bottom: 2px solid #0070CE; | |
} | |
</style> | |
<header> | |
<img src="./logo.jpg" width="2cm"/>. | |
</header> | |
<footer> | |
</footer> | |
<H1>Revue des calculs relatifs aux prèts</H1> | |
<h2> Table Of Content </h2> | |
<p> | |
Avec l'aide de <a href="https://github.com/Kozea/WeasyPrint/issues/23">cet article</a>, il sera possilbe d'insérer une table des matières ! | |
</p> | |
<p> | |
Ce mémo a pour objectif...... | |
</p> | |
<p> | |
Nous présentons ci-dessous, catégories par catégorie, les résultats des tests réaliés | |
</p> | |
{% for categorie in categories %} | |
<p style="page-break-before: always" ></p> | |
<h2>{{categorie.title}}</h2> | |
<h3>description de la carégorie</h3> | |
<p> | |
{{categorie.description}} | |
</p> | |
<h3>Résultats des tests</h3> | |
<table class="styled-table"> | |
<thead> | |
<tr> | |
<td>Test Id</td> | |
<td>Description</td> | |
<td>échecs</td> | |
<td>Testé</td> | |
<td>Taux de succès</td> | |
</tr> | |
</thead> | |
<tbody> | |
{% for test in categorie.tests %} | |
<tr> | |
<td>{{test.name}}</td> | |
<td>{{test.description}}</td> | |
<td>{{test.résultat.failed}}</td> | |
<td>{{test.résultat.testés}}</td> | |
<td>{{test.résultat.succès}}</td> | |
</tr> | |
{% endfor %} | |
</tbody> | |
</table> | |
<h3> Analyse des résultats</h3> | |
<p> | |
{{categorie.comments}} | |
</p> | |
{% endfor %} | |
""") | |
HTML_code = template_html.render(data) | |
with open("index.html", mode="w") as file: | |
file.write(HTML_code) | |
base_url = os.path.dirname(os.path.realpath(__file__)) | |
wp.HTML(string=HTML_code,base_url=base_url).write_pdf("test.pdf") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment