Giter Site home page Giter Site logo

Comments (5)

attack68 avatar attack68 commented on May 24, 2024

This is not a bug. It is as documented. escaping is done before formatter.

This is actually a feature by design and was implemented this way after careful consideration.

Achieving the result you want is possible. It is simply to design your own formatter that performs escaping after it has formatted your text. Then you do not add anything to the esacpe argument directly.

from pandas.

rhshadrach avatar rhshadrach commented on May 24, 2024

This is actually a feature by design and was implemented this way after careful consideration.

Just curious - what is the benefit of escaping prior?

from pandas.

attack68 avatar attack68 commented on May 24, 2024

To be honest I can't remember exactly, we are going back a few years.
It may be that in the way it is designed you can achieve both, whilst that might be trickier with the reverse.
It seems more natural that if you:

  • Turned on escape and added a formatter your formatter is applied to the strings that have been pre-escaped.
  • Turned off escape and added a formatter that formatter will operate directly on the strings unchanged (and then can include your own escaping function if you so choose).

from pandas.

rhshadrach avatar rhshadrach commented on May 24, 2024

To be honest I can't remember exactly, we are going back a few years.

Makes sense, no problem. The trouble I'm having is I can't envision a use case where you wouldn't want escaping to be the very last step.

It is simply to design your own formatter that performs escaping after it has formatted your text.

What would the code for this look like? At a glance, it sounds intricate in the general case.

from pandas.

attack68 avatar attack68 commented on May 24, 2024

OK I think it came to me.
Setup a DF and show it (you have to escape this to see the <div>):

df = DataFrame(["ab&%<div>"])
df.style.format(escape="html")
Screenshot 2024-04-20 at 00 20 46

Since Styler is a display tool one can use the formatter to hack elements into HTML or LaTeX, for specific rows, columns or even cells, with native HTML or LaTeX commands. Something like this:

df.style.format('<span style="color:red;">{}</span>', escape="html")
Screenshot 2024-04-20 at 00 26 25

This will not work if you escape after the fact. You can simulate escaping after as follows:

from pandas.io.formats.style_render import _str_escape
def custom_formatter(s):
    s = '<span style="color:red;">{}</span>'.format(s)
    return _str_escape(s, "html")
df.style.format(custom_formatter)
Screenshot 2024-04-20 at 00 31 41

Thus with the current system you can do everything if you know the various mechanisms.

You could of course argue that one could write a customer formatter to escape first and then format, but I do also think there were possibly some other reasons when mixing the other things that the format function did, e.g. hyperlinks, which shouldn't be escaped.

from pandas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.