From a8f2166dde8708872c8ff4c9f46b69485932e9d2 Mon Sep 17 00:00:00 2001 From: Kylian Schmidt Date: Fri, 26 Sep 2025 12:59:49 +0200 Subject: [PATCH] Add support for html plots --- assets/css/html-plots.css | 38 ++++++++++++++++++ generate_gallery.py | 83 +++++++++++++++++++++++++++++---------- templates/gallery.html | 34 ++++++++++------ 3 files changed, 123 insertions(+), 32 deletions(-) create mode 100644 assets/css/html-plots.css diff --git a/assets/css/html-plots.css b/assets/css/html-plots.css new file mode 100644 index 0000000..234a2e8 --- /dev/null +++ b/assets/css/html-plots.css @@ -0,0 +1,38 @@ +/* Styles for HTML plots */ +.plot-item.html-plot .plot-link { + position: relative; +} + +.plot-item.html-plot .html-thumbnail { + background: #f5f5f5; + border: 1px solid #ddd; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + min-height: 200px; + text-align: center; + padding: 20px; +} + +.plot-item.html-plot .html-indicator { + position: absolute; + top: 5px; + right: 5px; + background: #4CAF50; + color: white; + padding: 2px 6px; + border-radius: 3px; + font-size: 0.8em; +} + +.plot-item.html-plot .html-preview { + color: #666; + margin-top: 10px; + font-size: 0.9em; +} + +/* Add a hover effect */ +.plot-item.html-plot .plot-link:hover .html-thumbnail { + background: #e8e8e8; +} diff --git a/generate_gallery.py b/generate_gallery.py index 7b148ff..527b073 100644 --- a/generate_gallery.py +++ b/generate_gallery.py @@ -49,51 +49,89 @@ env.filters['strftime'] = strftime_filter template = env.get_template("templates/gallery.html") +def process_html_file(html_file: Path, web_dir: Path, current_metadata: Dict[str, Any] = None) -> dict: + """ + Process HTML plot file, copying it to web directory. + + Args: + html_file: Path to the source HTML file + web_dir: Target web directory + current_metadata: Current metadata dictionary for the plot + + Returns: + Dictionary containing plot information + """ + web_html = web_dir / html_file.name + + if needs_update(html_file, web_html): + shutil.copy2(html_file, web_html) + else: + print(f"Skipping {html_file.name} (up to date)") + + # Get source file creation time + source_creation_time = int(html_file.stat().st_ctime) + + # Resolve metadata if provided + plot_metadata = {} + if current_metadata is not None: + plot_metadata = resolve_metadata_for_plot(html_file, current_metadata) + + return { + "name": html_file.stem, + "html_href": html_file.name, + "is_html": True, + "metadata": plot_metadata, + "creation_time": source_creation_time + } + def process_plot_files( - pdf_file: Path, + plot_file: Path, web_dir: Path, current_metadata: Dict[str, Any] = None, ) -> dict: """ - Process PDF and PNG files, handling conversion and copying. - + Process plot files (PDF/PNG or HTML), handling conversion and copying. + Args: - pdf_file: Path to the source PDF file + plot_file: Path to the source plot file (PDF or HTML) web_dir: Target web directory current_metadata: Current metadata dictionary for the plot - + Returns: Dictionary containing plot information """ - png_file = pdf_file.with_suffix(".png") - web_pdf = web_dir / pdf_file.name + if plot_file.suffix.lower() == '.html': + return process_html_file(plot_file, web_dir, current_metadata) + + # Handle PDF files + png_file = plot_file.with_suffix(".png") + web_pdf = web_dir / plot_file.name web_png = web_dir / png_file.name - if needs_update(pdf_file, web_pdf): - shutil.copy2(pdf_file, web_pdf) + if needs_update(plot_file, web_pdf): + shutil.copy2(plot_file, web_pdf) else: - print(f"Skipping {pdf_file.name} (up to date)") + print(f"Skipping {plot_file.name} (up to date)") if not png_file.exists(): - convert_pdf_to_png(pdf_file) + convert_pdf_to_png(plot_file) if needs_update(png_file, web_png): shutil.copy2(png_file, web_png) else: print(f"Skipping {png_file.name} (up to date)") - # Get source file creation time - source_creation_time = int(pdf_file.stat().st_ctime) + source_creation_time = int(plot_file.stat().st_ctime) - # Resolve metadata if provided plot_metadata = {} if current_metadata is not None: - plot_metadata = resolve_metadata_for_plot(pdf_file, current_metadata) + plot_metadata = resolve_metadata_for_plot(plot_file, current_metadata) return { - "name": pdf_file.stem, - "pdf_href": pdf_file.name, + "name": plot_file.stem, + "pdf_href": plot_file.name, "png_href": png_file.name, + "is_html": False, "metadata": plot_metadata, "creation_time": source_creation_time } @@ -246,14 +284,19 @@ def build_gallery( folder_metadata = load_folder_metadata(source_dir) current_metadata = merge_metadata(inherited_metadata, folder_metadata) + # Find all plot files (both PDF and HTML) pdf_files = list(source_dir.glob("*.pdf")) + html_files = list(source_dir.glob("*.html")) + plot_files = pdf_files + html_files + items = [] plot_metadata_cache = {} - for pdf_file in pdf_files: - item = process_plot_files(pdf_file, web_dir, current_metadata) + # Process all plot files (PDFs and HTMLs) + for plot_file in plot_files: + item = process_plot_files(plot_file, web_dir, current_metadata) items.append(item) - plot_metadata_cache[pdf_file.stem] = item["metadata"] + plot_metadata_cache[plot_file.stem] = item["metadata"] save_metadata_cache(web_dir, plot_metadata_cache) diff --git a/templates/gallery.html b/templates/gallery.html index 35c7ea9..635852f 100644 --- a/templates/gallery.html +++ b/templates/gallery.html @@ -4,6 +4,7 @@ {{ title }} +