Skip to main content

A phone-tree like menu system for text-based interfaces

Project description

PhoneTree

PhoneTree is a Python framework for creating text-based menu systems, resembling phone tree systems or rudimentary chatbots. It allows you to easily create menus and actions, manage user input and output, and maintain state between interactions.

Features

  • Simple decorator-based syntax for defining menus and actions
  • Optional "ask" and "tell" callbacks for handling user input and output
  • State management for passing data between menus and actions

Installation

To install PhoneTree, simply use pip:

pip install phonetree

Usage

Here's an example of how to use PhoneTree to create a simple menu system:

import phonetree
from phonetree import Ask, Tell

@phonetree.menu()
def main_menu(state: dict) -> dict:
    """Main menu."""
    return {"interactions": state.get("interactions", 0) + 1}

@main_menu.menu("First Submenu")
def first_submenu(state: dict) -> dict:
    """First Submenu menu."""
    # here goes the code that runs when you enter the submenu
    ...

    return {"interactions": state.get("interactions", 0) + 1}

@first_submenu.action("Do something")
def do_something(state: dict, ask: Ask, tell: Tell) -> dict:
    """Some action"""
    anything = ask("Is there anything you want to say?")
    print("user answered: " + anything)
    tell("Alright! Thank you!")
    return {"interactions": state.get("interactions", 0) + 1}

@first_submenu.action("Do something else")
def do_something_else(state: dict, ask: Ask, tell: Tell) -> dict:
    """Some action"""
    color = ask("What's your favorite color?")
    print("User said " + color + " is their favorite color.")
    tell("Alright! Nice to know!")
    return {"interactions": state.get("interactions", 0) + 1, "favorite_color": color}

@main_menu.menu("Second submenu")
def second_submenu(state: dict, tell: Tell) -> dict:
    """Second submenu."""
    tell("Welcome to second submenu!")
    return {"interactions": state.get("interactions", 0) + 1}

Defining Menus and Actions

To define a menu, simply use the @phonetree.menu() decorator on a function. The function should return a dictionary representing the new state of the menu. This state will be passed on to the next menu or action function call.

To define an action within a menu, use the @menu.action("Action Name") decorator on a function. The function should also return a dictionary representing the new state of the menu.

Handling User Input and Output

Menu and action functions can take optional "ask" and "tell" callbacks.

The "ask" function sends some text to the user and expects an answer, returning the answer as the response of the function call. If the function returns None, the program execution is ended.

The "tell" function just sends some text to the user and doesn't return anything back. Both functions are recognized by their names in the menu/action functions argument list.

State Management

Menu and action functions can also take a state variable, which can be called anything (except for "ask" and "tell"). This argument is optional, but if passed, should be the first argument of the function. This argument can receive a state of any type.

The function should return the new state of the menu, which will determine what will be passed on as the state for the next menu/action function call. This state can be any object, including None, if the user doesn't need to keep any state.

Running the Application

To run the application defined by the menu, call the communicate method for the menu, passing the state, ask, and tell callbacks:

# communicate(state, ask, tell)
main_menu.communicate({"interactions": 0}, input, print)

This will start the menu system and handle user interactions according to the defined menu and action functions.

License

PhoneTree is released under the MIT License.

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

phonetree-1.3.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

phonetree-1.3.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file phonetree-1.3.1.tar.gz.

File metadata

  • Download URL: phonetree-1.3.1.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for phonetree-1.3.1.tar.gz
Algorithm Hash digest
SHA256 8fd6cc734c9ea77c7bae9f8c24039c44867925ff441959d687178053d04802ad
MD5 4b6a652182af9d6839776ab75b6d371c
BLAKE2b-256 2f37329ae3e7330c252be67c114790e9f52902ac7f388478b5595d54da43697f

See more details on using hashes here.

File details

Details for the file phonetree-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: phonetree-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for phonetree-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9cf03ccafc7ac93071ee4ffe9945eb086f51d985805a9922eff62e69451f18dc
MD5 180d7ce1a34a1192b12184ee648040e9
BLAKE2b-256 1d60759040a43f4b838779aa298503bb17fbdce512b974b37c201091fb918051

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