Skip to main content

Dynamic forms for Textual TUI Framework

Project description

Textual Forms

Python Versions License: MIT

Dynamic forms for Textual TUI framework.

Note: This library is still very much WIP 🧪

About

Textual Forms aims to make it easy to add forms to your Textual-powered applications.

Requirements

  • python >=3.7,<4
  • poetry

Install

pip install textual-forms

Form Field Schema

Key Type Required Options
id str X
type str string, number, integer
value str, number
required bool
placeholder str
rules dict

Type Rules

string

  • min_length
  • max_length

integer

  • min
  • max

number

  • N/A

Button Schema

Key Type Required Options
id str x
label str x
variant str primary, error, success, warning
watch_form_valid bool

Note: If you set the watch_form_valid property, the button will only be enabled when the form is valid.

Overriding Form Widgets

Documentation TBD


Example

from rich.table import Table
from textual.app import App, ComposeResult
from textual.widgets import Static

from textual_forms.forms import Form

FIELDS = [
    {
        'id': 'name',
        'required': True,
        'placeholder': 'name...',
        'rules': {
            'min_length': 3,
        }
    },
    {
        'id': 'age',
        'type': 'integer',
        'required': True,
        'placeholder': 'age...',
        'rules': {
            'min': 18,
            'max': 65
        }
    },
    {
        'id': 'email',
        'required': False,
        'placeholder': 'hi@example.com',
    },
]

BUTTONS = [
    {
        'id': 'submit',
        'label': 'Submit',
        'variant': 'success',
        'watch_form_valid': True
    },
]


class BasicTextualForm(App):
    def compose(self) -> ComposeResult:
        yield Static(id='submitted-data')
        yield Form(
            fields=FIELDS,
            buttons=BUTTONS,
        )

    def on_form_event(self, message: Form.Event) -> None:
        if message.event == 'submit':
            table = Table(*message.data.keys())
            table.add_row(*message.data.values())
            self.query_one('#submitted-data').update(table)


if __name__ == '__main__':

    BasicTextualForm().run()

Initial render Screenshot 2022-11-15 at 3 49 46 PM

Valid form Screenshot 2022-11-15 at 3 51 15 PM

Invalid form Screenshot 2022-11-15 at 3 51 39 PM

Contributing

TBD

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

textual_forms-0.1.5.tar.gz (6.6 kB view hashes)

Uploaded Source

Built Distribution

textual_forms-0.1.5-py3-none-any.whl (7.1 kB view hashes)

Uploaded Python 3

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