Module reahl.web.ui

Basic Widgets and related user interface elements.

Widgets for constructing a page

HTML5Page

class reahl.web.ui.HTML5Page(view, title='$current_title', css_id=None)

A web page that may be used as the page of a web application. It ensures that everything needed by the framework (linked CSS and JavaScript, etc) is available on such a page.

Styling

Renders as an HTML5 page with customised <head> and an empty <body>.

Parameters:

Changed in version 4.0: Removed style= keyword

head = None

The Head HTMLElement of this page

body = None

The Body HTMLElement of this page

render()

Returns an HTML representation of this Widget. (Not for general use, may be useful for testing.)

Slot

class reahl.web.ui.Slot(view, name)

A Slot is a placeholder area on a page into which different Views can plug different Widgets on the fly.

Using Slots, one can create a single HTML5page which can be re-used by different Views instead of having to create similar-looking HTML5Pages for each View in an application.

A Slot is added to an HTML5Page to represent an area that will be populated later by a View. When a View is served up the framework can then populate this empty area with Widgets specific to the current View.

Parameters:
  • view – (See reahl.web.fw.Widget)
  • name – A name for this Slot (must be unique on the page)

RunningOnBadge

class reahl.web.ui.RunningOnBadge(view)

A visual badge proclaiming that a website is “Running on Reahl”. Using it on your site is a bit of advertising for Reahl!

Styling

Renders as an HTML <a class=”runningon”> containing an <img>

Parameters:view – (See reahl.web.fw.Widget)

LiteralHTML

class reahl.web.ui.LiteralHTML(view, contents, transform=<function LiteralHTML.<lambda>>)

The LiteralHTML Widget renders a chunk of HTML as given in contents. If a single-argument callable is given as transform, contents will first be passed to that callable to possibly change the HTML on-the-fly before rendering (the callable should return the changed HTML to be rendered).

Parameters:
  • view – (See reahl.web.fw.Widget)
  • contents – Some literal HTML.
  • transform – If given, it is called passing contents to possibly transform contents before rendering. The callable should return the final HTML to be rendered.
render()

Returns an HTML representation of this Widget. (Not for general use, may be useful for testing.)

Very low-level Widgets that correspond to HTML

HTMLElement

class reahl.web.ui.HTMLElement(view, tag_name, children_allowed=False, css_id=None, read_check=None, write_check=None)

A Widget that is represented by an HTML element.

Parameters:
  • view – (See reahl.web.fw.Widget)
  • tag_name – The element name used to render tags for this HTMLElement
  • children_allowed – Elements that are not allowed to have children are rendered only with opening tags, others have an opening and closing tag. (See HTML5 void elements.)
  • css_id – If specified, the HTMLElement will have an id attribute set to this. Mandatory when a Widget has query_fields().
  • read_check – (See reahl.web.fw.Widget)
  • write_check – (See reahl.web.fw.Widget)
enable_refresh(*for_fields)

Sets this HTMLElement up so that it will refresh itself without reloading its page when it senses that one of the given query_fields have changed. If no Fields are specified here, the HTMLElement is refreshed when any of its query_fields change.

Parameters:for_fields – A selection of the .query_fields defined on this HTMLElement.

Changed in version 3.2: The for_fields arguments were added to allow control over which of an HTMLElement’s query_fields trigger a refresh.

add_child(child)

Adds another Widget (child) as a child Widget of this one.

Returns:the added child for convenience.
add_to_attribute(name, values)

Ensures that the value of the attribute name of this HTMLElement includes the words listed in values (a list of strings).

set_attribute(name, value)

Sets the value of the attribute name of this HTMLElement to the string value.

get_attribute(name)

Answers the value of the attribute named name.

has_attribute(name)

Answers whether this HTMLElement has an attribute named name.

attributes

Override this method if you want to change the attributes of this HTMLElement on the fly, based on the state of the HTMLElement at the point in time when it is rendered.

render()

Returns an HTML representation of this Widget. (Not for general use, may be useful for testing.)

contextualise_selector(selector, context)

Returns a JQuery selector for finding selector within the elements matched by context (another selector).

jquery_selector

