Learn PyHTML
This page contains the basic information on how to use PyHTML. In order to properly understand this, it will help to understand the basics of HTML.
Overview
You can think of PyHTML as a collection of functions that you can use to
generate a website. After you finish creating your PyHTML object, you render it
by passing it to the Python str
function.
Importing PyHTML
This the standard way to import the PyHTML library:
All of the PyHTML tags can then be accessed within an object named p
.
Creating elements
Every HTML tag is represented by a class
that generates that HTML code. For
example, to create a <br>
element, you could use:
Any standard HTML tag can be used within PyHTML.
Adding children to elements
Any 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
Adding attributes to elements
Any 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
Adding attributes and children
In 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))
<a href="https://github.com/COMP1010UNSW/pyhtml-enhanced">
Take a look at the code
</a>
Rendering HTML
Converting your PyHTML into HTML is as simple as stringifying it!
Where to from here?
- Learn about advanced PyHTML usage.
- Check out the PyHTML cheatsheet.