A single package to serve all your dynamic menu-ing needs with a simple Pythonic interface.
sudo apt install fzf rofi -y
From PyPi
pip3 install pydymenu
# upgrade to newest release (if previously installed)
pip3 install -U pydymenu
To see each finder in action and get a sample code snippet to get you started try the command-line interface.
python3 -m pydymenu --fzf
python3 -m pydymenu --rofi
To begin, use fzf
to pick an item from a list.
import pydymenu
with_a_list = ["apples", "grapes", "bananas", "pears", "strawberries"]
choice = pydymenu.fzf(with_a_list, prompt="Which fruit? ")
print(f"Enjoy your {choice[0]}")
# if you wanted to use rofi instead
choice = pydymenu.rofi(with_a_list, prompt="Which fruit? ")
My favorite way to use this tool is with a dictionary of values. Python makes it easy to select from a list of human-readable names but retrieve programming related values like lambda, lists, or dictionaries, or even your own custom objects!
import pydymenu
from_dict = {
"Coder": {"First": "Mikey", "Last": "Garcia", "alignment": "chaotic good"},
"square()": lambda x: x*x,
"List of fruits": ["apples", "grapes", "bananas", "pears", "strawberries"],
"rofi selector": pydymenu.rofi,
"fzf selector": pydymenu.fzf,
}
choice = pydymenu.fzf(from_dict, prompt="Which option? ")
if choice:
value = from_dict[choice[0]]
print(f"{choice[0]}: {value}")
else:
print("No selection made.")
items: Iterable[str]
(required)
: The only required argument is an iterable of objects that can be cast to str
: If items
is a generator each option will display as it is yielded
prompt: str
: The prompt text shown at the selection (default: >
)
multi: bool
: Whether or not to allow multiple selections. (default: multi=False
)
case_sensitive: bool
: Whether or not to use case sensitive search (default: case_sensitive=False
)
preview: str
(fzf only)
: Command that will be run on each entry and displayed as it's preview when
using the fuzzy finder. Read more in the fzf documentation
As soon as a selection is made the finder closes and returns the result as a
list[str]
. If no selection is made and/or the selection is cancelled None
is
returned. This return signature allows for the following nice pythonic use case.
import pydymenu
selection = pydymenu.fzf(items)
if selection:
print(selection[0])
else:
print('No selection made.')
To get started with development:
- clone the repository
- install development dependencies
source ./tools.sh
See my Super Python Project Template on GitHub to learn more about the automated features of this development environment. Clone the repo to get your own Python projects up and running quickly!
This project is available on GitHub and GitLab. Each push to
master
automatically goes to both so choose whichever platform you prefer. All
releases are uploaded to PyPi.
Big thanks to fzf and Rofi developers for making the utilities this tool relies upon.