import json import os import sys from jinja2 import Environment, FileSystemLoader def generate_html_from_json(json_file, output_html, template_file='template.html'): """ Génère une page HTML à partir d'un fichier JSON contenant les données de décompte pour une ville donnée en utilisant un template Jinja2. Args: json_file (str): Chemin vers le fichier JSON contenant les données output_html (str): Chemin où sauvegarder le fichier HTML généré template_file (str): Nom du fichier template à utiliser (par défaut: template.html) """ # Vérifier si le fichier JSON existe if not os.path.exists(json_file): print(f"Erreur: Le fichier {json_file} n'existe pas.") return False try: with open(json_file, 'r', encoding='utf-8') as file: data = json.load(file) except json.JSONDecodeError: print(f"Erreur: Le fichier {json_file} n'est pas un JSON valide.") return False except Exception as e: print(f"Erreur lors de la lecture du fichier: {str(e)}") return False # Configuration de Jinja2 env = Environment(loader=FileSystemLoader('.')) # Vérifier si le template existe if not os.path.exists(template_file): print(f"Erreur: Le fichier template {template_file} n'existe pas.") return False try: template = env.get_template(template_file) except Exception as e: print(f"Erreur lors du chargement du template: {str(e)}") return False # Calculer quelques statistiques supplémentaires ratio_cyclable = data["road_cycleway_km"] / data["longueur_route_km"] * 100 if data["longueur_route_km"] > 0 else 0 ratio_parking_surface = data["surface_parking_km2"] / data["surface_route_km2"] * 100 if data["surface_route_km2"] > 0 else 0 # Rendu du template avec les données try: html_content = template.render( city_name=data.get("city_name", "la ville"), longueur_route_km=data["longueur_route_km"], road_cycleway_km=data["road_cycleway_km"], compte_highways=data["compte_highways"], surface_route_km2=data["surface_route_km2"], compte_piste_cyclable=data["compte_piste_cyclable"], roundabout_count=data["roundabout_count"], mini_roundabout_count=data["mini_roundabout_count"], building_count=data["building_count"], building_area=data["building_area"], surface_parking_km2=data["surface_parking_km2"], surface_bicycle_parking_km2=data["surface_bicycle_parking_km2"], car_parking_capacity_provided=data["car_parking_capacity_provided"], building_size_counts=data["building_size_counts"], charging_stations=data["charging_stations"], charging_stations_with_capacity_count=data["charging_stations_with_capacity_count"], charging_stations_capacity_provided=data["charging_stations_capacity_provided"], charging_points=data["charging_points"], # Statistiques supplémentaires ratio_cyclable=ratio_cyclable, ratio_parking_surface=ratio_parking_surface, date_generation=data.get("date_generation", "Non spécifiée") ) except KeyError as e: print(f"Erreur: Clé manquante dans les données JSON: {str(e)}") return False except Exception as e: print(f"Erreur lors du rendu du template: {str(e)}") return False try: with open(output_html, 'w', encoding='utf-8') as html_file: html_file.write(html_content) print(f"Le fichier HTML a été généré avec succès: {output_html}") return True except Exception as e: print(f"Erreur lors de l'écriture du fichier HTML: {str(e)}") return False def main(): """ Fonction principale qui traite les arguments de ligne de commande et génère le fichier HTML. """ if len(sys.argv) < 2: print("Usage: python present.py [fichier_html_sortie] [fichier_template]") print(" : Chemin vers le fichier JSON contenant les données") print(" [fichier_html_sortie]: Chemin où sauvegarder le fichier HTML (par défaut: resultat.html)") print(" [fichier_template]: Nom du fichier template à utiliser (par défaut: template.html)") return json_file = sys.argv[1] output_html = sys.argv[2] if len(sys.argv) > 2 else "resultat.html" template_file = sys.argv[3] if len(sys.argv) > 3 else "template.html" generate_html_from_json(json_file, output_html, template_file) if __name__ == "__main__": main()