how to add class in table in python using Dominate library

3.5k views Asked by At

I have created Table using Dominate Library but Now I want to change my table class. can someone help me to do that ?

doc1 = dominate.document(title='Dominate your HTML')
    with doc1:
        with div():
            attr(cls='body')
            h1('Survey Report : Survey Report')
    oc = dominate.document(title="whatever")
    with doc1:
        tags.style(".calendar_table{width:880px;}")
        tags.style("body{font-family:Helvetica}")
        tags.style("h1{font-size:x-large}")
        tags.style("h2{font-size:large}")
        tags.style("table{border-collapse:collapse}")
        tags.style("th{font-size:small;border:1px solid gray;padding:4px;background-color:#DDD}")
        tags.style("td{font-size:small;text-align:center;border:1px solid gray;padding:4px}")
        with tags.table():
            with tags.thead():
                tags.th("Nominee", style = "color:#ffffff;background-color:#6A75F2")
                tags.th("counts", style = "color:#ffffff;background-color:#6A75F2")
            with tags.tbody():
                for i in range(0,len(nom)):
                    with tags.tr(): #Row 1
                        tags.td(nom[i], style = "font-size:small;text-align:center;padding:4px")
                        if int(count_nom[i]) > 1:
                            tags.td(count_nom[i], style = "font-size:small;text-align:center;padding:4px;background-color:#F4D8D2")
                        else:
                            tags.td(count_nom[i], style = "font-size:small;text-align:center;padding:4px")
                with tags.tr(): #Row 1
                        tags.td(b("Grand Total"), style = "font-size:small;text-align:center;padding:4px")
                        tags.td(b(sum(count_nom)), style = "font-size:small;text-align:center;padding:4px")
with open('/root/survey/'+'survey'+'.html', 'w') as f:
        f.write(doc1.render())

with this I am able to create Table in HTML

<div class="body">
  <h1>Survey Report</h1>
</div>
<style>.calendar_table{width:880px;}</style>
<style>body{font-family:Helvetica}</style>
<style>h1{font-size:x-large}</style>
<style>h2{font-size:large}</style>
<style>table{border-collapse:collapse}</style>
<style>th{font-size:small;border:1px solid gray;padding:4px;background-color:#DDD}</style>
<style>td{font-size:small;text-align:center;border:1px solid gray;padding:4px}</style>
<table>
  <thead>
    <th style="color:#ffffff;background-color:#6A75F2">Nominee</th>
    <th style="color:#ffffff;background-color:#6A75F2">counts</th>
  </thead>
  <tbody>
    <tr>
      <td style="font-size:small;text-align:center;padding:4px">Deepesh Ahuja</td>
      <td style="font-size:small;text-align:center;padding:4px">1</td>
    </tr>
    <tr>
      <td style="font-size:small;text-align:center;padding:4px">Sabyasachi Mallick</td>
      <td style="font-size:small;text-align:center;padding:4px">1</td>
    </tr>
    <tr>
      <td style="font-size:small;text-align:center;padding:4px">Raju Singh</td>
      <td style="font-size:small;text-align:center;padding:4px">1</td>
    </tr>
    <tr>
      <td style="font-size:small;text-align:center;padding:4px">Abarna Ravi</td>
      <td style="font-size:small;text-align:center;padding:4px;background-color:#F4D8D2">2</td>
    </tr>
    <tr>
      <td style="font-size:small;text-align:center;padding:4px">
        <b>Grand Total</b>
      </td>
      <td style="font-size:small;text-align:center;padding:4px">
        <b>5</b>
      </td>
    </tr>
  </tbody>
</table><br><br><br>

Now How I will set table class in python code like

<table class='calender_tabe'>

Can someone help me to set class of table and other tag using python dominate library?

1

There are 1 answers

2
Richard W On

Using the example syntax from github's documentation

from dominate.tags import *

testTable = table(border = 1)
print testTable

which will return:

<table border="1"></table> 

with the print statement. However since you can't use the word "class" to refer to the html attribute (class being a python-reserved word) you have to go about it indirectly:

testTable.set_attribute('class','my_class_name')

Adding the above to the original instance of testTable will result in:

<table border="1" class="my_class_name"></table>