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

Swapped pyinquirer with questionary #799

Merged
merged 18 commits into from
Dec 2, 2020
Merged

Swapped pyinquirer with questionary #799

merged 18 commits into from
Dec 2, 2020

Conversation

KevinMenden
Copy link
Contributor

@KevinMenden KevinMenden commented Nov 26, 2020

PR checklist

  • This comment contains a description of changes (with reason)
  • CHANGELOG.md is updated
  • If you've fixed a bug or added code that should be tested, add tests!
  • Documentation in docs is updated

To resolve #726 , swapped PyInquirer with questionary in launch.py and setup.py

Installed locally and nf-core launch seemed to behave as expected.

@codecov
Copy link

codecov bot commented Nov 26, 2020

Codecov Report

Merging #799 (b0b407f) into dev (3e0895e) will decrease coverage by 0.09%.
The diff coverage is 32.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev     #799      +/-   ##
==========================================
- Coverage   82.97%   82.88%   -0.10%     
==========================================
  Files          13       13              
  Lines        2726     2717       -9     
==========================================
- Hits         2262     2252      -10     
- Misses        464      465       +1     
Impacted Files Coverage Δ
nf_core/launch.py 70.38% <32.00%> (-0.57%) ⬇️
nf_core/modules.py 88.11% <0.00%> (-1.00%) ⬇️
nf_core/list.py 80.00% <0.00%> (-0.46%) ⬇️
nf_core/utils.py 91.42% <0.00%> (+0.06%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 298376d...b0b407f. Read the comment docs.

Copy link
Member

@ewels ewels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this really was an easy switch! Hardly any code at all. Kudos @KevinMenden for actually sitting down and doing this - great work!

  1. Love that the currently selected option in a Select list has a highlighted background!
  2. I notice that we're still shuffling the default option to the top of the list. Does Questionary support creating a list with the pointer at a given default instead of always being on the first option? (Sounds like no?)
  3. Similar: we're still using the if answer == {}: raise KeyboardInterrupt hack to capture ctrl-c events. Can Questionary allow KeyboardInterrupt events to bubbled to our code without capturing them first? If so could remove this hack. (Sounds like yes?)
  4. Something I could never do with PyInquirer: Is it possible to show one thing in a Select list but return a different value?
    • What I originally wanted to do was to show the default / filled in value for each option in a group. For example: --my_param [foo]. But this broke PyInquirer, as the select list would only take value so had to have just --my_param to work.
    • Would be awesome if we could show the given values for each option in the group list view, bonus points by colouring if different to the schema default.
  5. Select dropdown prompt - can we customise this to get the nice orange arrow back? 🤓 (not a dealbreaker)

Phew, finally managed to break it.. I was starting to get worried. Running nf-core launch atacseq --show-hidden and navigate through to --publish_dir_mode. This option uses enum to limit the allowed return values. Typing in random jibberish gave the following traceback:

/opt/miniconda3/envs/py3.8/lib/python3.8/site-packages/questionary/prompts/common.py:448 in create_inquirer_layout                     │
│                                                                                                                                        │
│   445 ) -> Layout:                                                                                                                     │
│   446"""Create a layout combining question and inquirer selection."""                                                             │
│   447 │                                                                                                                                │
│ ❱ 448ps = PromptSession(get_prompt_tokens, reserve_space_for_menu=0, **kwargs)                                                    │
│   449_fix_unecessary_blank_lines(ps)                                                                                              │
│   450 │                                                                                                                                │
│   451validation_prompt = PromptSession(bottom_toolbar=lambda: ic.error_message, **kwargs)
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
TypeError: __init__() got an unexpected keyword argument 'validate'

nf_core/launch.py Outdated Show resolved Hide resolved
Copy link
Member

@ewels ewels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of super minor things

nf_core/launch.py Outdated Show resolved Hide resolved
nf_core/launch.py Outdated Show resolved Hide resolved
Co-authored-by: Phil Ewels <[email protected]>
@KevinMenden
Copy link
Contributor Author

Thanks for the review Phil and sorry for the typos!

And especially thanks for breaking it :D I'll have a look into that :)

