Skip to content
Snippets Groups Projects
Commit 0c3108a8 authored by Thomas Kluyver's avatar Thomas Kluyver
Browse files

Use DisplayTables in calcat_interface

parent 34077af6
No related branches found
No related tags found
1 merge request!1040Make constant tables in both markdown & latex format
......@@ -16,7 +16,7 @@ from calibration_client.modules import (
PhysicalDetectorUnit,
)
from cal_tools.tools import module_index_to_qm
from cal_tools.tools import module_index_to_qm, DisplayTables
__all__ = [
"CalCatError",
......@@ -754,8 +754,7 @@ class CalibrationData:
Defaults to
"https://in.xfel.eu/calibration/calibration_constant_versions/".
"""
from IPython.display import Markdown, display
from tabulate import tabulate
from IPython.display import display
if metadata is None:
metadata = self.metadata()
......@@ -768,6 +767,8 @@ class CalibrationData:
cal_groups = [
list(calibrations)[x:x+4] for x in range(0, len(calibrations), 4)]
tables = []
# Loop over groups of calibrations.
for cal_group in cal_groups:
table = [["Modules"] + cal_group]
......@@ -786,22 +787,17 @@ class CalibrationData:
c_mdata["begin_validity_at"]).strftime(
"%Y-%m-%d %H:%M")
mod_consts.append(
f"[{c_time}]({ccvs_url}/{c_mdata['ccv_id']})")
(c_time, f"{ccvs_url}/{c_mdata['ccv_id']}")
)
else:
# Constant is not available for this module.
mod_consts.append("___")
table.append([mod] + mod_consts)
display(
Markdown(
tabulate(
table,
tablefmt="pipe",
headers="firstrow",
)
)
)
tables.append(table)
display(DisplayTables(tables))
def _build_condition(self, parameters):
cond = dict()
......
......@@ -1065,3 +1065,55 @@ def raw_data_location_string(proposal: str, runs: List[int]):
" a preceding 'p'. Example: 'p900203'")
return f"proposal:{proposal} runs:{' '.join(map(str, runs))}"
class DisplayTables:
def __init__(self, tables):
# list (tables) of lists (rows) of lists (cells). A cell may be str,
# or a (text, url) tuple to make a link.
self.tables = tables
@staticmethod
def _build_table(table, fmt_link):
res = []
for row in table:
prepd_row = []
for cell in row:
if isinstance(cell, tuple):
text, url = cell
else:
text = cell
url = None
if url is None:
prepd_row.append(text)
else:
prepd_row.append(fmt_link(text, url))
res.append(prepd_row)
return res
def _repr_markdown_(self):
from tabulate import tabulate
def fmt_link(text, url):
return f"[{text}]({url})"
prepd_tables = [self._build_table(table, fmt_link) for table in self.tables]
return '\n\n'.join(
tabulate(t, tablefmt="pipe", headers="firstrow")
for t in prepd_tables
)
def _repr_latex_(self):
from tabulate import tabulate
def fmt_link(text, url):
return r'\href{%s}{%s}' % (url, text)
prepd_tables = [self._build_table(table, fmt_link) for table in self.tables]
return '\n\n'.join(
tabulate(t, tablefmt="latex_raw", headers="firstrow")
for t in prepd_tables
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment