How to Create a Simple Menu Maker for Python Scripts

The aim of this page📝 is to share a module I’m using for creating a simple menu in Python scripts.

Let’s see the CIA (code-in-action:) first

A simple menu asking for the number of menu item as a selection mechanism

First, put the following code into a dedicated module

  • It’s just a class that creates an object with list of menu items and has a printer method and a simple selector

Then, in your script, import it ⟹ initialize with a list of options ⟹ print_menu ⟹ and select_menu_item

  • Import your menu_maker (not going into the weeds of importing here)
  • It takes a list of items, for example
  • The select_menu_item() maps the numerical input to the proper name of the menu item — Delete is 1 ; Patch is 2 ; etc.
  • And that’s all… in my case, I continue with your selection whatever you please, e.g. I’m calling dedicated functions with readable code
  • Since it takes a list, you can dynamically generate menu items, too (like I am using a base name of files for certain menu items ⟹ you can map them to template files in your folder, etc.)

--

--

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com