Prompts

Prompt class

Class for creating prompts to be used in a survey.

class edsl.prompts.Prompt.Prompt(text=None)

Bases: PromptBase

A prompt to be used in a survey.

component_type = 'generic'
class edsl.prompts.Prompt.PromptBase(text=None)

Bases: PersistenceMixin, RichPrintingMixin, ABC

Class for creating a prompt to be used in a survey.

component_type = 'generic'
classmethod example()

Return an example of the prompt.

classmethod from_dict(data)

Create a Prompt from a dictionary.

Example: >>> p = Prompt(“Hello, {{person}}”) >>> p2 = Prompt.from_dict(p.to_dict()) >>> p2 Prompt(text=’Hello, {{person}}’)

property has_variables: bool

Return True if the prompt has variables.

Example: >>> p = Prompt(“Hello, {{person}}”) >>> p.has_variables True >>> p = Prompt(“Hello, person”) >>> p.has_variables False

render(primary_replacement: dict, **additional_replacements) str

Render the prompt with the replacements.

>>> p = Prompt("Hello, {{person}}")
>>> p.render({"person": "John"})
'Hello, John'
>>> p.render({"person": "Mr. {{last_name}}", "last_name": "Horton"})
'Hello, Mr. Horton'
>>> p.render({"person": "Mr. {{last_name}}", "last_name": "Ho{{letter}}ton"}, max_nesting = 1)
'Hello, Mr. Horton'
rich_print()

Display an object as a table.

template_variables() list[str]

Return the the variables in the template.

Example: >>> p = Prompt(“Hello, {{person}}”) >>> p.template_variables() [‘person’]

property text

Return the Prompt text.

to_dict()

Return the Prompt as a dictionary.

Example: >>> p = Prompt(“Hello, {{person}}”) >>> p.to_dict() {‘text’: ‘Hello, {{person}}’, ‘class_name’: ‘Prompt’}

undefined_template_variables(replacement_dict: dict)

Return the variables in the template that are not in the replacement_dict.

Parameters:

replacement_dict – A dictionary of replacements to populate the template.

Example: >>> p = Prompt(“Hello, {{person}}”) >>> p.undefined_template_variables({“person”: “John”}) [] >>> p = Prompt(“Hello, {{title}} {{person}}”) >>> p.undefined_template_variables({“person”: “John”}) [‘title’]

unused_traits(traits: dict)

Return the traits that are not used in the template.

QuestionInstructionBase class