Returns a string (including its ” delimeters) which can be used to target this HTMLElement using JQuery. By default this uses the id attribute of the HTMLElement, but this property can be overridden to not be dependent on the id attribute of the HTMLElement.

Raises:ProgrammerError – Raised when accessing jquery_selector if the css_id of this HTMLElement is not set.
get_js(context=None)

Override this method if your Widget needs JavaScript to be activated on the browser side.

set_title(title)

A convenience method to set the “title” attribute of this HTMLElement.

set_id(css_id)

A convenience method to set the “id” attribute of this HTMLElement.

append_class(css_class)

A convenience method to add the word in css_class to the “class” attribute of thie HTMLElement.

TextNode

class reahl.web.ui.TextNode(view, value_or_getter, html_escape=True)

An HTML TextNode. You can, for example, use a TextNode to add a snippet of text as a child of a P inbetween its other children (which will all be HTMLElements, like Span).

Parameters:
  • view – (See reahl.web.fw.Widget)
  • value_or_getter – A string which will be used as the text of the TextNode. Pass a no-arg callable instead to delay the computation of what the text of the TextNode should be to the time of rendering the TextNode. The callable will be called right before the TextNode is rendered, and should return the text that should be used in the TextNode.
  • html_escape – If True (default), the given value will first be HTML-escaped before rendered to the browser.
render()

Returns an HTML representation of this Widget. (Not for general use, may be useful for testing.)

Title

class reahl.web.ui.Title(view, text, css_id=None)

The title of an HTML page (the title of a reahl.web.fw.View is usually shown via a Title).

Styling

Rendered as a <title> element.

Parameters:
  • view – (See reahl.web.fw.Widget)
  • text – A string for use in a PEP-292 template. The final value after substituting this string Template will be used as the value of this Title. The template string may use one placeholder: $current_title which contains the title of the current View.
  • css_id – (See reahl.web.ui.HTMLElement)

Body

class reahl.web.ui.Body(view, css_id=None)

The content of an HTML document.

Styling

Renders as an HTML <body> element.

Parameters:
add_child(child)

Adds another Widget (child) as a child Widget of this one.

Returns:the added child for convenience.

Span

class reahl.web.ui.Span(view, text=None, html_escape=True, css_id=None)

A logical grouping of other HTMLElements which fits in with text flow.

Styling

Renders as an HTML <span> element.

Parameters:

More commonly used Widgets that correspond to HTML

A

class reahl.web.ui.A(view, href, description=None, ajax=False, read_check=None, write_check=None, css_id=None)

A hyper link.

Styling

Renders as an HTML <a> element.

Parameters:
classmethod from_bookmark(view, bookmark)

Creates an A for the given bookmark on view.

classmethod factory_from_bookmark(bookmark)

Creates a reahl.web.fw.WidgetFactory for creating an A for the given bookmark.

attributes

Override this method if you want to change the attributes of this HTMLElement on the fly, based on the state of the HTMLElement at the point in time when it is rendered.

get_js(context=None)

Override this method if your Widget needs JavaScript to be activated on the browser side.

set_active(active)

Explicitly sets whether this hyperlink is “active” or not. An active hyperlink cannot be clicked on, because conceptually, the user is already at its target.

Parameters:active – A boolean – send True if active, else False.

H

class reahl.web.ui.H(view, priority, text=None, css_id=None)

The heading for a section.

Styling

Renders as an HTML <h1>, <h2> … , <h6> element, depending on priority.

Parameters:

P

class reahl.web.ui.P(view, text=None, css_id=None, html_escape=True)

A paragraph of text.

Styling

Renders as an HTML <p> element.

Parameters:
  • view – (See reahl.web.fw.Widget)
  • text – The text value displayed in the paragraph (if given)
  • css_id – (See reahl.web.ui.HTMLElement)
  • html_escape – If text is given, by default such text is HTML-escaped. Pass False in here to prevent this from happening.
format(*args, **kwargs)

A complicated paragraph may consist of many TextNodes interspersed with other Widgets. Creating such a paragraph programmatically is cumbersome. Instead, the text of a P can be a template resembling a PEP-292 template. This format method works analogously to string.format(), except that Widgets can be passed in to be substituted into the original P.

Parameters:
  • args – Positional arguments for substituting into the “template P”
  • kwargs – Named arguments for substituting into the “template P”

