Skip to content

Commit

Permalink
Start work on LaTeX button. #170
Browse files Browse the repository at this point in the history
Based on tikzplotly (by Thomas).
  • Loading branch information
JavierCladellas committed Jan 16, 2025
1 parent 63862b4 commit 35366d5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ traitlets
tabulate
typing-extensions>=4.12.2
python-dotenv
.
tikzplotly
.
3 changes: 2 additions & 1 deletion src/feelpp/benchmarking/report/atomicReports/atomicReport.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ def createReport(self, base_dir, renderer):
hash_params_headers = hash_params_headers,
description_path = self.description_path,
figures = controller.generateFiguresHtml(),
figure_csvs = controller.buildCsvs()
figure_csvs = controller.buildCsvs(),
figure_pgfs = controller.buildPgfs()
)
)

Expand Down
3 changes: 2 additions & 1 deletion src/feelpp/benchmarking/report/base/baseComponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ def createOverview(self,base_dir,renderer,parents,plots_config,master_df):
parent_catalogs = "-".join([parent.id for parent in parents]),
parents = parents,
figures = controller.generateFiguresHtml(),
figure_csvs = controller.buildCsvs()
figure_csvs = controller.buildCsvs(),
figure_pgfs = controller.buildPgfs()
)
)

Expand Down
28 changes: 28 additions & 0 deletions src/feelpp/benchmarking/report/base/controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from feelpp.benchmarking.report.figureFactory import FigureFactory
from feelpp.benchmarking.report.strategies import StrategyFactory

import tempfile
import tikzplotly

class Controller:
""" Controller component , it orchestrates the model with the view"""
def __init__(self, model, view):
Expand Down Expand Up @@ -28,6 +31,31 @@ def generateFiguresHtml(self):
figures = self.generateFigures()
return [fig.to_html() for fig in figures]

def buildPgfs(self):
figures = self.generateFigures()
pgf_figures = []

with tempfile.NamedTemporaryFile() as tmp_pgf_save:
for figure in figures:
if figure.frames:
animation_pgf = []
for frame in figure.frames:
tikzplotly.save(tmp_pgf_save.name,frame)

with open(tmp_pgf_save.name) as tmp:
animation_pgf.append(tmp.read())
pgf_figures.append("\n\n".join(animation_pgf))
else:
try:
tikzplotly.save(tmp_pgf_save.name,figure)
except IndexError as e:
print(e)
pgf_figures.append("")
else:
with open(tmp_pgf_save.name) as tmp:
pgf_figures.append(tmp.read())
return pgf_figures


def buildCsvs(self):
""" Create a list containing the data for each plot specified on the view config in CSV format.
Expand Down
14 changes: 8 additions & 6 deletions src/feelpp/benchmarking/report/templates/figures.html.j2
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{% for figure in figures %}
++++
<button id="download-csv-{{parent_catalogs}}-{{loop.index}}" > CSV </button>
<button id="download-pgf-{{parent_catalogs}}-{{loop.index}}" > LaTeX </button>
{{figure}}
++++
{% endfor %}


++++
<script>
function downloadString(data, filename) {
const blob = new Blob([data], { type: 'text/csv' });
function downloadString(data, filename, mimeType ) {
const blob = new Blob([data], { type: `${mimeType};charset=utf-8` });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
Expand All @@ -20,14 +21,15 @@ function downloadString(data, filename) {
URL.revokeObjectURL(url);
}
function addDownloadListener( button_id, data, filename ) {
function addDownloadListener( button_id, data, filename, mimeType = 'text/plain') {
document.getElementById(button_id).addEventListener('click', function() {
downloadString(data,filename)
downloadString(data,filename,mimeType);
});
}
{% for csv in figure_csvs %}
addDownloadListener('download-csv-{{parent_catalogs}}-{{loop.index}}' , `{{csv | safe}}`, 'data.csv')
{% for csv,pgf in zip(figure_csvs,figure_pgfs) %}
addDownloadListener('download-csv-{{parent_catalogs}}-{{loop.index}}' , `{{csv | tojson}}`, 'data.csv', 'text/csv')
addDownloadListener('download-pgf-{{parent_catalogs}}-{{loop.index}}' , `{{pgf | tojson}}`, 'figure.tex' )
{% endfor %}
</script>
Expand Down

0 comments on commit 35366d5

Please sign in to comment.