repytex/Repytex/reports/filters.py

49 lines
1.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# encoding: utf-8
from uuid import uuid4
from path import Path
import logging
import matplotlib.pyplot as plt
2017-03-07 10:08:50 +00:00
logger = logging.getLogger(__name__)
2017-04-17 10:10:20 +00:00
def includegraphics(fig_ax, document_path="./", fig_path="fig/",
2017-04-17 10:10:20 +00:00
prefix="", scale=1):
""" Jinja2 filter which save the figure
and return latex includegraphic to display it.
:param fig_ax: TODO
:param path: TODO
:param prefix: TODO
:param scale: TODO
:returns: TODO
"""
try:
fig, ax = fig_ax
2017-03-07 15:52:37 +00:00
except (TypeError, ValueError):
ax = fig_ax
fig = ax.figure
if prefix:
filename = "{}_{}.pdf".format(prefix, str(uuid4())[:8])
else:
filename = "{}.pdf".format(str(uuid4())[:8])
path_to_file = Path(document_path/fig_path)
path_to_file.mkdir_p()
2017-03-31 16:56:12 +00:00
fig.tight_layout()
fig.savefig(path_to_file/filename)
2017-04-17 10:10:20 +00:00
logger.info(f"Graphique {filename} sauvé à {path_to_file}")
plt.close(fig)
return "\includegraphics[scale={sc}]{{{f}}}".format(sc = scale,
f=Path(fig_path)/filename)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
2017-04-17 10:10:20 +00:00
# cursor: 16 del