JA-pythonista
JA-pythonista

Reputation: 1323

How to get or assign a table id when rendering your styling in Pandas

I am trying to assign a table id to the rendered HTML file generated from pandas using Styler.render()

After defining the styles I want for my table in my python file.

I styled my table below:

styler = df.style.set_table_styles(styles)

Then rendering my styles gives me a dynamic table id

styler.render()

How do I get the table id or assign a table id so that I can use it elsewhere in my code?

I read the following: How to inject a table id into pandas.DataFrame.to_html() output?

But this is not working for me maybe because I am not exporting from from pandas but rather styling and rendering? Kindly advise

Upvotes: 1

Views: 1204

Answers (1)

Tim Fanselow
Tim Fanselow

Reputation: 56

I'm using

styler.render(uuid="my_id")

which gives a table with id:

<table id="T_my_id" >

This at least gives a predictable id, although you probably didn't want the T_ prefix

Looking at the template used for html rendering, it looks like the table id is set as

id="T_{{uuid}}"

then any attributes you pass are appended after that. This will be why df.style.set_table_attributes('id="ABC"').render() gave you two ids, as mentioned in the comments to your question.

I think if you want to avoid the T_ prefix you'd have to provide your own template for rendering, although I didn't look into how to achieve that.

Upvotes: 3

Related Questions