Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Génération de rapports

APERTO-NOTA

Pug

Pug est un outil s’appuyant sur NodeJS. Il s’agit d’un compilateur transformant les fichiers .pug en HTML. Le module pypugjs permet de compiler des fichiers .pug (anciennement Jade) en HTML directement depuis Python. Cela permet d’utiliser ce moteur de template sans dépendre de Node.js.

Installation

pip install pypugjs

Exemple de compilation

import pypugjs

template_pug = """
html
  head
    title Page Pug
  body
    h1 Bonjour #{nom}
    p Ceci est un exemple généré avec pypugjs.
"""

# Compilation avec des variables
html = pypugjs.simple_convert(template_pug, context={"nom": "Alice"})
print(html)

Exercice

Solution to Exercise 1 #
import pypugjs
import webbrowser

# Lecture du template Pug
with open("profil.pug", "r", encoding="utf-8") as f:
    template = f.read()

# Entrée utilisateur
nom = input("Entrez votre nom : ")
email = input("Entrez votre email : ")

# Compilation en HTML
html = pypugjs.simple_convert(template, context={"nom": nom, "email": email})

# Sauvegarde dans un fichier HTML
with open("profil.html", "w", encoding="utf-8") as f:
    f.write(html)

# Ouverture dans le navigateur
webbrowser.open("profil.html")