-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsubmodel.py
More file actions
47 lines (36 loc) · 1.29 KB
/
Copy pathsubmodel.py
File metadata and controls
47 lines (36 loc) · 1.29 KB
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
import logging
from nicegui import ui
from pydantic import BaseModel, SerializationInfo, model_serializer
from niceguicrud import NiceCRUD, NiceCRUDConfig
log = logging.getLogger("niceguicrud")
log.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter("%(levelname)s - %(name)s - %(message)s"))
log.addHandler(console_handler)
class SpouseModel(BaseModel):
name: str
age: int
is_employed: bool = False
@model_serializer(mode="wrap")
def gui(self, default_serializer, info: SerializationInfo):
context = info.context # pyright: ignore[reportAttributeAccessIssue]
if context and context.get("gui"):
return self.name + (" (unemployed)" if not self.is_employed else "")
return default_serializer(self)
class Actor(BaseModel):
name: str
age: int
spouse: SpouseModel
actress = Actor(
name="Scarlett Joh",
age=36,
spouse=SpouseModel(name="Colin Jost", age=39, is_employed=True),
)
another_actress = Actor(
name="Natalie Portman",
age=40,
spouse=SpouseModel(name="Benjamin Millepied", age=45),
)
crud_config = NiceCRUDConfig(id_field="name", heading="Actress Database")
crud_app = NiceCRUD(basemodels=[actress, another_actress], config=crud_config)
ui.run(show=False)