Add <style> element to an HTML using Python Dominate library

4.3k views Asked by At

I'm trying to generate an HTML with the dominate package in Python. For the format, I have to add a short CSS content into the <style> tag. However, the only documentation I can find is the GitHub page and that page doesn't have any function related to adding the <style>.

PS: Here's an image

enter image description here

1

There are 1 answers

2
SteveJ On BEST ANSWER

Is this what you are after? from here

import dominate
from dominate.tags import link, script, style

doc = dominate.document(title='Dominate your HTML')

with doc.head:
    link(rel='stylesheet', href='style.css')
    script(type='text/javascript', src='script.js')
    style("""\
         body {
             background-color: #F9F8F1;
             color: #2C232A;
             font-family: sans-serif;
             font-size: 2.6em;
             margin: 3em 1em;
         }

     """)

print(doc.render(pretty=True))

It yields;

<!DOCTYPE html>
<html>
  <head>
    <title>Dominate your HTML</title>
    <link href="style.css" rel="stylesheet">
    <script src="script.js" type="text/javascript"></script>
    <style>         body {
             background-color: #F9F8F1;
             color: #2C232A;
             font-family: sans-serif;
             font-size: 2.6em;
             margin: 3em 1em;
         }

     </style>
  </head>
  <body></body>
</html>