I am writing a Pandas dataframe as HTML using this code
import pandas as pd
df = pd.DataFrame({ "a": [1] })
print(df.style.to_html())
I ran it once and it produced this output
<style type="text/css">
</style>
<table id="T_f9297">
<thead>
<tr>
<th class="blank level0" > </th>
<th id="T_f9297_level0_col0" class="col_heading level0 col0" >a</th>
</tr>
</thead>
<tbody>
<tr>
<th id="T_f9297_level0_row0" class="row_heading level0 row0" >0</th>
<td id="T_f9297_row0_col0" class="data row0 col0" >1</td>
</tr>
</tbody>
</table>
But when I run the same program again a moment later it gives
<style type="text/css">
</style>
<table id="T_d628d">
<thead>
<tr>
<th class="blank level0" > </th>
<th id="T_d628d_level0_col0" class="col_heading level0 col0" >a</th>
</tr>
</thead>
<tbody>
<tr>
<th id="T_d628d_level0_row0" class="row_heading level0 row0" >0</th>
<td id="T_d628d_row0_col0" class="data row0 col0" >1</td>
</tr>
</tbody>
</table>
I would like to get the same output each time. That is, the T_f9297
and T_d628d
identifiers shouldn't change from one run to the next. How can I get that?
I believe that I could generate HTML without any CSS styling and without the identifiers, but I do want CSS (I just omitted it from my example) and I'm happy to have the identifiers, as long as I get the same output given the same input data.
I am using Python 3.11.7 and Pandas 2.1.4.
pandas.io.formats.style.Styler.to_html
has a table_uuid
parameter:
table_uuid
str, optional: Id attribute assigned to the<table>
HTML element in the format:<table id="T_<table_uuid>" ..>
If not provided it generates a random uuid. If set it will use the uuid provided:
print(df.style.to_html(table_uuid="my_table_id"))
Output:
<style type="text/css">
</style>
<table id="T_my_table_id">
<thead>
<tr>
<th class="blank level0" > </th>
<th id="T_my_table_id_level0_col0" class="col_heading level0 col0" >a</th>
</tr>
</thead>
<tbody>
<tr>
<th id="T_my_table_id_level0_row0" class="row_heading level0 row0" >0</th>
<td id="T_my_table_id_row0_col0" class="data row0 col0" >1</td>
</tr>
</tbody>
</table>