https://github.com/rhymiz/textual-forms
Dynamic forms for Textual TUI framework
https://github.com/rhymiz/textual-forms
forms textual tui validation
Last synced: 7 months ago
JSON representation
Dynamic forms for Textual TUI framework
- Host: GitHub
- URL: https://github.com/rhymiz/textual-forms
- Owner: rhymiz
- License: mit
- Created: 2022-11-12T05:30:44.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-18T01:36:18.000Z (over 2 years ago)
- Last Synced: 2025-03-11T07:18:55.843Z (7 months ago)
- Topics: forms, textual, tui, validation
- Language: Python
- Homepage: https://github.com/rhymiz/textual-forms
- Size: 139 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Textual Forms
[](https://www.python.org/downloads/)
[](https://opensource.org/licenses/MIT)
[](https://pepy.tech/project/textual-forms)
[](https://pepy.tech/project/textual-forms)Dynamic forms for [Textual](https://github.com/willmcgugan/textual) TUI framework.
> #### Note: This library is still very much WIP 🧪. This means that breaking changes can be introduced at any point in time.
## About
Textual Forms aims to make it easy to add forms to your Textual-powered applications.
### Development Requirements
* python >=3.7,<4
* poetry
* textual >=0.11.0## Install
```bash
pip install textual-forms
```## Forms
`textual_forms.forms.Form`
## Buttons
`textual_forms.buttons.Button`
## Fields
`textual_forms.fields.StringField`
`textual_forms.fields.NumberField`
`textual_forms.fields.IntegerField`
### Custom fields and validators
```python
from __future__ import annotationsfrom typing import Any
from textual_forms.fields import Field
from textual_forms.validators import FieldValidatorclass UUIDValidator(FieldValidator):
def validate(self, value: str, rules: dict[str, Any]) -> tuple[bool, str | None]:
return True, Noneclass UUIDField(Field):
validator = UUIDValidator()def __init__(
self,
name: str,
*,
value: str | None = None,
required: bool = False,
placeholder: str | None = None,
**kwargs,
):
data: dict[str, Any] = {
"name": name,
"value": value,
"required": required,
"placeholder": placeholder,
"rules": {},
}
super().__init__(data, **kwargs)
```---
## Example
```python
from rich.table import Table
from textual.app import App, ComposeResult
from textual.widgets import Staticfrom textual_forms.forms import Form
from textual_forms.fields import StringField, IntegerField
from textual_forms.buttons import Buttonclass BasicTextualForm(App):
def compose(self) -> ComposeResult:
yield Static(id="submitted-data")
yield Static("Order for beers")
yield Form(
fields=[
StringField("name"),
IntegerField("age", required=True, min_value=21),
],
buttons=[
Button(
"Submit",
enabled_on_form_valid=True,
)
],
)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**
**Valid form**
**Invalid form**
## Contributing
TBD