Div

class reahl.web.ui.Div(view, css_id=None)

A logical grouping of other HTMLElements.

Styling

Renders as an HTML <div> element

Parameters:

Article

class reahl.web.ui.Article(view, css_id=None)

An independent section of informational content.

Styling

Renders as an HTML <article> element.

Parameters:

Li

class reahl.web.ui.Li(view, css_id=None)

A list item.

Styling

Renders as an HTML <li> element.

Parameters:

Ul

class reahl.web.ui.Ul(view, css_id=None)

An unordered list.

Styling

Renders as an HTML <ul> element.

Parameters:

Ol

class reahl.web.ui.Ol(view, css_id=None)

An ordered list.

Styling

Renders as an HTML <ol> element.

Parameters:

Img

class reahl.web.ui.Img(view, src=None, alt=None, css_id=None)

An embedded image.

Styling

Renders as an HTML <img> element.

Parameters:

Widgets for Input

Form

class reahl.web.ui.Form(view, unique_name, rendered_form=None)

A Form is a container for Inputs. Any Input has to belong to a Form. When a user clicks on a Button associated with a Form, the Event to which the Button is linked occurs at the server. The value of every Input that is associated with the Form is sent along with the Event to the server.

Styling

Renders as an HTML <form class=”reahl-form”> element.

Parameters:
  • view – (See reahl.web.fw.Widget)
  • unique_name – A name for this form, unique in the UserInterface where it is used.
attributes

Override this method if you want to change the attributes of this HTMLElement on the fly, based on the state of the HTMLElement at the point in time when it is rendered.

create_error_label(input_widget)

Creates a label containing the current validation error message for the given Input.

exception

The reahl.component.exceptions.DomainException which occurred, if any.

get_js(context=None)

Override this method if your Widget needs JavaScript to be activated on the browser side.

jquery_selector

Returns a string (including its ” delimeters) which can be used to target this HTMLElement using JQuery. By default this uses the id attribute of the HTMLElement, but this property can be overridden to not be dependent on the id attribute of the HTMLElement.

Raises:ProgrammerError – Raised when accessing jquery_selector if the css_id of this HTMLElement is not set.

NestedForm

class reahl.web.ui.NestedForm(view, unique_name)

A NestedForm can create the appearance of one Form being visually contained in another. Forms are not allowed to be children of other Forms but this restriction does not apply to NestedForms.

Styling

Rendered as an HTML <div class=”reahl-nested-form”> element.

Parameters:

FieldSet

class reahl.web.ui.FieldSet(view, legend_text=None, css_id=None)

A visual grouping of HTMLElements inside a Form.

Styling

Rendered as an HTML <fieldset> element.

Parameters:

Legend

class reahl.web.ui.Legend(view, text=None, css_id=None)

A caption for an reahl.web.ui.HTMLElement.

Styling

Rendered as an HTML <legend> element.

Parameters:

Input

class reahl.web.ui.Input(form, bound_field)

A Widget that proxies data between a user and the web application.

Parameters:

Changed in version 4.0: Made .validation_error part of the public API of Input.

validation_error

The failing ValidationConstraint if the current value entered into this Input is invalid, else None.

includes_label

If True, the Label of this Input forms part of the input itself.

TextArea

class reahl.web.ui.TextArea(form, bound_field, rows=None, columns=None)

A muli-line Input for plain text.

Styling

Represented in HTML as a <textarea> element.

Parameters:
  • form – (See Input)
  • bound_field – (See Input)
  • rows – The number of rows that this Input should have.
  • columns – The number of columns that this Input should have.
create_html_widget()

Override this in subclasses to create the HTMLElement that represents this Input in HTML to the user. .. versionadded: 3.2

SelectInput

class reahl.web.ui.SelectInput(form, bound_field, name=None, registers_with_form=True)

An Input that lets the user select an reahl.component.modelinterface.Choice from a dropdown list of valid ones if used with a ChoiceField, or many choices if used with a MultiChoiceField.

Styling

Represented in HTML as a <select> element which can contain <option> and <optgroup> children.

Parameters:
  • form – (See Input)
  • bound_field – (See Input)
create_html_widget()

Override this in subclasses to create the HTMLElement that represents this Input in HTML to the user. .. versionadded: 3.2

