-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Styling console/terminal output #18066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@scls19fr : Thanks for the report! I think in the interest of modularity, we would probably want to move away from incorporating giant chunks of code into our codebase. That being said, we could add functions that call those function from something like |
RStudio adds color to the output of a data frame in the console: Since IPython now supports syntax highlighting in a normal console, I was wondering if something similar was possible in pandas. This would likely not require any additional packages, but instead adding color codes to the |
yes this certainly could be done. would likely take a separate formatting method (just to avoid having string / html / color strings code) in one place. |
Great! What do you mean with separate method? Ideally, color strings should be used when I type |
ANSI escape color codes work, so it is pretty straightforward to get colorized output, e.g.: The bigger questions are:
|
Is this going to be doable with how Styler used jinja, but I'm not sure what our appetite is for adopting that as a full dependency. |
I guess it's doable in If there is an external tool that can do this coloring for us, we should use it if it means less work implementing it. It could be an optional dependency, in the sense that if it's not installed there will only be plain non-colored output. |
@cbrnr wrote:
Say I wanted to colorize just specific table cells upon output to a terminal to highlight them, based on a list of iloc-compatible cell locations. How would I do that? I just need some pointers on what Pandas functions to tweak to allow this. Thanks. |
I could do somehting like
|
Interesting. This was already suggested in #459, but never implemented. |
Thanks for pointing me to this. It would be nice to have this as a core Pandas capability but at least terminal styling is possible with this package.
…-- Bob
On Jun 12, 2019, at 10:55 PM, Rohan Machado ***@***.***> wrote:
I could do somehting like
from colorama import Fore, Back, Style
df[c] = Fore.RED + Style.BRIGHT + df[c].astype(str) + Style.RESET_ALL
print (df)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
I was looking for a way to use ANSI color codes in the terminal or qtconsole for a long time, and finally put this together. The main problem was that ANSI codes were being incorporated into the print width calculation, which messed up registration of the columns. This is too hacky for a pull request, but it solves my problem, so I thought I'd post it in case it helps anyone else. You can replace the "TextAdjustment" class with the version below in this file:
|
I got similar requirements to colorize specific column and here's my workaround:
thanks @texxronn |
Trying to use corlorama on a pandas dataframe, but running into the same problem with misaligned printing menioned above: #18066 (comment) Does anyone know of a way to patch this behavior in without modifying pandas itself? I see pandas.DataFrame.to_string has a |
I've updated the workaround from @ghost711 to work with pandas 1.2.4. Hopefully this feature will be supported in the main branch somehow, but in the meantime: def monkeypatch_pandas():
"""
References:
https://github.com/pandas-dev/pandas/issues/18066
"""
import pandas.io.formats.format as format_
from six import text_type
# Made wrt pd.__version__ == '1.2.4'
class TextAdjustmentMonkey(object):
def __init__(self):
import re
self.ansi_regx = re.compile(r'\x1B[@-_][0-?]*[ -/]*[@-~]')
self.encoding = format_.get_option("display.encoding")
def len(self, text):
return len(self.ansi_regx.sub('', text))
def justify(self, texts, max_len, mode='right'):
jfunc = str.ljust if (mode == 'left') else \
str.rjust if (mode == 'right') else str.center
out = []
for s in texts:
escapes = self.ansi_regx.findall(s)
if len(escapes) == 2:
out.append(escapes[0].strip() +
jfunc(self.ansi_regx.sub('', s), max_len) +
escapes[1].strip())
else:
out.append(jfunc(s, max_len))
return out
def _join_unicode(self, lines, sep=''):
try:
return sep.join(lines)
except UnicodeDecodeError:
sep = text_type(sep)
return sep.join([x.decode('utf-8') if isinstance(x, str) else x
for x in lines])
def adjoin(self, space, *lists, **kwargs):
# Add space for all but the last column:
pads = ([space] * (len(lists) - 1)) + [0]
max_col_len = max([len(col) for col in lists])
new_cols = []
for col, pad in zip(lists, pads):
width = max([self.len(s) for s in col]) + pad
c = self.justify(col, width, mode='left')
# Add blank cells to end of col if needed for different col lens:
if len(col) < max_col_len:
c.extend([' ' * width] * (max_col_len - len(col)))
new_cols.append(c)
rows = [self._join_unicode(row_tup) for row_tup in zip(*new_cols)]
return self._join_unicode(rows, sep='\n')
format_.TextAdjustment = TextAdjustmentMonkey |
For what it's worth, I found it pretty frustrating that even something basic like representing NaNs differently on the terminal is impossible. For HTML and LaTeX output, there's the Styler and its na_rep argument, but there doesn't seem to be something equivalent for terminal output. I'm currently working around this by replacing NaNs with def float_format(value: float):
if isinf(value):
return ''
return str(value)
pd.options.display.float_format = float_format A simple solution would be to also pass NaNs through the float_format function, but I guess that's not an option considering backward compatibilty. |
I still think that rendering the output with Rich would be the best option. Basically, |
Hello,
Since v0.17.1, Pandas have an interesting API for styling Jupyter HTML outputs for Series / DataFrames...
https://pandas.pydata.org/pandas-docs/stable/style.html
It will be a nice feature to be able to have styling for console/terminal output
I wonder if such feature should be part of Pandas core or be inside an other package (like tabulate from @astanin which was mentioned in #11052
There is also PrettyPandas from @HHammond
Kind regards
The text was updated successfully, but these errors were encountered: