Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-selected value in list #57

Closed
lazee opened this issue Sep 14, 2020 · 14 comments · Fixed by #56
Closed

Pre-selected value in list #57

lazee opened this issue Sep 14, 2020 · 14 comments · Fixed by #56
Labels
Duplicate This issue or pull request already exists

Comments

@lazee
Copy link

lazee commented Sep 14, 2020

Hi. I'm sorry if this has already been answered before...And thank you for questionary btw.

Is it possible in a select to have a preselected option? I have found the default-settings but it doesn't behave the way I would expect.

image
Here review is the default, but the marker stays on test. And if the user hits enter he will select test and not review.

In my scenario I will cache the last environment the user worked on and let him hit enter next time, instead of having to select from the list each time.

@kiancross kiancross added the Duplicate This issue or pull request already exists label Sep 15, 2020
@kiancross
Copy link
Collaborator

I think this is a duplicate of #43? If so, I have implemented the behaviour in #56, and it is awaiting review.

The default parameter is actually something slightly different, it defines the value that is returned if no value is selected. However, this doesn't really make sense for a select input (it makes more sense for the checkbox, where a value is not required to continue).

@tmbo Is there a reason the select input accepts a default parameter? Should ctrl + c return the default value instead of none?

@lazee
Copy link
Author

lazee commented Sep 15, 2020

Thank you @kiancross . Indeed it is a duplicate. I just didn't find it. Thanks for your answer.

@tmbo
Copy link
Owner

tmbo commented Sep 21, 2020

Right, yes I think we should point at the default value 👍 I don't think we should return a default value on keyboard interrupt, as it signals the user wants to abort / go back.

I think there are two ways to define the selected value (either specifying a value for default or by marking the choice as selected in the array of choices), I think we should make sure that

  • there isn't more than one choice selected
  • we point to the selected value if there is one (otherwise this is odd, since we return the pointed at value and not the selected one...).

@kiancross
Copy link
Collaborator

@tmbo In #56 I implemented the 'point at' behaviour by adding an initial_value field. Do you want to replace this with the default field, or remove the default field for select (obviously the default parameter does make sense for checkbox and it should remain for this type).

@tmbo
Copy link
Owner

tmbo commented Sep 22, 2020

I think a default value makes the most sense for the select element (and would coincide with the pointed at element in this case). So I guess the least effort would be to adapt your or, use the logic to set the pointed at element but remove the parameter and only use default.

@kiancross
Copy link
Collaborator

@tmbo Do you think it might be confusing that the same functionality is implemented using two differently named arguments (default and initial_choice) for checkbox and select? I think it would be nicer if they shared the same argument name, for consistency?

@kiancross kiancross linked a pull request Sep 22, 2020 that will close this issue
@tmbo
Copy link
Owner

tmbo commented Sep 25, 2020

well, I would say that the behaviour from a users perspective should be consistent for arguments that share the same name. so if we implement it in the way that,

  • default sets the default return value of the prompt AND also points to the set item
  • and initial_choice allows the user to overwrite the above
    this seems pretty consistent. WDYT?

@kiancross
Copy link
Collaborator

kiancross commented Sep 25, 2020

@tmbo I think ideally:

  • we would remove default from the select type (seeing as it doesn't make sense);
  • rename initial_choice to selected (I think this is a better name in hindsight).

This would leave us with:

  • Only a selected argument for select, which indicates the initial pointed to item.
  • Both selected and default on checkbox, where default behaves the same way as it currently does.

However, this may break backwards compatibility for those already using the default parameter on select (probably not many people though, seeing as it doesn't really do anything at the moment).

@yajo
Copy link
Contributor

yajo commented Sep 26, 2020

Breaking backwards compatibility for something broken doesn't seem like a big issue for me... this is more a fix than a new feature. I agree with @tmbo.

@tmbo
Copy link
Owner

tmbo commented Oct 1, 2020

I think there is no right or wrong here, both approaches would work. I'd go for the default argument, as it doesn't break compatibility (I'd also consider the default element not being selected a bug, I don't think there is any use case where this would be the expected behaviour).

@lazee
Copy link
Author

lazee commented Oct 5, 2020

Thank you guys!

@WilliamStam
Copy link

WilliamStam commented Feb 19, 2021

how dos this get used? i think im doing something wrong in my app :(

menu_list = list()
menu_list.append(questionary.Choice(
    title=f"test1 title",
    value="test1"
))
menu_list.append(questionary.Choice(
    title=f"test2 title",
    value="test2"
))

print("values: ",[x.value for x in menu_list])
# ['test1', 'test2']

action = questionary.select(
    "SELECT IT",
    choices=menu_list,
    default="test2"
).ask()

print("choice selected: ",action)

throws an error

ValueError: Invalid `default` value passed. The value (`test2`) does not exist in the set of choices. Please make sure the default value is one of the available choices.

works fine if i remove the default but then it just selects the first item in the list by default

(with me commenting out default="test2" and selecting test2 )

values:  ['test1', 'test2']
? SELECT IT  test2 title
choice selected:  test2

thanks for the awesome library!

@kiancross
Copy link
Collaborator

@WilliamStam Haven't tested your particular example, but the line that says default="test2" should be default=menu_list[1] I think :)

@WilliamStam
Copy link

that does actually work, thank you! seems slightly counter intuitive tho in a way lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate This issue or pull request already exists
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants