Skip to content

Commit

Permalink
Introduce a compact layout for ncol=1 (#89)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Kaszynski <akascap@gmail.com>
  • Loading branch information
prisae and akaszynski committed Jul 23, 2022
1 parent dcb53ff commit 4c95f5a
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions scooby/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,23 @@ class Report(PlatformInfo, PythonInfo):
optional : list(ModuleType), list(str)
A list of packages to list if they are available. If not available,
no warnings or error will be thrown.
Defaults to ['numpy', 'scipy', 'IPython', 'matplotlib', 'scooby']
Defaults to ``['numpy', 'scipy', 'IPython', 'matplotlib', 'scooby']``
ncol : int, optional
Number of package-columns in html table (no effect in text-version);
Defaults to 3.
text_width : int, optional
The text width for non-HTML display modes
The text width for non-HTML display modes.
sort : bool, optional
Sort the packages when the report is shown
Sort the packages when the report is shown.
extra_meta : tuple(str, str)
Additional two component pairs of meta information to display
extra_meta : tuple(str, str), optional
Additional two component pairs of meta information to display.
max_width : int, optional
Max-width of html-table. By default None.
"""

Expand All @@ -187,6 +190,7 @@ def __init__(
text_width=80,
sort=False,
extra_meta=None,
max_width=None,
):
"""Initialize report."""
# Set default optional packages to investigate
Expand All @@ -196,6 +200,7 @@ def __init__(
PythonInfo.__init__(self, additional=additional, core=core, optional=optional, sort=sort)
self.ncol = int(ncol)
self.text_width = int(text_width)
self.max_width = max_width

if extra_meta is not None:
if not isinstance(extra_meta, (list, tuple)):
Expand Down Expand Up @@ -229,15 +234,15 @@ def __repr__(self):
else:
row_width = 18

# ########## Platform/OS details ############
# Platform/OS details
repr_dict = self.to_dict()
for key in ['OS', 'CPU(s)', 'Machine', 'Architecture', 'RAM', 'Environment', 'File system']:
if key in repr_dict:
text += f'{key:>{row_width}} : {repr_dict[key]}\n'
for key, value in self._extra_meta:
text += f'{key:>{row_width}} : {value}\n'

# ########## Python details ############
# Python details
text += '\n'
for txt in textwrap.wrap('Python ' + self.sys_version, self.text_width - 4):
text += ' ' + txt + '\n'
Expand All @@ -248,13 +253,13 @@ def __repr__(self):
for name, version in self._packages.items():
text += f'{name:>{row_width}} : {version}\n'

# ########## MKL details ############
# MKL details
if MKL_INFO:
text += '\n'
for txt in textwrap.wrap(MKL_INFO, self.text_width - 4):
text += ' ' + txt + '\n'

# ########## Finish ############
# Finish
text += self.text_width * '-'

return text
Expand All @@ -267,11 +272,15 @@ def _repr_html_(self):
def colspan(html, txt, ncol, nrow):
r"""Print txt in a row spanning whole table."""
html += " <tr>\n"
html += " <td style='text-align: center; "
html += " <td style='"
if ncol == 1:
html += "text-align: left; "
else:
html += "text-align: center; "
if nrow == 0:
html += "font-weight: bold; font-size: 1.2em; "
html += border + " colspan='"
html += str(2 * ncol) + "'>%s</td>\n" % txt
html += f"{2 * ncol}'>{txt}</td>\n"
html += " </tr>\n"
return html

Expand All @@ -282,7 +291,8 @@ def cols(html, version, name, ncol, i):
html += " </tr>\n"
html += " <tr>\n"

html += " <td style='text-align: right;"
align = "left" if ncol == 1 else "right"
html += f" <td style='text-align: {align};"
html += " " + border + ">%s</td>\n" % name

html += " <td style='text-align: left; "
Expand All @@ -291,12 +301,15 @@ def cols(html, version, name, ncol, i):
return html, i + 1

# Start html-table
html = "<table style='border: 1.5px solid;'>\n"
html = "<table style='border: 1.5px solid;"
if self.max_width:
html += f" max-width: {self.max_width}px;"
html += "'>\n"

# Date and time info as title
html = colspan(html, self.date, self.ncol, 0)

# ########## Platform/OS details ############
# Platform/OS details
html += " <tr>\n"
repr_dict = self.to_dict()
i = 0
Expand All @@ -308,10 +321,10 @@ def cols(html, version, name, ncol, i):
# Finish row
html += " </tr>\n"

# ########## Python details ############
# Python details
html = colspan(html, 'Python ' + self.sys_version, self.ncol, 1)

html += " <tr>\n"

# Loop over packages
i = 0 # Reset count for rows.
for name, version in self.packages.items():
Expand All @@ -324,11 +337,11 @@ def cols(html, version, name, ncol, i):
# Finish row
html += " </tr>\n"

# ########## MKL details ############
# MKL details
if MKL_INFO:
html = colspan(html, MKL_INFO, self.ncol, 2)

# ########## Finish ############
# Finish
html += "</table>"

return html
Expand Down

0 comments on commit 4c95f5a

Please sign in to comment.