pythonpython-3.xinquirer

How to change Python inquirer checkbox's select / unselect symbol from X and o to Y and N respectively?


The sample script:

import os
import sys
from pprint import pprint
import yaml

sys.path.append(os.path.realpath("."))
import inquirer  # noqa

questions = [
    inquirer.Checkbox(
        "interests",
        message="What are you interested in?",
        choices=["Computers", "Books", "Science", "Nature", "Fantasy", "History"],
        default=["Computers", "Books"],
    ),
]

answers = inquirer.prompt(questions)

pprint(answers)
print(yaml.dump(answers))

produces:

[?] What are you interested in?: 
   X Computers
   o Books

How do I change "X" and "o" to "Y" and "N" respectively?

PS: It's pretty common knowledge that XOXO means "hugs and kisses", so it may not be appropriate in some working environments.


Solution

  • You have to define Your new theme and pass it as a parameter to inquirer.prompt.

    Here is modified code changing "X" to "Y" and "o" to "N":

    import os
    import sys
    from pprint import pprint
    
    import yaml
    from inquirer.themes import Default
    
    sys.path.append(os.path.realpath("."))
    import inquirer  # noqa
    
    questions = [
        inquirer.Checkbox(
            "interests",
            message="What are you interested in?",
            choices=["Computers", "Books", "Science", "Nature", "Fantasy", "History"],
            default=["Computers", "Books"],
        ),
    ]
    
    
    class WorkplaceFriendlyTheme(Default):
        """Custom theme replacing X with Y and o with N"""
    
        def __init__(self):
            super().__init__()
            self.Checkbox.selected_icon = "Y"
            self.Checkbox.unselected_icon = "N"
    
    
    answers = inquirer.prompt(questions, theme=WorkplaceFriendlyTheme())
    
    pprint(answers)
    print(yaml.dump(answers))