Closed mliszcz closed 2 years ago
For now, I monkey patched it with this code:
OriginalTextWrap = tabulate._CustomTextWrap
class PatchedTextWrap(OriginalTextWrap):
def wrap(self, cell):
return ['\n'.join(super(OriginalTextWrap, self).wrap(line)) for line in cell.splitlines() if line.strip() != '']
tabulate._CustomTextWrap = PatchedTextWrap
I found the monkey patch helpful but also unclear. Here's a more complete version of it.
# patched_tabulate.py
import tabulate as original_tabulate
# This is to work around an issue where new lines were getting stripped out of column cells when using maxcolwidths option
# Make sure that when you import `tabulate` from this module to use the patched version
# Access the _CustomTextWrap class from the tabulate module
OriginalTextWrap = original_tabulate._CustomTextWrap
# Define the patched version of the _CustomTextWrap class
class PatchedTextWrap(OriginalTextWrap):
def wrap(self, cell):
return ['\n'.join(super(OriginalTextWrap, self).wrap(line)) for line in cell.splitlines() if line.strip() != '']
# Apply the monkeypatch
original_tabulate._CustomTextWrap = PatchedTextWrap
# Re-export everything from the original tabulate module
# This makes sure that you can still access all functions and classes as usual.
__all__ = ['tabulate', 'tabulate_formats', 'simple_separated_format']
tabulate = original_tabulate.tabulate
tabulate_formats = original_tabulate.tabulate_formats
simple_separated_format = original_tabulate.simple_separated_format
maxcolwidths does not work well with multiline cells. Using the latest v0.8.10 I'm observing this behavior:
Note that the first one does not have a line break between
bbb
andccc
. The expected result is:This can be fixed by changing the
_wrap_text_to_colwidths
from:to:
As described https://stackoverflow.com/a/26538082.
I can send a PR if such change would be accepted.