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

Add table id option to pandas.DataFrame.to_html #8496

Closed
psychemedia opened this issue Oct 7, 2014 · 4 comments · Fixed by #19594
Closed

Add table id option to pandas.DataFrame.to_html #8496

psychemedia opened this issue Oct 7, 2014 · 4 comments · Fixed by #19594
Labels
IO HTML read_html, to_html, Styler.apply, Styler.applymap Output-Formatting __repr__ of pandas objects, to_string
Milestone

Comments

@psychemedia
Copy link
Contributor

I'm using the pandas.DataFrame.to_html() method to generate a table to embed into a a templated document which references the table via an id. It would be useful if pandas.DataFrame.to_html() supported a table_id parameter:

df.to_html(table_id='myTable')

to produce:

<table border="1" class="dataframe" id='mytable'>

@jreback jreback added the IO HTML read_html, to_html, Styler.apply, Styler.applymap label Oct 7, 2014
@jreback
Copy link
Contributor

jreback commented Oct 7, 2014

see #6488 , #3190, #4315

@jreback jreback added the Output-Formatting __repr__ of pandas objects, to_string label Oct 7, 2014
@jreback jreback added this to the 0.15.1 milestone Oct 7, 2014
@jreback
Copy link
Contributor

jreback commented Oct 7, 2014

We are looking for a more general soln (I pointed to some related issues). If you have some ideas, pls post/indicate!

@psychemedia
Copy link
Contributor Author

I'm not sure my py skills are up to contributing code but if I can abstract anything from use cases I come across I'll add them in to an appropriate thread.

Something that comes to mind in terms of generalisation is how to cope with outputting metadata, either about the dataframe itself, or additional to the dataframe in terms of presentation (for example, the id attribute for the table, or column layout information etc).

The ONS (Office of National Statistics) in the UK have started experimenting with the Tabular Data Package format which bundles a JSON file carrying metadata about the contents of one or more CSV files with those files packaged alongside it.

I've been wondering what a pandas to_datapackage() write method might have to do and what additional items are required for the datapackage that can't be extracted from a dataframe itself (we should be able to get column type information from the dataframe itself, for example).

(How to make effective use of all the metadata when doing a from_datapackage() read also raises interesting questions about how data metadata and provenance information could or should be managed within the pandas context.)

It seems to me that there are perhaps three sorts of metadata that need addressing in outputs:

  1. metadata about the data itself (eg column typing)
  2. metadata about data presentation (so eg column styling, table id etc)
  3. metadata about provenance, dataset title, collection methods etc etc. This might be passed to an output template when rendering the dataset, or bundled up as core/required metadata in a datapackage.json file for example.

@scls19fr
Copy link
Contributor

scls19fr commented Nov 14, 2014

Hello,

I was also looking for the same feature because I'm using Flask and want to output a DataFrame as HTML and have a nice formatting using a JS library for table. So I will need to refer to a given id in my JS code.

Having just an id parameter will help.

I understand the wish for a more general solution but it will be a pragmatic solution
to just add this id parameter as a short term solution.
A "quick and dirty" solution was given here
http://stackoverflow.com/questions/15079118/js-datatables-from-pandas
I post this... because it can temporarily help

Here is my own version (to support for example escape parameter also)

def df_to_html_with_id(df, id, *args, **kwargs):
    s = df.to_html(*args, **kwargs)
    return s[:7] + 'id="%s" ' % id + s[7:]

Kind regards

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 Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
3 participants