API Reference¶
Ui
¶
A factory class for creating UI elements using BeautifulSoup.
Source code in weba/ui.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
get_html_parser()
classmethod
¶
Get the LRU cache size from environment variable.
get_xml_parser()
classmethod
¶
Get the XML parser from environment variable.
raw(html, parser=None, parse_only=None)
¶
Create a Tag from a raw HTML string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
html
|
str | bytes
|
Raw HTML string to parse |
required |
parser
|
str | None
|
Parser to use (defaults to XML or HTML parser based on content) |
None
|
parse_only
|
SoupStrainer | None
|
Optional SoupStrainer to limit parsing to specific tags |
None
|
Returns:
Name | Type | Description |
---|---|---|
Tag |
Tag
|
A new Tag object containing the parsed HTML |
Source code in weba/ui.py
text(html)
¶
Create a raw text node from a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
html
|
str | int | float | Sequence[Any] | None
|
Raw text to insert |
required |
Returns:
Type | Description |
---|---|
str
|
A string containing the text. |
Source code in weba/ui.py
Tag
¶
Bases: Tag
Source code in weba/tag.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
|
__call__(**kwargs)
¶
Support calling a tag with attributes to use with_attrs under the hood.
This allows using html.header(_class="foo") instead of html.header.with_attrs(_class="foo").
Also supports special class operations: - _append_class: Append classes to existing class list - _prepend_class: Prepend classes to existing class list
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Any
|
Keyword arguments to set as attributes on the tag. Use _attr name for Python reserved words (e.g., _class for "class"). |
{}
|
Returns:
Name | Type | Description |
---|---|---|
self |
Self
|
Returns self after applying attributes for context manager use. |
Example
with html.header(_class="header", _append_class="sticky"): ui.h1("Page Title")
Source code in weba/tag.py
__init__(parser=None, builder=None, name=None, namespace=None, prefix=None, attrs=None, parent=None, previous=None, is_xml=None, sourceline=None, sourcepos=None, can_be_empty_element=None, cdata_list_attributes=None, preserve_whitespace_tags=None, interesting_string_types=None, namespaces=None)
¶
Basic constructor.
:param parser: A BeautifulSoup object.
:param builder: A TreeBuilder.
:param name: The name of the tag.
:param namespace: The URI of this Tag's XML namespace, if any.
:param prefix: The prefix for this Tag's XML namespace, if any.
:param attrs: A dictionary of this Tag's attribute values.
:param parent: The PageElement to use as this Tag's parent.
:param previous: The PageElement that was parsed immediately before
this tag.
:param is_xml: If True, this is an XML tag. Otherwise, this is an
HTML tag.
:param sourceline: The line number where this tag was found in its
source document.
:param sourcepos: The character position within sourceline
where this
tag was found.
:param can_be_empty_element: If True, this tag should be
represented as
Source code in weba/tag.py
__iter__()
¶
__setitem__(key, value)
¶
Set an attribute value, handling boolean attributes correctly.
Source code in weba/tag.py
__str__()
¶
Custom string representation that handles boolean attributes correctly.
Source code in weba/tag.py
comment(selector)
¶
Find all tags or text nodes that follow comments matching the given selector.
This method searches for HTML comments containing the selector text and returns the elements that immediately follow those comments. It can return both HTML elements and text nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selector
|
str
|
The comment text to search for (e.g., "#button" or ".card") |
required |
Returns:
Type | Description |
---|---|
Comment
|
A list of Tag or NavigableString objects that immediately follow matching comments. |
Comment
|
For text nodes, returns them as |
Comment
|
Returns an empty list if no matches are found. |
Source code in weba/tag.py
comment_one(selector)
¶
Find the first tag or text node that follows a comment matching the given selector.
This method searches for the first HTML comment containing the selector text and returns the element that immediately follows it. It can return both HTML elements and text nodes. Returns None if no match is found.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selector
|
str
|
The comment text to search for (e.g., "#button" or ".card") |
required |
Returns:
Type | Description |
---|---|
Tag | None
|
A Tag object if the next element is an HTML tag, or a NavigableString if it's a text node. |
Tag | None
|
Returns None if no match is found. |
Source code in weba/tag.py
copy()
¶
decompose()
¶
Decompose this tag and ensure context is cleaned up.
Source code in weba/tag.py
with_attrs(**kwargs)
¶
Apply attributes to the tag and return self for use in a with statement.
This method is useful for adding attributes to a tag before using it in a with statement. It handles Python reserved words by allowing attributes to be prefixed with an underscore.
Special attributes: - _append_class: Append classes to existing class list - _prepend_class: Prepend classes to existing class list
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Any
|
Keyword arguments to set as attributes on the tag. Use _attr name for Python reserved words (e.g., _class for "class"). |
{}
|
Returns:
Name | Type | Description |
---|---|---|
self |
Self
|
Returns self for method chaining and context manager use. |
Example
with tag.with_attrs(class_="container", id="main"): ui.h1("Hello, World!")
Or with Python reserved words:¶
with tag.with_attrs(_class="container", _for="form1"): ui.h1("Hello, World!")
Append classes:¶
with tag.with_attrs(_append_class="text-lg"): ui.h1("Hello, World!")