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: Html table export: Add of other attributes #6488

Closed
Gjacquenot opened this issue Feb 26, 2014 · 7 comments
Closed

ENH: Html table export: Add of other attributes #6488

Gjacquenot opened this issue Feb 26, 2014 · 7 comments
Labels
Closing Candidate May be closeable, needs more eyeballs Enhancement IO HTML read_html, to_html, Styler.apply, Styler.applymap Output-Formatting __repr__ of pandas objects, to_string Styler conditional formatting using DataFrame.style

Comments

@Gjacquenot
Copy link

The to_html method allows to specify a set of classes for table rendering

However, I would like to specify other attribute to the table markup

Typically I want to use the DataTables (table plug-in for jQuery) library, that requires another attribute for rendering table
Here is an example

https://datatables.net/release-datatables/examples/basic_init/multi_col_sort.html

In method write_result of class HTMLFormatter in pandas/core/format.py
there is this command that I would like to update with other attributes

    self.write('<table border="1" class="%s">' % ' '.join(_classes),
               indent)

I tweak it for my personal needs, and it looks like this.

    self.write('<table border="1" class="%s" id="example">' % ' '.join(_classes),
               indent)

But I would like to be more general.
I would like to give a dictionary to the to_html method that will expand and produce the line below.

Is it possible?

@jreback jreback added this to the 0.14.0 milestone Feb 26, 2014
@jreback
Copy link
Contributor

jreback commented Feb 26, 2014

related is #3190 which proposes to do this with templates

however, I think a possible solution is as follows.

modify core/frame/to_html.py to allow it to take a custom HTMLFormatter (as a keyword argument), defaulting to the current HTMLFormatter, which is then instantiated in core/format.py/DataFrameFormatter/to_html.

Then modify HTMLFormatter to be a bit more flexible (so in stead of 'hard coding' things, would just call methods that have defaults); these can be overriden by the user via a custom sub-class of HTMLFormatter, or by passing in different defaults at construction time

e.g.

from pandas.core.format import HTMLFormatter
df.to_html(........, formatter = HTMLFormatter(.....))

and need to modify HTMLFormatter a bit to make it a bit more sub-class friendly, as well as to deal with passes in kwargs better. (as it will be instantiated before the format
is available)

class HTMLFormatter(TableFormatter):

    def __init__(self, fmt=None,.......):
          self.fmt = fmt

    def set_formatter(self, fmt):
          self.fmt = fmt

    def write_result(self, buf):
            ........
            self.write_classes(self):
            self.write(....)

lmk if you are up for this....It seems a much bigger change that it actually is, but it is necessary
in order to avoid having the user pass a gazillion number of parameters in that pandas then has to handle. This makes the most useful of parameters/attributes available for modification by simply overriding the HTMLFormatter class.

@jreback jreback modified the milestones: 0.15.0, 0.14.0 Mar 28, 2014
@hmeine
Copy link

hmeine commented Jun 4, 2014

Currently, the HTMLFormatter methods take a 'tags' kwarg, IMHO that should be named 'attributes' instead (in order to match expected HTML/XML terminology).

@CRP
Copy link
Contributor

CRP commented Jun 4, 2014

How about adding the scope attribute to the <td>
tag in order to allow for separate formatting of columns and index?
Currently, I can add a style such as <style>th {text-align: left}</style>, but this will affect both axes. Or does anyone know a different way to achieve the same result?

Thanks

@hmeine
Copy link

hmeine commented Jun 4, 2014

This could be done similar to #7338 , but I think in this case it may even be possible using non-trivial CSS selectors like

table.dataframe tr:firstchild { color: red }

Of course, that would not deal with hierarchical indices yet, so I am +1 on additional classes.

@CRP
Copy link
Contributor

CRP commented Jun 4, 2014

Yeah, I tried that but results are bad with MultiIndex, so this is a no go.
I understand that the pandas team is trying to keep the "output formatting" features at the minimum level, as this can be handled perfectly well by dedicated packages or style sheets, but for the latter to be really useful classes should be added wherever possible.

@attack68
Copy link
Contributor

This is old, now addressed with Styler?

@attack68 attack68 added Styler conditional formatting using DataFrame.style Closing Candidate May be closeable, needs more eyeballs labels Feb 20, 2021
@jreback
Copy link
Contributor

jreback commented Feb 20, 2021

yep closing

@jreback jreback closed this as completed Feb 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs Enhancement IO HTML read_html, to_html, Styler.apply, Styler.applymap Output-Formatting __repr__ of pandas objects, to_string Styler conditional formatting using DataFrame.style
Projects
None yet
Development

No branches or pull requests

9 participants