Coverage for fastagency/ui/mesop/components/inputs.py: 85%
44 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-19 12:16 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-19 12:16 +0000
1from collections.abc import Iterator 1bcd
2from typing import Callable, Optional 1bcd
4import mesop as me 1bcd
6from ....logging import get_logger 1bcd
7from ..data_model import State 1bcd
8from ..styles import MesopTextInputInnerStyles 1bcd
10# Get the logger
11logger = get_logger(__name__) 1bcd
14def input_text( # noqa: C901 1bcd
15 on_input: Callable[[str], Iterator[None]],
16 *,
17 key: str,
18 disabled: bool = False,
19 value: Optional[str] = None,
20 style: MesopTextInputInnerStyles,
21) -> None:
22 def on_click(e: me.ClickEvent) -> Iterator[None]: 1bcda
23 state = me.state(State) 1a
24 output_key = get_output_key() 1a
25 inp = getattr(state, output_key) 1a
26 clear_in_out() 1a
27 yield from on_input(inp) 1a
29 def on_newline(e: me.TextareaShortcutEvent) -> Iterator[None]: 1bcda
30 state = me.state(State)
31 input_key = get_input_key()
32 setattr(state, input_key, e.value + "\n")
33 yield
35 def on_submit(e: me.TextareaShortcutEvent) -> Iterator[None]: 1bcda
36 clear_in_out()
37 yield from on_input(e.value)
39 def on_blur(e: me.InputBlurEvent) -> None: 1bcda
40 if disabled or e.key != key_num: 40 ↛ 41line 40 didn't jump to line 41 because the condition on line 40 was never true1a
41 return
42 state = me.state(State) 1a
43 input_key, output_key = get_in_out_keys() 1a
44 setattr(state, input_key, e.value) 1a
45 setattr(state, output_key, e.value) 1a
47 def get_input_key() -> str: 1bcda
48 return f"{key}_input" 1a
50 def get_output_key() -> str: 1bcda
51 return f"{key}_output" 1a
53 def get_in_out_keys() -> list[str]: 1bcda
54 return [get_input_key(), get_output_key()] 1a
56 def clear_in_out() -> None: 1bcda
57 input_key, output_key = get_in_out_keys() 1a
58 setattr(state, input_key, "") 1a
59 setattr(state, output_key, "") 1a
61 state = me.state(State) 1bcda
62 key_num = f"{key}{len(state.conversation.messages)}" 1bcda
63 with me.box(style=style.box): 1bcda
64 if disabled: 1bcda
65 in_value = value 1bcda
66 key_num = f"{key}disabled{len(state.conversation.messages)}" 1bcda
67 else:
68 input_key = get_input_key() 1a
69 in_value = getattr(state, input_key) 1a
70 key_num = f"{key}{len(state.conversation.messages)}" 1a
72 with me.box(style=me.Style(flex_grow=1)): 1bcda
73 me.native_textarea( 1bcda
74 on_blur=on_blur,
75 key=key_num,
76 autosize=True,
77 min_rows=3,
78 max_rows=10,
79 readonly=disabled,
80 shortcuts={
81 me.Shortcut(key="enter", shift=True): on_newline,
82 me.Shortcut(key="enter"): on_submit,
83 },
84 style=style.native_textarea,
85 value=in_value,
86 )
88 with me.content_button( 1bcda
89 type="icon",
90 on_click=on_click,
91 disabled=disabled,
92 ):
93 me.icon("send") 1bcda