Skip to content
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

ENH:Need an option to get html and css separately from pd.DataFrame.to_html . #46946

Open
Teut2711 opened this issue May 5, 2022 · 4 comments
Labels
Enhancement IO HTML read_html, to_html, Styler.apply, Styler.applymap Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@Teut2711
Copy link

Teut2711 commented May 5, 2022

Is your feature request related to a problem?

I recently was writing a script to make html body of a mail to be sent through the gmail api and I was inserting pandas tables in the mail. Surprising to me, the css was missing in those mails. On googling a bit I got to know that these email clients remove the style tags and you need to inline your css if you want to use them. I used the python's css-inline package for now , but I was wondering if I could get the styles separately and thus remove the dependency.

Describe the solution you'd like

I just was thinking to put the css in a separate file and link it.For that I need to put the generated css by .to_html to a separate css file.

API breaking implications

html = df.to_html()
html, css = df.to_html()

Describe alternatives you've considered

Yes, I used css-inline to solve the problem temporarily.

Additional context

NA

# Your code here, if applicable
@Teut2711 Teut2711 added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels May 5, 2022
@simonjayhawkins simonjayhawkins added the IO HTML read_html, to_html, Styler.apply, Styler.applymap label May 5, 2022
@xignoe
Copy link

xignoe commented May 5, 2022

Hello, I would like to work on this issue!

@attack68
Copy link
Contributor

attack68 commented May 6, 2022

what css are you referring to? df.to_html produces basic (and deprecated) inline html css styles.

if you use Styler, you can access df.style.to_html and this is designed to produce css with a define <style > tag.

it would be easier to separate the style tag from the table tag using basic string search functions.

this is not something we would look to add a feature though, your use case is too detached from the main purpose of the to_html methods

@VisheshMangla2711
Copy link

VisheshMangla2711 commented May 15, 2022

Yes you are correct @attack68. I am using the styler and at the end I do my_styler.to_html() to get the html which had style tags in it. You are correct about the other thing too that we can retrieve it by string search methods, but letting users passing an option to the .to_html() can be better.

I mean something like this.

html = df.to_html(inline_css=True)
html, css = df.to_html(inline_css=False)

@fivage
Copy link

fivage commented Jan 21, 2024

You can make it work by putting the <style> tag in the <head> section. Gmail requires this.

<style type="text/css">
</style>
<table id="T_1e78e">

update to:

<head>
<style type="text/css">
</style>
</head>
<table id="T_1e78e">

Something like this can do the trick:
html.replace('<style type','<head><style type').replace('</style>','</style></head>')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement IO HTML read_html, to_html, Styler.apply, Styler.applymap Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

6 participants