get_value_from_input(input_values)

Obtains the value received for this Input from the browser, given a cgi.FieldStorage containing name, value pairs of user input as sent by the browser. Override this method if your Input needs special handling to obtain its value.

RadioButtonSelectInput

class reahl.web.ui.RadioButtonSelectInput(form, bound_field)

An Input that lets the user select an reahl.component.modelinterface.Choice from a list of valid ones shown as radio buttons.

Styling

Represented in HTML as a <div class=”reahl-radio-button-input”> element which contains an <input type=”radio”>, wrapped in a <span class=”reahl-radio-button”> for each valid reahl.component.modelinterface.Choice.

Parameters:
  • form – (See Input)
  • bound_field – (See Input)

Changed in version 4.0: Renamed from RadioButtonInput

create_html_widget()

Override this in subclasses to create the HTMLElement that represents this Input in HTML to the user. .. versionadded: 3.2

get_value_from_input(input_values)

Obtains the value received for this Input from the browser, given a cgi.FieldStorage containing name, value pairs of user input as sent by the browser. Override this method if your Input needs special handling to obtain its value.

TextInput

class reahl.web.ui.TextInput(form, bound_field, fuzzy=False, placeholder=False)

A single line Input for typing plain text.

Styling

Represented in HTML by an <input type=”text” class=”reahl-textinput”> element.

Parameters:
  • form – (See Input)
  • bound_field – (See Input)
  • fuzzy – If True, the typed input will be dealt with as “fuzzy input”. Fuzzy input is when a user is allowed to type almost free-form input for structured types of input, such as a date. The assumption is that the bound_field used should be able to parse such “fuzzy input”. If fuzzy=True, the typed value will be changed on the fly to the system’s interpretation of what the user originally typed as soon as the TextInput looses focus.
  • placeholder – If given a string, placeholder is displayed in the TextInput if the TextInput is empty in order to provide a hint to the user of what may be entered into the TextInput. If given True instead of a string, the label of the TextInput is used.

Changed in version 3.2: Added placeholder.

get_js(context=None)

Override this method if your Widget needs JavaScript to be activated on the browser side.

create_html_widget()

Override this in subclasses to create the HTMLElement that represents this Input in HTML to the user. .. versionadded: 3.2

PasswordInput

class reahl.web.ui.PasswordInput(form, bound_field)

A PasswordInput is a single line text input, but it does not show what the user is typing.

Styling

Represented in HTML by a <input type=”password”> element.

Parameters:
  • form – (See Input)
  • bound_field – (See Input)
create_html_widget()

Override this in subclasses to create the HTMLElement that represents this Input in HTML to the user. .. versionadded: 3.2

CheckboxInput

class reahl.web.ui.CheckboxInput(form, bound_field)

A single checkbox that represent a true or false value.

Styling

Represented in HTML by an <input type=”checkbox”> element.

Parameters:
  • form – (See Input)
  • bound_field – (See Input)
create_html_widget()

Override this in subclasses to create the HTMLElement that represents this Input in HTML to the user. .. versionadded: 3.2

get_value_from_input(input_values)

Obtains the value received for this Input from the browser, given a cgi.FieldStorage containing name, value pairs of user input as sent by the browser. Override this method if your Input needs special handling to obtain its value.

CheckboxSelectInput

class reahl.web.ui.CheckboxSelectInput(form, bound_field)

An Input that lets the user select more than one reahl.component.modelinterface.Choice from a list of valid ones shown as checkboxes.

param form:(See Input)
param bound_field:
 (See Input)

New in version 4.0.

includes_label

If True, the Label of this Input forms part of the input itself.

create_html_widget()

Override this in subclasses to create the HTMLElement that represents this Input in HTML to the user. .. versionadded: 3.2

get_value_from_input(input_values)

Obtains the value received for this Input from the browser, given a cgi.FieldStorage containing name, value pairs of user input as sent by the browser. Override this method if your Input needs special handling to obtain its value.

ButtonInput

class reahl.web.ui.ButtonInput(form, event)
get_value_from_input(input_values)

Obtains the value received for this Input from the browser, given a cgi.FieldStorage containing name, value pairs of user input as sent by the browser. Override this method if your Input needs special handling to obtain its value.

