Skip to content

Add raw_html parameter to Styler.format, enabling richer output #40388

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

Closed
jnothman opened this issue Mar 12, 2021 · 4 comments · Fixed by #40437
Closed

Add raw_html parameter to Styler.format, enabling richer output #40388

jnothman opened this issue Mar 12, 2021 · 4 comments · Fixed by #40437
Labels
IO HTML read_html, to_html, Styler.apply, Styler.applymap Styler conditional formatting using DataFrame.style
Milestone

Comments

@jnothman
Copy link
Contributor

Styler.format could allow a parameter raw_html (bool, False for current behaviour), enabling richer output

For example, df.style.format(lambda s: f"<a href='https://en.wikipedia.org/wiki/{html.escape(s)}'>{html.escape(s)}</a>", raw_html=True) could be used to turn a column of values into a column of links to English Wikipedia articles, when exported with to_html.

To achieve this, we would need to always pass raw HTML to the jina templates, changing how formatting is currently done in Styler.to_html.

Formats performed with raw_html=True would have no effect on Styler.to_excel.

@attack68
Copy link
Contributor

does using jinja2 safe keyword help here? {{ something.render() | safe }}? maybe that is an easy fix? maybe not..

@attack68 attack68 added Styler conditional formatting using DataFrame.style IO HTML read_html, to_html, Styler.apply, Styler.applymap labels Mar 12, 2021
@jnothman
Copy link
Contributor Author

The jinja2 safe keyword would be necessary for this feature to work. But complementary functionally in pandas to distinguish between safe and unsafe formats would be necessary

@attack68
Copy link
Contributor

attack68 commented Mar 14, 2021

@jnothman, OK ignore my comment I misunderstood I think I now understand what you are trying to achieve, correct me if I am wrong, but of the following:

from jinja2.filters import escape
df = pd.DataFrame([["just&'beans'", 'Fish<div>Chips']])
format_func_1 = lambda v: f'<a href="https://www.abc.com/{v}">{v}</a>'
format_func_2 = lambda v: f'<a href="https://www.abc.com/{escape(v)}">{v}</a>'
format_func_3 = lambda v: escape(f'<a href="https://www.abc.com/{v}">{v}</a>')
format_func_4 = lambda v: f'<a href="https://www.abc.com/{escape(v)}">{escape(v)}</a>'
df.style.format(format_func_x).render()

Will only render correctly in the final case. So basically it eliminates no-escaping (case 1), selective display value escaping (case 2) and post-formatting escaping (case 3). We need to escape the display value before the formatter acts on it.

i.e. format(lambda v: f'<a href="https://www.abc.com/{v}">{v}</a>', escape=True) should act as case 4.

@jreback jreback added this to the 1.3 milestone Mar 15, 2021
@jnothman
Copy link
Contributor Author

yes, it seems as though I was mistaken about the current behaviour. I had thought it would already escape the content for me. I had presumed so just because of the use of jinja templates. Now I need to go back and look at where in my code I'd thought I'd needed this!

In the context of my confusion, your PR seems appropriate @attack68!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO HTML read_html, to_html, Styler.apply, Styler.applymap Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants