-
Notifications
You must be signed in to change notification settings - Fork 1
WriteHtml
Using HTMLTags to generate HTML with Python code
A dynamic web application presents data inside an HTML document. The best way to generate HTML is to use the HTMLTags module.
HTMLTags defines a class for each valid HTML tag (including HTML5), in uppercase letters : for instance, there are classes HTML, HEAD, BODY, TABLE, B, I, etc.
These classes are initiated with a value, and optional keyword arguments :
DIV(content,key1#value1,key2value2...)
The str()
method of these classes returns the HTML code matching the tag name and arguments. For HTML attributes without arguments, such as the MULTIPLE argument of SELECT tags, the constructor must be called with the value True
:
SELECT(name#"foo",multipleTrue)
The content argument of a tag can be another instance of an HTMLTags class. For instance :
title = TITLE("FC Barcelona Home Page")
head = HEAD(title)
print(head)
>>> <HEAD><TITLE>FC Barcelona Home Page</TITLE></HEAD>
Tags support addition :
line # TD("email")+TD(INPUT(name"email"))
print(line)
>>> <TD>email</TD><TD><INPUT name="email"></TD>
and multiplication
line = TH(" ")*3
print(line)
>>> <TH> </TH><TH> </TH><TH> </TH>
The operator <= means "add child", you can use it to build complex HTML documents :
body = BODY()
body <= H2("Home page")
table # TABLE(Class"players")
table <= TR(TH("Name")+TH("Birth"))
for name,birth in [("Andres Iniesta","1984-05-11"),
("Lionel Messi","1987-06-24")]:
table <= TR(TD(name)+TD(birth))
body <= table
This tag has additional methods to build it from a list, and to select options
###from_list(items,[
use_content]
)
Builds the OPTION tags inside the SELECT tag, using the list items. Each option matches an element in the list. If use_
content is set to True, the option value for the n-th item is set to items[n
] ; if set to False (default), it is set to n. The method returns the SELECT tag itself
###select(value=v_arg,content=c_arg) Select the options with the specified value or content If v_arg is a list, select the options whose value is in the list. Same thing if content is a list
Examples
Python code | generates HTML code |
---|---|
```python sel = SELECT(name="foo").from_list(["one","two"]) sel.select(value=0) ``` | ```python one two ``` |
```python sel = SELECT(name="foo").from_list(["one","two"]) sel.select(content="two") ``` | ```python one two ``` |
= The RADIO and CHECKBOX classes =
These classes are provided for the HTML INPUT tags of types radio and checkbox
The constructor is
###RADIO(items,[
values,[**
attrs]]
)
It builds a list of INPUT tags, one per item in the list items. Each INPUT tag has the attributes attrs
If values is specified, it must be a list of the same size as items. Each INPUT tag value is taken from values If it is not specified, the value is the index in items
An instance of RADIO is an iterator on a list of tuples (value,tag) where value is the value in items, and tag is the matching INPUT instance
Instances of RADIO and CHECKBOX support the method
###check(value=arg) or check(content=arg)
If the key is value, arg is an index (or a list of indices), the method checks the tag(s) at the given index or indices in items
If key is content, arg is a string (or a list of strings), the method checks the tag(s) whose value is the given string, or in the list of strings
Examples
Python code | generates HTML code |
---|---|
```python r = RADIO(["one","two"],name="foo") r.check(value=0) for v,tag in r: print(v,tag) ``` | ```python one two ``` |
```python r = RADIO(["one","two"],[350,18],name="foo") r.check(content="two") for v,tag in r: print(v,tag) ``` | ```python one two ``` |
= Editing a tag =
A tag can be manipulated in the same way as a DOM element
It supports the dictionary interface to access the attributes. With the example above, you can add an attribute to the table :
table["width"] = "80%"
To get a specific tag inside the document tree, use the method
get(*
tags,**
attrs)
Returns a list of all the tags in the tree below self. tags are tag classes, and attrs are key/values arguments. The method returns the tags of the specified classes that have the specified arguments
In the above example, to get all the TH elements :
ths = body.get(TH)
= Using HTMLTags in Karrigell scripts =
All the names defined by HTMLTags are present in the execution namespace of Karrigell scripts
Suppose you have a script with this function index() :
def index():
return B("Hello world")
When a function is executed, the string representation of the return value is sent to the browser. Here, the return value is an instance of the HTMLTags class B ; its string representation is
<B>Hello world</B>