Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A beautiful html builder built with python.

License

NotificationsYou must be signed in to change notification settings

jaimevp54/htmlBuilder

Repository files navigation

HtmlBuilder is a python library that allows you to render HTML files by writing python code. And to make use of python features, clean syntax, and object-oriented design to their full potential. (Full documentation available atreadthedocs.io)

codecovpython version badge

Why should you care about this library?

When rendering HTML programmatically, there are other options available (template engines and other rendering libraries). Still, these are often limited in what they can do, or it's necessary to learn a new level of abstraction before being productive. HtmlBuilder tries to improve on this by following the next few ideas:

  • Minimal learning curve: Users should need no more than Python and HTML knowledge to be productive using this tool.
  • Real python code: The final code looks and behaves as you would expect from other python code.
  • Easily testable: Users can introspect and unit test the HTML object structurebefore rendering the HTML string.

Installation

runpip install htmlBuilder

Use examples:

A simple example

# import necessary tags and attributesfromhtmlBuilder.tagsimport*fromhtmlBuilder.attributesimportClass,StyleasInlineStyle# html tags are represented by classeshtml=Html([],# any tag can receive another tag as constructor parameterHead([],Title([],"A beautiful site")    ),Body([Class('btn btn-success'),InlineStyle(background_color='red',bottom='35px')],Hr(),Div([],Div()        )    ))# no closing tags are required# call the render() method to return tag instances as html textprint(html.render(pretty=True))

Output

<html><head><title>          A beautiful site</title></head><bodyclass='btn btn-success'style='background-color: red; bottom: 35px'><hr/><div><div></div></div></body></html>

A not so simple example

fromhtmlBuilder.attributesimportClassfromhtmlBuilder.tagsimportHtml,Head,Title,Body,Nav,Div,Footer,Ul,Li# declare datausers= [    {"name":"Jose","movies": ['A beautiful mind','Red'],"favorite-number":42,    },    {"name":"Jaime","movies": ['The breakfast club','Fight club'],"favorite-number":7,    },    {"name":"Jhon","movies": ['The room','Yes man'],"favorite-number":987654321,    },]# functions can be used to handle recurring tag structuresdefmy_custom_nav():# these functions can return a tag or a list of tags ( [tag1,tag2,tag3] )returnNav([Class("nav pretty")],Div([],"A beautiful NavBar")    )html=Html([],Head([],Title([],"An awesome site")    ),Body([],my_custom_nav(),# calling previously defined function        [Div([Class(f"user-{user['name'].lower()}")],Div([],user['name']),Ul([],                [Li([],movie)formovieinuser["movies"]]# list comprehensions can be used to easily render multiple tags            )ifuser['favorite-number']<100else"Favorite number is too high"# python's ternary operation is allowed too        )foruserinusers],Footer([],"My Footer"),    ))print(html.render(pretty=True,doctype=True))# pass doctype=True to add a document declaration

Output

<!DOCTYPE html><html><head><title>          An awesome site</title></head><body><navclass='nav pretty'><div>            A beautiful NavBar</div></nav><divclass='user-jose'><div>            Jose</div><ul><li>              A beautiful mind</li><li>              Red</li></ul></div><divclass='user-jaime'><div>            Jaime</div><ul><li>              The breakfast club</li><li>              Fight club</li></ul></div><divclass='user-jhon'><div>            Jhon</div>          Favorite number is too high</div><footer>          My Footer</footer></body></html>

[8]ページ先頭

©2009-2025 Movatter.jp