Yes it was hardly any brainwork - really simply swapping the two. Probably possible because the questionary code was based on PyInyquirer to some extent, that's why they are so similar.

  1. No I think specifying a default is not possible currently ... I'll have another look.

  2. Actually yes, looking at the code it seems like they are supported. I will remove our workaround and keyboard-interrupt it - let's see if it works 👍

  3. I see, so you want to have e.g. '--single_end (False)' or something similar to that. That would be quite nice I agree .... will have a look into that as well :)

  4. Haha, okay I'll try to get the orange cursor back 😁

@KevinMenden
Copy link
Contributor Author

Good news, there was a PR 2 months back that introduced the default option in questionary. So we can just comment-out the workaround code and default options are automatically selected.

Questionary also 'catches' keyboard interrupts, but instead of exiting it just returns None, so I fear we'll probably have to stick to the workaround there ....

Bad news is, the error you encountered is caused because in questionary, listor choice questions don't have the validateparameter anymore - for some reason. It's still there for other questions like text, that's why it works for those and we can keep it. Will see if there's another way including the validation in list choices.

@KevinMenden
Copy link
Contributor Author

unsafe_prompt doesn't catch keyboard interrupts, which basically means it behaves as we want it - exiting the program when interrupted. So this should fix this workaround :)

@KevinMenden
Copy link
Contributor Author

Okay so for now I have commented all things related to the validator for 'enum' objects --> I was wondering whether we actually need validation of the input there? Because it is a selection from a list, the user shouldn't have the option to pass wrong inputs - or am I missing something?

Tried to monkey-patch your arrow back in, but it didn't work somehow :/ but at least now it's orange again!
There's also a list for the style at the beginning if you want to adapt that a bit ;)

Didn't find any specific feature that would allow to do what you suggest in point 4. So we'd have to code it ourselves I guess, if that's even possible.

Copy link
Member

@ewels ewels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick code review on my phone, will try to test again when I get a chance.

This is looking brilliant - it makes me very happy to see my hacks and workarounds being removed! 😄

.gitignore Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
nf_core/launch.py Outdated Show resolved Hide resolved
nf_core/launch.py Outdated Show resolved Hide resolved
setup.py Outdated Show resolved Hide resolved
* Use ANSI colours instead of hex values, so that colours honour the terminal settings (and match the nf-core logo)
* Show the value for each parameter in the select list - either the default from the schema or the entered value
@ewels
Copy link
Member

ewels commented Dec 2, 2020

Something I could never do with PyInquirer: Is it possible to show one thing in a Select list but return a different value?

Turns out that you can do this with Questionary! It accepts both title and value (see code), which can be different.

I pushed a change to add this above, so now we get either the entered value, or the schema default in square brackets in the list view:

? Nextflow command-line flags  (Use arrow keys)
 » Continue >>
   ---------------
   -name
   -profile
   -work-dir  [./work]
   -resume  [False]
? Input/output options  (Use arrow keys)
 » Continue >>
   ---------------
   input
   single_end  [False]
   fragment_size  [0]
   seq_center
   outdir  [./results]
   email  [[email protected]]

This is super cool! Makes the interface a lot more usable as you can see at a glance that -resume is False by default and so you probably want to change it. Before this, you'd have to go through each option to find out what the current value was.

I'd love to have the default values printed in a different colour, but I couldn't figure out how to do that and have run out of time.

@ewels
Copy link
Member

ewels commented Dec 2, 2020

Also, I changed the colours - I hope that's ok! Instead of absolute hex codes, I switched to ANSI colour codes. This means that the colours (a) should exactly match the nf-core ANSI logo, which also uses the same codes, and (b) will change according to the colour scheme that people have set on their terminal.

Copy link
Member

@ewels ewels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great now! If you're happy with my changes then please merge 🚀

@KevinMenden
Copy link
Contributor Author

Awesome, I completely oversaw that. That's indeed pretty neat :-)
Thanks!!

@KevinMenden KevinMenden merged commit ae2d7e7 into nf-core:dev Dec 2, 2020
@ewels
Copy link
Member

ewels commented Dec 2, 2020

I'm not sure that it was actually documented it anywhere? I only found it when digging around in the questionary code base..

@KevinMenden
Copy link
Contributor Author

Yes same for the other things - they could certainly use a better documentation ... but I understand that it's not what developers like to do :)
There's even an open issue for it
tmbo/questionary#47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants