Skip to main content

Pick an option in the terminal with a simple GUI

Project description

https://github.com/wong2/pick/actions/workflows/ci.yml/badge.svg PyPI PyPI

pick is a small python library to help you create curses based interactive selection list in the terminal.

Demo

Installation

$ pip install pick

Usage

pick comes with a simple api:

>>> from pick import pick

>>> title = 'Please choose your favorite programming language: '
>>> options = ['Java', 'JavaScript', 'Python', 'PHP', 'C++', 'Erlang', 'Haskell']
>>> option, index = pick(options, title)
>>> print(option)
>>> print(index)

outputs:

>>> C++
>>> 4

pick multiselect example:

>>> from pick import pick

>>> title = 'Please choose your favorite programming language (press SPACE to mark, ENTER to continue): '
>>> options = ['Java', 'JavaScript', 'Python', 'PHP', 'C++', 'Erlang', 'Haskell']
>>> selected = pick(options, title, multiselect=True, min_selection_count=1)
>>> print(selected)

outputs:

>>> [('Java', 0), ('C++', 4)]

Options

  • options: a list of options to choose from

  • title: (optional) a title above options list

  • indicator: (optional) custom the selection indicator, defaults to *

  • default_index: (optional) set this if the default selected option is not the first one

  • multiselect: (optional), if set to True its possible to select multiple items by hitting SPACE

  • min_selection_count: (optional) for multi select feature to dictate a minimum of selected items before continuing

  • options_map_func: (optional) a mapping function to pass each option through before displaying

Register custom handlers

Sometimes you may need to register custom handlers for specific keyboard keys, you can use the register_custom_handler API:

>>> from pick import Picker
>>> title, options = 'Title', ['Option1', 'Option2']
>>> picker = Picker(options, title)
>>> def go_back(picker):
...     return None, -1
>>> picker.register_custom_handler(ord('h'),  go_back)
>>> option, index = picker.start()
  • the custom handler will be called with the picker instance as it’s parameter.

  • the custom handler should either return a two element tuple, or None.

  • if None is returned, the picker would continue to run, otherwise the picker will stop and return the tuple.

Options Map Function

If your options are not in a format that you want displayed (such as a dictionary), you can pass in a mapping function which each option will be run through. The return value of the function will be displayed.

  • the selected option returned will be the original value and not the displayed return result from the options_map_func function.

pick options map function example:

>>> from pick import pick

>>> title = 'Please choose an option: '
>>> options = [{'label': 'option1'}, {'label': 'option2'}, {'label': 'option3'}]

>>> def get_label(option): return option.get('label')

>>> selected = pick(options, title, indicator='*', options_map_func=get_label)
>>> print(selected)

displays:

Please choose an option:

* option1
  option2
  option3

outputs:

>>> ({ 'label': 'option1' }, 0)

Community Projects

pickpack: A fork of pick to select tree data.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pick-1.5.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

pick-1.5.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pick-1.5.0.tar.gz.

File metadata

  • Download URL: pick-1.5.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.0 Darwin/21.5.0

File hashes

Hashes for pick-1.5.0.tar.gz
Algorithm Hash digest
SHA256 2ec16d30c7f2f26391b99b42f214d6823623176fdbb5a5927b90997d5d6c3115
MD5 90186ccd42ea3d6abba401f0ae6ebd66
BLAKE2b-256 fb954814acf305fb6f33e17480f97e9b943afe9ca559165db3164452239c3db2

See more details on using hashes here.

File details

Details for the file pick-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: pick-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.0 Darwin/21.5.0

File hashes

Hashes for pick-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 20fdf5768fd5e60e866094321a0fc15337ec42724c575a4c762a84ac4792ccd3
MD5 85a65f2ac11bcac173d2be23a2c9ee50
BLAKE2b-256 1d75375deeeb4a1d1756cda5c5ede63f40b791af5a30957fe9d28a3f36f73bae

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page