Comments (6)
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.
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.
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.
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.
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](https://private-user-images.githubusercontent.com/24256554/324129787-037496c9-3a59-4278-b0bf-65a5d402d5e0.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTY1MDM4MzYsIm5iZiI6MTcxNjUwMzUzNiwicGF0aCI6Ii8yNDI1NjU1NC8zMjQxMjk3ODctMDM3NDk2YzktM2E1OS00Mjc4LWIwYmYtNjVhNWQ0MDJkNWUwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA1MjMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNTIzVDIyMzIxNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWVlNmI1MWI4ZDFmZjY4ZWMwZjA2ZjljN2NlZGZhZTU3MWVkYmQzOWQ3MjU3ZWM0Zjg5MmNiOWU2YTAxMmIyZjUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.k3Rnv8xGq64e11TXQAAsCxcGS-xtsEXRA6Szp33qNLM)
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](https://private-user-images.githubusercontent.com/24256554/324130503-01c4401a-c2a3-4eb4-bfc4-0e417d02aa8d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTY1MDM4MzYsIm5iZiI6MTcxNjUwMzUzNiwicGF0aCI6Ii8yNDI1NjU1NC8zMjQxMzA1MDMtMDFjNDQwMWEtYzJhMy00ZWI0LWJmYzQtMGU0MTdkMDJhYThkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA1MjMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNTIzVDIyMzIxNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQ0YmRlZDhjMzVjOTI2YjRlN2EwYmQ2OGRlNDRkNWMzOGY0ZmIzYzU1YTg0NmEzMzliNWI4ZjE3Y2VhZjYyNDAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.bj5FR-xeJrAeFadoy0j_gg-8eWp7sz6RofdxvR9PAqw)
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](https://private-user-images.githubusercontent.com/24256554/324130976-30efb7ec-0046-410c-a7f4-a908a226971b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTY1MDM4MzYsIm5iZiI6MTcxNjUwMzUzNiwicGF0aCI6Ii8yNDI1NjU1NC8zMjQxMzA5NzYtMzBlZmI3ZWMtMDA0Ni00MTBjLWE3ZjQtYTkwOGEyMjY5NzFiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA1MjMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNTIzVDIyMzIxNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTk3NDRhMmQ2MDViM2I1YmY2MzQ3ZTU2NmFiNWQ0MDRmZDc3MzZhYWVkOWE5M2FlMzMzYjdhMjk3Y2M4YjM2MDEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.ijBoxudkEQTEsO18gOeg9njOFrh1JFKTiI_yBZniANI)
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.
Since it seems like this is the expected behavior, closing
from pandas.
Related Issues (20)
- BUG: Pandas 2 is broken! HOT 2
- BUG: 2-sided inplace drop loses freq in DatetimeIndex HOT 4
- BUG: read_orc does not use the provided filesystem for all operations HOT 1
- BUG: pd.to_datetime fails to identify actual date format HOT 4
- BUG: eval fails for ExtensionArray HOT 2
- ENH: Randomised row selection with read_csv() HOT 4
- BUG: read_parquet converts all digits strings to int HOT 2
- Make specific pandas dataframe column immuteable / not changeable HOT 4
- BUG: df.drop_duplicates fails if there is only a single row HOT 3
- Potential regression with PR "PERF: Eliminate circular references in accessor attributes (#58733)" HOT 1
- ENH: support parquet's enum type using Categorical when (de)serializing HOT 3
- ENH: generalize `__init__` on a `dict` to `abc.collections.Mapping` and `__getitem__` on a `list` to `abc.collections.Sequence` HOT 10
- ENH: Add a Series method which checks whether a Series is constant HOT 2
- BUG: df.agg with pd.NamedAgg axis=1 unsupported, but errors differently depending on contents of index HOT 1
- BUG: Segmentation Fault when importing Pandas in python 3.10.14 HOT 3
- BUG: df.agg with df with missing values results in IndexError HOT 3
- BUG: Groupby transformation (cumsum) output dtype depends on whether NA is among group labels HOT 9
- DOC: Docstrings missing from .py files in Sphinxext docs folder HOT 7
- BUG: Lookup by datetime in timestamp index does not work HOT 1
- DOC: Insufficient Project Background Information HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pandas.