From fae3ca91ae1ce6aee10bdaa5e6a4a3229334d6e5 Mon Sep 17 00:00:00 2001 From: rmanach Date: Wed, 29 Dec 2021 11:14:51 +0100 Subject: [PATCH] create tmp plots and move them after generation (does not affect html plots) --- drees.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/drees.py b/drees.py index 49879a6..06aacac 100644 --- a/drees.py +++ b/drees.py @@ -2,6 +2,7 @@ import argparse import json import logging import os +import re from datetime import datetime as dt from enum import Enum from functools import partial @@ -21,10 +22,17 @@ DATE_FORMAT = "%Y-%m-%d" DATA_URL = "https://data.drees.solidarites-sante.gouv.fr/api/records/1.0/search/?dataset=covid-19-resultats-par-age-issus-des-appariements-entre-si-vic-si-dep-et-vac-si&q=&rows=-1&facet=date&facet=vac_statut&facet=age" DATA_REPOSITORY = "data" + STATIC_REPOSITORY = "static" OUTPUT_REPOSITORY = os.path.join(STATIC_REPOSITORY, "plots") BUILD_REPOSITORY = "build" +TMP_SUFFIX = ".tmp" +FORMAT_SUFFIX = ".png" +OUTPUT_SUFFIX = f"{TMP_SUFFIX}{FORMAT_SUFFIX}" + +TMP_FILE_REGEX = re.compile(r"^.*{}$".format(OUTPUT_SUFFIX.replace(".", "\."))) + MAIN_URL = "https://covid.thegux.fr/" # cycler could be better, but for ages plots it's ok @@ -270,7 +278,7 @@ def save_and_close_fig( plt.legend() if is_tight: plt.tight_layout() - plt.savefig(output_path) + plt.savefig(f"{output_path}{OUTPUT_SUFFIX}") plt.close(fig) logging.info(f"{output_path} plotted") @@ -499,6 +507,16 @@ def get_age_field_args() -> List[Tuple[AgeGroup, Field]]: return pool_args +def move_tmp_plots() -> None: + """ + move .tmp.png plots into .png after generation + """ + for filename in os.listdir(OUTPUT_REPOSITORY): + file_path = os.path.join(OUTPUT_REPOSITORY, filename) + if re.match(TMP_FILE_REGEX, filename): + os.rename(file_path, file_path.replace(OUTPUT_SUFFIX, FORMAT_SUFFIX)) + + def generate_html_page(np_date: np.ndarray) -> None: logging.info("generating html page with plots...") os.makedirs(BUILD_REPOSITORY, exist_ok=True) @@ -515,7 +533,7 @@ def generate_html_page(np_date: np.ndarray) -> None: "status": VacStatus, "static": os.path.join(MAIN_URL, STATIC_REPOSITORY), "src": DATA_URL, - "period": f"{date_start} -> {date_end}" + "period": f"{date_start} -> {date_end}", } ) with open(os.path.join(BUILD_REPOSITORY, "index.html"), "w") as f: @@ -585,5 +603,7 @@ if __name__ == "__main__": plot_cumulative_field(np_data, np_date, field) plot_bar_age_percent_by_field(np_data, np_date, field) + move_tmp_plots() + if args.to_html: generate_html_page(np_date)