-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_app.py
86 lines (65 loc) · 2.38 KB
/
user_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import annotations
import json
from datetime import date
from enum import Enum
from typing import Literal
from pydantic import BaseModel, Field
from textual import on
from textual.app import App, ComposeResult
from textual.containers import Horizontal, VerticalScroll
from textual.widgets import Footer
from textual_jsonschema_form import FormContainer, JsonSchemaTree
from textual_jsonschema_form.converter import TextualObjectParams
class Interests(str, Enum):
SPORTS = "Sports"
TV = "TV"
MATH = "Math"
LIT = "Literature"
class Address(BaseModel):
street: str = Field(title="Street Name")
street_num: str = Field(title="Street Number")
postal_code: int
city: str = Field(description="City")
class UserModel(BaseModel):
first_name: str = Field(description="Name of User")
age: int = Field(description="Your age", ge=10, le=115)
country: Literal["CH", "DE", "AT"]
interests: tuple[Interests, ...] = Field(
(Interests.LIT,), description="Your interests"
)
vegetarian: bool = Field(False, description="Are you a veggie?")
date_of_birth: date = Field(description="Your date of birth")
address: Address
user_data = dict(
first_name="Hans",
age=11,
country="CH",
date_of_birth=date.today(),
address=dict(street="A", street_num="55", postal_code=8000, city="Bobo"),
interests=["TV"],
)
schema = UserModel.model_json_schema()
converter = TextualObjectParams.from_jsonschema(UserModel.model_json_schema())
class UserApp(App):
BINDINGS = [("ctrl+l", "load_data", "Load Data")]
FORM_CONTAINER_ID = "form-container"
FORM_TREE_ID = "form-tree"
def compose(self) -> ComposeResult:
with Horizontal():
with VerticalScroll():
tree = JsonSchemaTree("Form Navigator")
tree.root.expand()
yield tree
with VerticalScroll(id=self.FORM_CONTAINER_ID):
yield FormContainer(model=converter)
yield Footer()
def on_mount(self):
self.query_one(JsonSchemaTree).data = converter
def action_load_data(self):
self.query_one(FormContainer).form_data = user_data
@on(FormContainer.FormSubmitted)
def on_submit_form(self, event: FormContainer.FormSubmitted):
self.notify(json.dumps(event.form_data, indent=2), timeout=7)
if __name__ == "__main__":
app = UserApp()
app.run()