๐Ÿš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more โ†’
Socket
Book a DemoInstallSign in
Socket

jsonmenu

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonmenu

The easiest way to create interactive console menus in Python using JSON

1.0.1
PyPI
Maintainers
1

JSON Menu

The easiest way to create interactive console menus in Python
Report bug ยท Request feature

Table of contents

Introduction

JSON Menu is a Python library created by Xavi Burgos that lets you create interactive console menus from basic JSON structures. With just a few lines of code, you can build user-friendly interfaces for your Python scripts.

The menu generated by JSON Menu will be displayed as the following example:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚        My first menu        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1. My first action          โ”‚
โ”‚ 2. Submenu                  โ”‚
โ”‚ 0. Exit                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Select an option: 

Installation

To install JSON Menu, you can use the following pip command:

pip install jsonmenu

Alternatively, you can clone the repository from GitHub or download the source code from the releases page and include the jsonmenu.py file in your project.

Import

Once installed, you can import the library in your Python script with the following line:

from jsonmenu import JsonMenu

How to use

To create a menu, you need to define a JSON structure with the following format:

menu_data = [
	{
		"label": "My first action",
		"action": my_function
	},
	{
		"label": "Submenu",
		"submenu": [
			{
				"label": "Option 1",
				"action": custom_function_1
			},
			{
				"label": "Option 2",
				"action": custom_function_2
			}
		]
	}
]

Also, you can add an options dictionary to customize the menu, explained at options section.

custom_options = {
	"theme": "rounded",
	# More options here...
}

Then, you can create the menu with the following code:

jm = JsonMenu("My first menu", menu_data, custom_options)
jm.show()

Compact version

A compact version of the previous code to create and show the menu is the following:

jm = JsonMenu("My first menu", [
	{
		"label": "My first action",
		"action": my_function
	},
	{
		"label": "Submenu",
		"submenu": [
			{
				"label": "Option 1",
				"action": custom_function_1
			},
			{
				"label": "Option 2",
				"action": custom_function_2
			}
		]
	}
], { "theme": "rounded" })
jm.show()

Options

The options dictionary allows you to customize the appearance and behavior of the menu. The following options are available:

OptionDescriptionDefault valuePossible values
back_menuPrevious menu to return when the user selects the 0 option. If not defined, the menu will close.NoneJsonMenu object
clearClear the console before showing the menu. This option is useful to avoid cluttering the console with previous outputs.FalseTrue, False
debugShow debug information in the console. This option is useful for development and testing. Warnings will not be displayed if clear option is enabled (console is cleared before showing the menu).FalseTrue, False
widthWidth of the menu in characters.31int
languageLanguage of the menu. Also a dictionary could be passed, and the menu will be use the provided custom translations.enar, ca, de, en, es, fr, hi, it, ja, ko, nl, pt, ru, sv, tr, zh, <custom dictionary>
themeTheme of the menu. Also a dictionary could be passed, and the menu will be use the provided custom theme.unicodeascii, unicode, bold, double, rounded, dotted, striped, <custom dictionary>

Custom language

A custom language dictionary can be passed to the language option. The dictionary must contain the following keys:

KeyDescriptionEnglish value
backText for the back option.Back
exitText for the exit option.Exit
invalid_optionText for the invalid option message.Invalid option
select_optionText for the select option message.Select an option
press_enterText for the press enter to continue message.Press "Enter" to continue

Custom theme

A custom theme dictionary can be passed to the theme option. The dictionary must contain the following keys:

KeyDescriptionUnicode value
tlTop left corner of the menu.โ”Œ
trTop right corner of the menu.โ”
clCenter left corner of the menu.โ”œ
crCenter right corner of the menu.โ”ค
blBottom left corner of the menu.โ””
brBottom right corner of the menu.โ”˜
hHorizontal line of the menu.โ”€
vVertical line of the menu.โ”‚

What's included

Within the download you'll find the following directories and files, logically grouping common assets. You'll see something like this:

jsonmenu/
โ”œโ”€โ”€ jsonmenu
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ jsonmenu.py
โ”œโ”€โ”€ setup.py
โ”œโ”€โ”€ CONTRIBUTING.md
โ”œโ”€โ”€ LICENSE
โ””โ”€โ”€ README.md

Bugs and feature requests

Have a bug or a feature request? Please first read the issue guidelines and search for existing and closed issues. If your problem or idea is not addressed yet, please open a new issue.

Contributing

Please read through our contributing guidelines. Included are directions for opening issues, coding standards, and notes on development.

Creator

Xavier Burgos

Keywords

json menu console interactive python

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts