Skip to main content

Multiplexed user input via console, GUI, and web, depending on availability.

Project description

dworshak-prompt

A Python utility that ensures you can always get user input by falling back through multiple interfaces. dworshak-prompt is a CI-safe, shell-friendly prompting engine that integrates with persistent config and secret storage.

How it works

It captures input by cycling through modes based on environment availability:

  1. Console (CLI)
  2. GUI (Tkinter)
  3. Web (Local Browser Server)

Automatically skips incompatible modes (e.g., GUI on WSL) via pyhabitat.

Install

Install on most systems.

pip install "dworshak-prompt[typer,cryptography]"

Install on Termux.

pkg add python-cryptography
uv venv --system-site-packages
uv add "dworshak-prompt[typer]"

Obtain vs DworshakPrompt

Use Obtain for managed config/secret storage; use DworshakPrompt directly if you want to handle values yourself.

Usage

Obtain

Leverage dworshak-config, dworshak-secret, and dworshak-env to automatically handle values.

from dworshak_prompt import Obtain

obtain = Obtain()

obtain.config(service, item, message)
obtain.secret(service, item, message)
obtain.env(key, message)

Ask

Prompt the user then handle values manually.

from dworshak_prompt import DworshakPrompt, PromptMode

# Basic
val = DworshakPrompt().ask("Enter value")

# Options
val = DworshakPrompt().ask(
    message = "Secure Key",
    hide_input=True,
    interface_priority = [PromptMode.CONSOLE, PromptMode.GUI]
    interface_avoid = {PromptMode.WEB}

)

Another example, for handling CI:

from dworshak_prompt import Obtain

# If this runs in GitHub Actions, it returns "staging" immediately.
# If it runs on a laptop, it pops up a GUI or Console prompt.
obtain = Obtain()
val = obtain.env(
    key = "TARGET_ENV",
    message = "Target Environment",
    suggestion="production",  # What the human sees
    default="staging"         # What the CI/Headless system uses if the request fails.
)

Leveraging dworshak-prompt for calling and adding configured values. The default config file path is "~/.dworshak/config.json".

from dworshak_prompt import Obtain, PromptMode, InterruptBehavior, 

# Identify custom path for a specific project, and set defaults during instantiation of the Obtain class.
obtain = Obtain(
    config_path="~/.pipeline-eds/config.json"
    interface_priority = [PromptMode.GUI, PromptMode.WEB]
    interface_avoid = {PromptMode.CONSOLE}
    interrupt_behavior = InterruptBehavior.EXIT,
    debug = True
    )
api_key = obtain.config("emerson-ovation-api","api_key", message="Enter EDS API Key")

Another example of the Obtain pattern.

from O365 import Account
from dworshak_prompt import Obtain, InterruptBehavior, PromptMode
import os
import logging
logger=logging.getLogger(__name__)

avoid_set = set()
DWO_AVOID_CONSOLE = os.environ.get('DWO_AVOID_CONSOLE')
if  DWO_AVOID_CONSOLE == "1":
    avoid_set.add(PromptMode.CONSOLE)
else:
    logger.warning("Use 'export DWO_AVOID_CONSOLE=1 to avoid the console, to make enable unhiding hidden input.'")
logger.debug(f"{avoid_set=}")
logger.debug(f"{DWO_AVOID_CONSOLE=}")

# Instantiate the prompt handler
prompt = Obtain(
    interrupt_behavior = InterruptBehavior.EXIT,
    interface_avoid = avoid_set
)

# Ask for client credentials securely
CLIENT_ID = prompt.secret(
    service="o365",
    item="CLIENT_ID",
    message="Enter your O365 Client ID: "
    ).value
CLIENT_SECRET = prompt.secret(
    service="o365",
    item="CLIENT_SECRET",
    message="Enter your O365 Client Secret: "
    ).value

# Prepare credentials tuple
credentials = (CLIENT_ID, CLIENT_SECRET)

# Initialize O365 account
account = Account(credentials)

# Authenticate (interactive for first-time login)
if not account.is_authenticated:
    account.authenticate(scopes=['https://graph.microsoft.com/Mail.Read'])

# Access mailbox and list folders
mailbox = account.mailbox()
folders = mailbox.list_folders()

print("Mailbox folders:")
for folder in folders:
    print("-", folder.name)

CLI

The dworshak layer is the intended primary CLI entry point, but the dworshak-prompt CLI can be used directly.

pipx install "dworshak-prompt[typer]"
dworshak-prompt --version
dworshak-prompt --help
dworshak-prompt ask --message "Please state name" --interface web

dworshak-prompt is designed to be useful even without Python code. It can be used directly from shell scripts, CI pipelines, and ops tooling to safely obtain, persist, and reuse configuration values.

Piping and environment variable capture works, but will naturally fallback to GUI or Web input because in these cases stdout is not a TTY. However, you may continue to use console input and leverage dev/tty by using envionrmental variable DWORSHAK_FORCE_INTERACTIVE_TTY.

# Enable console input during wrapped or piped shell capture.
export DWORSHAK_FORCE_INTERACTIVE_TTY=1
VAR=$(dworshak-prompt obtain secret "special_api" "password" --emit)

See the dworshak-prompt Typer CLI structure.

dworshak-prompt helptree

Screenshot of the dworshak-prompt CLI helptree

helptree is utility funtion for Typer CLIs, imported from the typer-helptree library.


Add dworshak-prompt to Python project

When using uv for dependency management.

uv add dworshak-prompt --extra typer

Or, when using raw pip for dependency management.

pip install "dworshak-prompt[typer]"

Including the typer optional dependency group ensures that Typer and Rich are included as a dependencies. Without this, the CLI and console prompting functionality are still stable, due to Python standard library fallbacks.


More Information


Sister Projects in the Dworshak Ecosystem

pipx install dworshak
pip install dworshak-secret
pip install dworshak-config
pip install dworshak-env
pip install dworshak-prompt

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

dworshak_prompt-0.3.4.tar.gz (32.8 kB view details)

Uploaded Source

Built Distribution

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

dworshak_prompt-0.3.4-py3-none-any.whl (37.5 kB view details)

Uploaded Python 3

File details

Details for the file dworshak_prompt-0.3.4.tar.gz.

File metadata

  • Download URL: dworshak_prompt-0.3.4.tar.gz
  • Upload date:
  • Size: 32.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dworshak_prompt-0.3.4.tar.gz
Algorithm Hash digest
SHA256 4de0a2ae3fc6d43f1fe6effafb2a4355b5963e533361f2caeba951b6047fd24a
MD5 d74143a3c81d427b8c72353118e231ba
BLAKE2b-256 e0fb9ae909c66cb518be5d3d094388e5a6ab52a5dd4672e382b6866cbd2fdc2b

See more details on using hashes here.

Provenance

The following attestation bundles were made for dworshak_prompt-0.3.4.tar.gz:

Publisher: publish.yml on City-of-Memphis-Wastewater/dworshak-prompt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dworshak_prompt-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: dworshak_prompt-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 37.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dworshak_prompt-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a2a749e02c8ad950e10a0d820f21b433b19f4e1ba668342f80427dc76c99082a
MD5 a4b42dac0dc7f89595e04e866f44f195
BLAKE2b-256 9f1dc3eee7bee246593ef8f1e9a0974c8e9a653508d24a590b1b104d8dc377c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for dworshak_prompt-0.3.4-py3-none-any.whl:

Publisher: publish.yml on City-of-Memphis-Wastewater/dworshak-prompt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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