Skip to main content

A easy-to-use console-based menu.

Project description

menyou

An easy-to-use console-based menu module.

Requirements

  • Python >= 3.6
  • pyfiglet

Installation

pip install menyou

Basic Usage

import menyou as m

# Create a submenu with a couple of options (Opshins)
sub_menu = m.Menyou(
    'My Great App',
    'Sub Menu',
    [m.Opshin(name='Hello World', payload=print),
     # This option will be displayed, but disabled
     m.Opshin(name='Five times x', payload=lambda x: x*5, disabled=True)],
    'Pick a function to execute: '
)

# Create the main menu
main_menu = m.Menyou(
    'My Great App',
    'Main Menu',
    [m.Opshin(name='Sub Menu', payload=sub_menu),
    # For convenience, the Menyou class has an cross-platform exit function
    m.Opshin(name='Exit', payload=m.Menyou.exit)],
    'Pick a submenu to navigate to: '
)

# Display the menu
main_menu.display_menu()

Executing Complex Payloads

The simplest approach would be to have a your program handle interaction with the user. You can define a parameterless function that captures user input and calls any business logic functions defined in your program. For example:

# In your program, setup a parameterless function that handles user input, validation, and calls any necessary functions
def called_by_menu():
    name = my_app.prompt_for_name()
    my_app.greet(name)
    age = my_app.prompt_for_age()
    old_enough = my_app.validate_age(age)

    if old_enough:
        print(f'Congrats{name}, you are old enough.')
    else:
        print(f'Sorry, {name}, you are not old enough')

    # Program control returns to Menyou

# Instantiate the menu
import menyou as m

my_menu = m.Menyou(
    'Menu Title',
    'Menu Subtitle',
    [m.Opshin(name='Interact with my_app', payload=called_by_menu)],
    'Please choose an option: '
)

# Display
my_menu.display_menu()

When you design your program, you may want to keep all the functions that take user input within the Menyou class.

If you want to operate this way you will need to edit the payload_director function in menyou.py. You may also want to add some custom functions to the Menyou class. Here is an example of how you could prompt the user for their name before calling the greet function:

# Assume you have a function in your program that greets the user with the name they provide
def greet(name):
    print(f'Hello {name}')

# Instantiate a menu with the greet function as a payload
import menyou as m

my_menu = m.menyou('My Menu Title', 
                   'My Menu Subtitle',
                   [m.Opshin(name='Greet me', payload=greet)],
                   'Please choose an option: ')

# Add the prompt_for_name function to the Menyou class
...
@staticmethod
def prompt_for_name():
    name = input('What is your name?')
    return name

# Add the logic to the payload_director function so that your_app's greet function gets supplied the correct parameter.
...
def payload_director(fn):
        def inner():
            # Your 'complex' menu operations go here...
            if fn.__name__ == your_app.greet.__name__:
                # call the prompt function you added to the Menyou class:
                name = self.prompt_for_name()
                # supply the return value to the payload function:
                fn(name)  
            else:
                # Default behaviour - execute the payload
                fn()
        return inner

Alternatively, your app could provide the functions to execute:

# Setup a menu
import menyou as m

my_menu = m.menyou('My Menu Title', 
                   'My Menu Subtitle',
                   [m.Opshin(name='Execute three functions', payload=called_by_menu)],
                   'Please choose an option: ')

# In your app:
def called_by_menu():
    # return a tuple of functions to execute
    return func1, func2, func3

# In the payload_director function of the Menyou class
def payload_director(fn):
        def inner():
            # Your 'complex' menu operations go here...
            if fn.__name__ == your_app.called_by_menu.__name__:
                # call the payload that returns three functions:
                func1, func2, func3 = fn()
                # execute them 
                func1()
                func2()
                func3()
            else:
                # Default behaviour - execute the payload
                fn()
        return inner

License

GNU GPLv3

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

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

Source Distribution

menyou-1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

menyou-1.0-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file menyou-1.0.tar.gz.

File metadata

  • Download URL: menyou-1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.8

File hashes

Hashes for menyou-1.0.tar.gz
Algorithm Hash digest
SHA256 8176bc39ce165cb34d90c60208cd1f0b8bbaa52457c796802c1d06834507914a
MD5 be70a3b56d8b3126b563d7b8e212e5b1
BLAKE2b-256 c8f9a9fe5887677a1cdc2bf9fbe4a03f6bb55e1e3f954d2ff140db6dd0348f99

See more details on using hashes here.

File details

Details for the file menyou-1.0-py3-none-any.whl.

File metadata

  • Download URL: menyou-1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.8

File hashes

Hashes for menyou-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 644107963e55158a0f080a8a2fcc57702333114dc3ff0af75044c6bb41ecbd83
MD5 d2d742323f7e19cbe2f533f4b252b6a4
BLAKE2b-256 45f037d3a444d58047fddb2deb4cce5d8422532b4286fca54023da0179d20ad5

See more details on using hashes here.

Supported by

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