Skip to content

Commit

Permalink
Change html repr away from latex to a Sparse/Dask-like repr
Browse files Browse the repository at this point in the history
  • Loading branch information
jthielen committed Jul 14, 2020
1 parent b6e1811 commit ec65e47
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
6 changes: 1 addition & 5 deletions pint/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _pretty_fmt_exponent(num):
"single_denominator": True,
"product_fmt": r" ",
"division_fmt": r"{}/{}",
"power_fmt": "{{{}}}^{{{}}}", # braces superscript whole exponent
"power_fmt": r"{}<sup>{}</sup>",
"parentheses_fmt": r"({})",
},
"": { # Default format.
Expand Down Expand Up @@ -270,10 +270,6 @@ def format_unit(unit, spec, **kwspec):
(r"\mathrm{{{}}}".format(u.replace("_", r"\_")), p) for u, p in unit.items()
]
return formatter(rm, **fmt).replace("[", "{").replace("]", "}")
elif spec == "H":
# HTML (Jupyter Notebook)
rm = [(u.replace("_", r"\_"), p) for u, p in unit.items()]
return formatter(rm, **fmt)
else:
# Plain text
return formatter(unit.items(), **fmt)
Expand Down
63 changes: 39 additions & 24 deletions pint/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import operator
import re
import warnings
from html import escape
from typing import List

from packaging import version
Expand Down Expand Up @@ -66,6 +67,7 @@
SharedRegistryObject,
UnitsContainer,
infer_base_unit,
iterable,
logger,
to_units_container,
)
Expand Down Expand Up @@ -311,47 +313,62 @@ def __format__(self, spec):

spec = spec or self.default_format

if "L" in spec:
allf = plain_allf = r"{}\ {}"
else:
allf = plain_allf = "{} {}"

# If Compact is selected, do it at the beginning
if "#" in spec:
spec = spec.replace("#", "")
obj = self.to_compact()
else:
obj = self

if "L" in spec:
allf = plain_allf = r"{}\ {}"
elif "H" in spec:
allf = plain_allf = "{} {}"
if iterable(obj.magnitude):
allf = (
"<table><tbody>"
"<tr><th>Magnitude</th>"
"<td style='text-align:left;'>{}</td></tr>"
"<tr><th>Units</th><td style='text-align:left;'>{}</td></tr>"
"</tbody></table>"
)
else:
allf = plain_allf = "{} {}"

# the LaTeX siunitx code
if "Lx" in spec:
spec = spec.replace("Lx", "")
# TODO: add support for extracting options
opts = ""
ustr = siunitx_format_unit(obj.units)
allf = r"\SI[%s]{{{}}}{{{}}}" % opts
elif "H" in spec:
ustr = format(obj.units, spec)
assert ustr[:2] == r"\["
assert ustr[-2:] == r"\]"
ustr = ustr[2:-2]
allf = r"\[{}\ {}\]"
else:
ustr = format(obj.units, spec)

mspec = remove_custom_flags(spec)
if isinstance(self.magnitude, ndarray):
if "H" in spec:
if hasattr(obj.magnitude, "_repr_html_"):
mstr = obj.magnitude._repr_html_()
else:
if isinstance(self.magnitude, ndarray):
formatter = "{{:{}}}".format(mspec)
with printoptions(formatter={"float_kind": formatter.format}):
mstr = (
"<pre>"
+ format(obj.magnitude).replace("\n", "<br>")
+ "</pre>"
)
elif not iterable(obj.magnitude):
mstr = format(obj.magnitude, mspec)
else:
mstr = (
"<pre>"
+ format(obj.magnitude, mspec).replace("\n", "<br>")
+ "</pre>"
)
elif isinstance(self.magnitude, ndarray):
if "L" in spec:
mstr = ndarray_to_latex(obj.magnitude, mspec)
elif "H" in spec:
allf = r"\[{} {}\]"
# this is required to have the magnitude and unit in the same line
parts = ndarray_to_latex_parts(obj.magnitude, mspec)

if len(parts) > 1:
return "\n".join(allf.format(part, ustr) for part in parts)

mstr = parts[0]
else:
formatter = "{{:{}}}".format(mspec)
with printoptions(formatter={"float_kind": formatter.format}):
Expand All @@ -361,9 +378,7 @@ def __format__(self, spec):

if "L" in spec:
mstr = self._exp_pattern.sub(r"\1\\times 10^{\2\3}", mstr)
elif "H" in spec:
mstr = self._exp_pattern.sub(r"\1×10^{\2\3}", mstr)
elif "P" in spec:
elif "H" in spec or "P" in spec:
m = self._exp_pattern.match(mstr)
if m:
exp = int(m.group(2) + m.group(3))
Expand Down
4 changes: 0 additions & 4 deletions pint/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ def __format__(self, spec):
else:
units = self._units

if "H" in spec:
# HTML / Jupyter Notebook
return r"\[" + format(units, spec).replace(" ", r"\ ") + r"\]"

return format(units, spec)

def format_babel(self, spec="", **kwspec):
Expand Down

0 comments on commit ec65e47

Please sign in to comment.