Callback#

class alfred3.element.misc.Callback(func: callable, followup: str = 'refresh', submit_first: bool = True, delay: int = 0, reset_delay: bool = False, custom_js: str = '')[source]#

Bases: alfred3.element.core.Element

Triggers execution of a python function from a loaded page.

Parameters
  • func (callable) – Python function to be called on button click. The function must take zero arguments, but it can be a method of a class or instance.

  • followup (str) –

    What to do after the python function was called. Can take the following values:

    • refresh submits and reloads the current page (default),

    • none does nothing,

    • forward submits the current page and moves forward,

    • backward submits the current page and moves backward,

    • jump>page_name submits the current page and triggers a jump to a page with the name ‘page_name’

    • custom executes custom JavaScript. If you choose this option, you must supply your custom JavaScript through the argument custom_js.

  • submit_first (bool) – If True, the current values of all input elements on the current page will be saved on button click, before func is called. This way, these values will be available in func through ExperimentSession.values, if func has access to the ExperimentSession object. See Example 3. Defaults to True.

  • delay (int) – Number of seconds to wait before the callback is triggered. Defaults to 0.

  • reset_delay (bool) – If True, the delay will start from the beginning every time the page is reloaded, refreshed, or revisited. Defaults to False.

  • custom_js (str) – Custom JavaScript to execute after the python function specified in func was called. Only takes effect, if followup is set to ‘custom’.

Examples

The callback on the first page will trigger after a delay of 10 seconds. It will print the experiment id to the terminal:

import alfred3 as al
exp = al.Experiment()

@exp.member
class Demo(al.Page):

    def on_exp_access(self):
        self += al.Callback(func=self.print_expid, delay=10)

    def print_expid(self):
        print(self.exp.exp_id)

Methods

add_css

Adds CSS to the element.

add_js

Adds Javascript to the element.

added_to_experiment

Tells the element that it was added to an experiment.

added_to_page

Tells the element that it was added to a page.

prepare_web_widget

Hook for computations for preparing an element's web widget.

render_inner_html

Renders the element template element_template.

Attributes

base_template

Base template for the element, which will be used to hold the rendered element template.

converted_width

List of bootstrap column widths at different screen sizes.

css_class_container

Returns the name the element container's CSS class.

css_class_element

Returns the name of the element's CSS class.

css_code

A list of tuples, which contain a priority and CSS code.

css_urls

A list of tuples, which contain a priority and an url pointing to CSS code.

display_standalone

If True (default), the element will be displayed as usual on its own.

element_template

The element's specific, inner template.

element_width

Returns a string of column width definitions.

exp

The experiment session to which this element belongs.

experiment

Alias for exp

followup

Value of the 'followup' setting for this element.

font_size

Font size

js_code

A list of tuples, which contain a priority and Javascript.

js_template

Javascript template

js_urls

A list of tuples, which contain a priority and an url pointing to JavaScript.

name

Unique identifier for the element.

page

The page to which this element belongs.

position

Position of the whole element on the page.

section

The direct parent section of this element's page.

short_tree

String, giving the exact position in the experiment.

should_be_shown

showif

Conditions that have to be met for the element to be shown.

template_data

Dictionary of data to be passed on to jinja templates.

tree

String, giving the exact position in the experiment.

web_widget

width

Element width