Your first form#
In this tutorial you will create a small interactive terminal form, run it, and inspect the answers it returns. No prior knowledge of TUI Forms is required.
By the end you will have a working Python script that asks the user three questions and prints the collected answers.
What you will build#
A form that collects:
a project name (free-text, with a default)
whether to include tests (yes/no)
a preferred license (single choice from a fixed list)
Prerequisites#
Python 3.12 or later
tui-formsinstalled:pip install tui-formsOr, with
uv:uv add tui-forms
Step 1—Define the schema#
TUI Forms reads a standard JSONSchema dict to decide what questions to
ask.
Create a new file called first_form.py and add the schema:
schema = {
"title": "New project",
"properties": {
"project_name": {
"type": "string",
"title": "Project name",
"default": "my-project",
},
"use_tests": {
"type": "boolean",
"title": "Include tests?",
"default": True,
},
"license": {
"type": "string",
"title": "License",
"default": "MIT",
"oneOf": [
{"const": "MIT", "title": "MIT"},
{"const": "Apache-2.0", "title": "Apache 2.0"},
{"const": "GPL-3.0", "title": "GNU GPL v3"},
],
},
},
}
Each key under properties becomes one question:
Key |
Schema construct |
Question type |
|---|---|---|
|
|
Free-text input |
|
|
Yes/no prompt |
|
|
Single-choice menu |
Step 2—Create a renderer and run the form#
Add these lines after the schema definition:
from tui_forms import create_renderer
renderer = create_renderer("stdlib", schema)
answers = renderer.render()
print(answers)
create_renderer parses the schema and returns a renderer ready to run.
render() starts the interactive session and returns a dict when the user has
answered all questions.
The full first_form.py script:
from tui_forms import create_renderer
schema = {
"title": "New project",
"properties": {
"project_name": {
"type": "string",
"title": "Project name",
"default": "my-project",
},
"use_tests": {
"type": "boolean",
"title": "Include tests?",
"default": True,
},
"license": {
"type": "string",
"title": "License",
"default": "MIT",
"oneOf": [
{"const": "MIT", "title": "MIT"},
{"const": "Apache-2.0", "title": "Apache 2.0"},
{"const": "GPL-3.0", "title": "GNU GPL v3"},
],
},
},
}
renderer = create_renderer("stdlib", schema)
answers = renderer.render()
print(answers)
Step 3—Run it#
python first_form.py
You will see three prompts, one per question. Press Enter to accept the shown default, or type a new value. After the last question the script prints the collected answers:
{'project_name': 'my-project', 'use_tests': True, 'license': 'MIT'}
What just happened#
create_renderer("stdlib", schema)parsed the schema and created aStdlibRenderer: a renderer that uses only Python's standard library for I/O.renderer.render()iterated through the questions in schema order, asked each one, and recorded the answers.The returned dict maps each property key to the value the user provided (or the default they accepted).
Next steps#
Store the schema in a JSON file and load it with
json.loads(Path("form.json").read_text()).Try the
richrenderer for styled prompts:create_renderer("rich", schema)(requirespip install "tui-forms[rich]").Conditional questions: show extra questions only when a previous answer matches a specific value.
JSONSchema support: full reference for all supported schema constructs.