Checkbox¶
This page will explain how to use the Checkbox cue of the Cues library.
Checkbox objects are useful when you need to give a user a list of options and have them select as many as they would like by using the spacebar. The result is a dict containing a str.
Before we start, make sure you have Cues installed.
Setting up¶
Checkbox objects have three required parameters:
Parameters |
Type |
Optional |
Default |
|---|---|---|---|
name |
str |
No |
|
message |
str |
No |
|
options |
iterable |
No |
The signature for the __init__ method of a Checkbox object:
def __init__(self, name, message, options):
# ...
We first need to start by importing Checkbox from the Cues library:
from cues import Checkbox
Now, we need to instantiate a Checkbox object. We can do this with a little bit of setup by initializing some variables:
name = 'guitars'
message = 'Pick your favorite guitars:'
options = [
'Les Paul',
'Stratocaster',
'Telecaster',
'SG',
'Flying V',
'Acoustic',
'Classical',
]
In the code above, we created the variables name and message:
namewill be used to retrieve the results from aCheckboxobjectmessageis the text that will be displayed to the user
In addition to those, we also created a options variable. This will be used to present a list of options for the user to pick from. This variable can be any iterable that contains str objects.
Now that our setup is complete, we can go ahead and initialize a Checkbox object and ask the user to select none, one, or more of our options by invoking our instance’s send method:
cue = Checkbox(name, message, options)
answer = cue.send()
When you “send” the cue to the user, they will be presented with something that looks like the following:
The Checkbox cue¶
Once the user is done perusing and has selected an option, a dict consisting of the name variable and the option the user chose will be returned. The result will resemble the following:
{'guitars': ['Les Paul', 'Stratocaster', 'Classical']}
Instantiating from a dict¶
In the previous example, we initialized separte variables for the __init__ method of a Checkbox object. However, we could also make use of the class’s from_dict classmethod and instantiate by using a dict instead:
from cues import Checkbox
checkbox_dict = {
'name': 'guitars'
'message': 'Pick your favorite guitars:'
'options': [
'Les Paul',
'Stratocaster',
'Telecaster',
'SG',
'Flying V',
'Acoustic',
'Classical',
]
}
cue = Checkbox.from_dict(checkbox_dict)
answer = cue.send()
The names for the values in this dict must be the same as the names of the parameters in the __init__ method.