create_html_widget()

Override this in subclasses to create the HTMLElement that represents this Input in HTML to the user. .. versionadded: 3.2

Label

class reahl.web.ui.Label(view, text=None, for_input=None, css_id=None)

A label for an Input.

If for_input is given, the Label will only be visible if for_input is visible.

Styling

Rendered as an HTML <label> element.

Parameters:

Changed in version 3.2: Added the for_input keyword argument.

visible

Answers whether this Widget should be rendered to the current user at all.

attributes

Override this method if you want to change the attributes of this HTMLElement on the fly, based on the state of the HTMLElement at the point in time when it is rendered.

Tables

Table

class reahl.web.ui.Table(view, caption_text=None, summary=None, css_id=None)

An HTML table element: data displayed on columns and rows.

Parameters:
  • view – (See reahl.web.fw.Widget)
  • caption_text – If text is given here, a caption will be added to the table containing the caption text.
  • summary – A textual summary of the contents of the table which is not displayed visually, but may be used by a user agent for accessibility purposes.
  • css_id – (See reahl.web.ui.HTMLElement)
with_data(columns, items)

Populate the table with the given data. Data is arranged into columns as defined by the list of DynamicColumn or StaticColumn instances passed in.

Parameters:
  • columns – The DynamicColumn instances that define the contents of the table.
  • items – A list containing objects represented in each row of the table.

DynamicColumn

class reahl.web.ui.DynamicColumn(make_heading_or_string, make_widget, sort_key=None)

DynamicColumn defines a logical column of a table, specifying how its heading will be rendered, and how the cell in this column will be displayed for each data item in the table.

Parameters:
  • make_heading_or_string – A string to be used as heading for this column, or a single-argument callable that will be called (passing the current view) in order to compute a Widget to be displayed as heading of the column.
  • make_widget – A callable that takes two arguments: the current view, and an item of data of the current table row. It will be called to compute a Widget to be displayed in the current column for the given data item.
  • sort_key – If specified, this value will be passed to sort() for sortable tables.

StaticColumn

class reahl.web.ui.StaticColumn(field, attribute_name, sort_key=None)

StaticColumn defines a column whose heading and contents are derived from the given field.

Parameters:
  • field – The Field that defines the heading for this column, and which will also be used to get the data to be displayed for each row in this column.
  • attribute_name – The name of the attribute to which field should be bound to on each data item when rendering this column.
  • sort_key – If specified, this value will be passed to sort() for sortable tables.

Caption

class reahl.web.ui.Caption(view, text=None, css_id=None)

An HTML caption element.

Styling

Renders as an HTML <caption> element.

Parameters:

Thead

class reahl.web.ui.Thead(view, css_id=None)

An HTML thead element. Contains the header of the table columns.

Parameters:

Tfoot

class reahl.web.ui.Tfoot(view, css_id=None)

An HTML tfoot element. Contains the footer of the table columns.

Parameters:

Tbody

class reahl.web.ui.Tbody(view, css_id=None)

An HTML tbody element. Contains the rows with data in the table.

Parameters:

Tr

class reahl.web.ui.Tr(view, css_id=None)

An HTML tr element represents one row of data in a table.

Parameters:

Td

class reahl.web.ui.Td(view, rowspan=None, colspan=None, css_id=None)

An HTML td element - a single cell of data inside a row of a table.

Parameters:

Th

class reahl.web.ui.Th(view, rowspan=None, colspan=None, css_id=None)

An HTML th element - a single cell heading for a column of a table.

Parameters:

Uploading of files

SimpleFileInput

class reahl.web.ui.SimpleFileInput(form, bound_field)

An Input for selecting a single file which will be uploaded once the user clicks on any Button associated with the same Form as this Input.

Styling

Represented in HTML by an <input type=”file”> element. Can have attribute multiple set, if allowed by the bound_field.

Parameters:
create_html_widget()

Override this in subclasses to create the HTMLElement that represents this Input in HTML to the user. .. versionadded: 3.2

get_value_from_input(input_values)

Obtains the value received for this Input from the browser, given a cgi.FieldStorage containing name, value pairs of user input as sent by the browser. Override this method if your Input needs special handling to obtain its value.