Add support for html plots

This commit is contained in:
Kylian Schmidt
2025-09-26 12:59:49 +02:00
parent 041c761df0
commit a8f2166dde
3 changed files with 123 additions and 32 deletions
+38
View File
@@ -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;
}
+63 -20
View File
@@ -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)
+22 -12
View File
@@ -4,6 +4,7 @@
<meta charset="UTF-8">
<title>{{ title }}</title>
<link rel="stylesheet" href="{{ assets_path }}/css/main.css">
<link rel="stylesheet" href="{{ assets_path }}/css/html-plots.css">
<!-- MathJax for LaTeX rendering -->
<script>
@@ -173,21 +174,30 @@
<!-- Plot Container -->
<div class="plot-container grid-view" id="plotContainer">
{% for item in items %}
<div class="plot-item grid-item"
<div class="plot-item grid-item {% if item.is_html %}html-plot{% endif %}"
data-name="{{ item.name }}"
data-time="{{ item.creation_time|default(0) }}">
<a href="{{ item.pdf_href }}" class="plot-link">
<img src="{{ item.png_href }}" alt="{{ item.name }}" class="plot-thumbnail">
</a>
{% if item.is_html %}
<a href="{{ item.html_href }}" class="plot-link" target="_blank">
<div class="html-thumbnail">
<div class="html-indicator">HTML</div>
<div class="html-preview">Click to open interactive plot</div>
</div>
</a>
{% else %}
<a href="{{ item.pdf_href }}" class="plot-link">
<img src="{{ item.png_href }}" alt="{{ item.name }}" class="plot-thumbnail">
</a>
{% endif %}
<div class="plot-info">
<div class="plot-name" title="{{ item.name }}">{{ item.name }}</div>
<div class="plot-date" title="Created: {{ item.creation_time|default(0)|int|datetime_from_timestamp|strftime('%Y-%m-%d %H:%M') if item.creation_time and item.creation_time|int > 0 else 'Unknown' }}">
{% if item.creation_time and item.creation_time|int > 0 %}
{{ item.creation_time|int|datetime_from_timestamp|strftime('%Y-%m-%d') }}
{% else %}
Unknown
{% endif %}
</div>
<div class="plot-name" title="{{ item.name }}">{{ item.name }}</div>
<div class="plot-date" title="Created: {{ item.creation_time|default(0)|int|datetime_from_timestamp|strftime('%Y-%m-%d %H:%M') if item.creation_time and item.creation_time|int > 0 else 'Unknown' }}">
{% if item.creation_time and item.creation_time|int > 0 %}
{{ item.creation_time|int|datetime_from_timestamp|strftime('%Y-%m-%d') }}
{% else %}
Unknown
{% endif %}
</div>
</div>
</div>
{% endfor %}