OiO.lk Blog HTML Pandas HTML generation, reproducible output
HTML

Pandas HTML generation, reproducible output


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" >&nbsp;</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" >&nbsp;</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.



You need to sign in to view this answers

Exit mobile version