Skip to main content

A simple library to handle the menus for your python console applications.

Project description

# Python Console Menu
![license](https://img.shields.io/hexpm/l/plug.svg)

This library provides a way to quickly create the menu for your python console app.

## Overview

### Classes

#### AbstractMenu
This is the abstract class you need to extend in your menus.
It's constructor takes in a title which is displayed at the top of the menu. This should be called from your implementations constructor. Like so:
```pythonstub
class MenuImplementation(AbstractMenu):
def __init__(self):
super().__init__("Menu Title")
```
##### Methods
- `initialise()` this needs overridding in your implementations and is where you add the items to the menu.
- `display()` this starts this menu. This only needs to be called on the root menu in your system, as all sub-menus are handled by this library.
- `add_menu_item(MenuItem(id, description, subMenu or action))` this adds an item to the menu.
- `add_hidden_menu_item(new MenuItem(id, description, subMenu or action))` this is a helper method that adds a menu item, which is then hidden.
- `update_menu_items()` this can be overriden per menu to update items based on changes to your application, such as showing hidden menu items if they're now needed.
- `show_menu_item(id)` this can be used to show hidden menu items, most commonly in the method above. This uses the unique id given to the menu item.
- `hide_menu_item(id)` this can be used to hide menu items.

#### MenuItem
This is the class used to define items for the menus in your system.
It has two constructors one for if the item is a sub menu and another for if its an action.
These should be called like this: `MenuItem(id, description, subMenu or action)`
##### Methods
- `hide()` which is used on menu items, to hide them from the list.
- `show()` which is used on hidden menu items, to show them in the list.
- `set_as_exit_option()` which is used to set menu items as the exit option for a menu, either going to the parent menu, or exiting the application.

## Example
#### Menu initialisation
```pythonstub
mainMenu = MainMenu()
mainMenu.display()
```
#### Main Menu Class
```pythonstub
class MainMenu(AbstractMenu):
def __init__(self):
super().__init__("Welcome to the main menu")

def initialise(self):
self.add_menu_item(MenuItem(100, "Exit menu").set_as_exit_option())
self.add_menu_item(MenuItem(101, "Print Hello World", lambda: print("Hello World!")))
```

#### Output
```text
Welcome to the main menu
0. Exit menu
1. Print Hello World
Select option: 1
Hello World!

Welcome to the main menu
0. Exit menu
1. Print Hello World
Select option: 0

Process finished with exit code 0
```

Look in test.py for a better example implementation of the library.


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

python_console_menu-1.0.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_console_menu-1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_console_menu-1.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.10.0 pkginfo/1.2.1 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.5

File hashes

Hashes for python_console_menu-1.0.tar.gz
Algorithm Hash digest
SHA256 3b168cc0e742f33da1d00ce91413ae52e7a14838347a10ff4cac6a489b6662c6
MD5 e00ce9e28f9e8bd47485776f9df4c717
BLAKE2b-256 a14fe3f5a18ecab6c287a58bf147ae8487d03fc5981a626fb8cb201437cf848a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_console_menu-1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.10.0 pkginfo/1.2.1 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.5

File hashes

Hashes for python_console_menu-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f998eaaeaf87339a917c56c671de5bb8f00a3c347846ad43b9fea578c91bbd27
MD5 3eb9bd9fe75314e84ac28f54e58ff3bf
BLAKE2b-256 dc636289e8400aba488ad401159b0aa00a82303d8b44b6893593055b17ba0cb9

See more details on using hashes here.

Supported by

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