Package: pyhtml-enhanced
PyHTML EnhancedA library for building HTML documents with a simple and learnable syntax,
inspired by (and similar to)Cenk Altı's PyHTML library, but
with improved documentation and type safety.FeaturesInline documentation and type safety for all tags.Editor support for many common tags (attribute suggestions).A modern and readable codebase.No dependencies.100% test coverageUsage>>>importpyhtmlasp>>>my_website=p.html(...p.head(...p.title("Hello, world!"),...p.script(src="http://example.com/script.js"),...),...p.body(...p.h1("Hello, world!"),...p.p("This is my amazing website!"),...),...)>>>print(str(my_website))<html><head><title>Hello,world!</title><scripttype="text/javascript"src="http://example.com/script.js"></script></head><body><h1>Hello,world!</h1><p>Thisismyamazingwebsite!</p></body></html>Creating elementsEvery HTML tag is represented by aclassthat generates that HTML code. For
example, to create a<br>element, you could use:>>>line_break=p.br()>>>print(str(line_break))<br/>Adding children to elementsAny arguments to a tag are used as a child element to the created HTML element.
For example, to create a heading with the text"My awesome website", you
could use>>>heading=p.h1("My awesome website")>>>print(str(heading))<h1>Myawesomewebsite</h1>Adding attributes to elementsAny keyword arguments to a tag are used as an attribute of the created HTML
element. For example, to create a form submit button, you could use>>>submit_button=p.input(type="submit")>>>print(str(submit_button))<inputtype="submit"/>Adding attributes and childrenIn HTML, attributes are specified within the opening tag. Contrastingly, Python
requires keyword arguments (attributes) to be specified after regular arguments
(children). To maintain similarity to writing regular HTML, you can call an
element in order to add more attributes and children. For example, to create
a link to PyHTML's GitHub page, you could use>>>my_link=p.a(href="https://github.com/COMP1010UNSW/pyhtml-enhanced")("Take a look at the code")>>>print(str(my_link))<ahref="https://github.com/COMP1010UNSW/pyhtml-enhanced">Takealookatthecode</a>HTML commentsYou can add comments to HTML (useful for debugging) by using theCommenttag.>>>comment=p.Comment("This is an HTML comment")>>>print(str(comment))<!--ThisisanHTMLcomment-->Rendering HTMLConverting your PyHTML into HTML is as simple as stringifying it!>>>print(str(p.i("How straightforward!")))<i>Howstraightforward!</i>Custom tagsSince this library includes all modern HTML tags, it is very unlikely that
you'll need to do create a custom tag. However if you really need to, you can
create a class deriving fromTag.>>>classfancytag(p.Tag):......>>>print(fancytag())<fancytag></fancytag>Refer to the documentation forTagfor more information.Differences to PyHTMLThere are some minor usage differences compared to the original PyHTML library.Uninstantiated classes are only rendered if they are given as the child of an
instantiated element.>>>p.br<class'pyhtml.__tags.generated.br'>>>>print(str(p.html(p.body(p.br))))<html><body><br/></body></html>Calling an instance of aTagwill return a new tag containing all elements of
the original tag combined with the new attributes and children, but will not
modify the original instance, as I found the old behaviour confusing and
bug-prone.>>>para=p.p("Base paragraph")>>>para2=para("Extra text")>>>para2<p>BaseparagraphExtratext</p>>>>para<p>Baseparagraph</p>Known issuesThere are a couple of things I haven't gotten round to sorting out yetAdd default attributes to more tagsSome tags (eg<pre>,<script>) currently aren't properly implemented
and escape their contents.How it worksSince there are so many HTML tags, it would be extremely tedious to document
them all manually. In themetadirectory, you will find code that solves this
problem with the following steps:Download the Markdown source forMDN's documentation of all HTML elements.Parse the markdown to gather all tag names and descriptions, discarding
garbage data and obsolete tags.Use data from a YAML configuration file (meta/tags.yml)
to gather information on suggested attributes and base classes to use for
each tag.Generate Python code to represent all of these tags, including their
documentation.CreditsCenkalti/PyHTMLCenk Altı's work was used as a source of inspiration and reference. Although
all the code inpyhtml-enhancedwas written by me, I want to thank them for
the significant help their hard work provided while creating this project.MDN Web DocsAlmost all of the documentation was gathered from the MDN Web Docs. It's super
neat that all their documentation is open (licensed asCC-BY-SA-2.5if you're
interested).LicenseSource codeCopyright (c) 2023 Miguel Guthridge, COMP1010 UNSWSource code for the library is open source, using theMIT license. A copy of the license
text is available inLICENSE.mdDocumentationDocumentation is copied from MDN Web Docs, and is license underCC-BY-SA-2.5. A copy of the
license text is available inLICENSE_DOCS.md