Daryl Lim Claude Opus 4.6 (1M context) commited on
Commit
0d20f5b
·
1 Parent(s): 809ee63

refactor: move hint and char count from textbox info to gr.HTML element

Browse files

Replace the input textbox's info field with a standalone gr.HTML element
so both input and output panels have matching textbox styling.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Files changed (2) hide show
  1. app.py +9 -5
  2. tests/test_app.py +8 -11
app.py CHANGED
@@ -87,11 +87,15 @@ def translate(
87
 
88
 
89
  def _format_info(char_count: int) -> str:
90
- return f"Press Ctrl+Enter to apply · {char_count} / 2,000"
 
 
 
 
91
 
92
 
93
- def _update_char_count(text: str) -> dict:
94
- return gr.update(info=_format_info(len(text)))
95
 
96
 
97
  def _translate_with_loading(
@@ -120,8 +124,8 @@ def _build_demo() -> gr.Blocks:
120
  max_length=2000,
121
  show_label=False,
122
  buttons=["clear"],
123
- info=_format_info(0),
124
  )
 
125
  with gr.Column():
126
  target_language = gr.Dropdown(
127
  choices=language_names,
@@ -152,7 +156,7 @@ def _build_demo() -> gr.Blocks:
152
  input_text.change(
153
  fn=_update_char_count,
154
  inputs=input_text,
155
- outputs=input_text,
156
  )
157
 
158
  return demo
 
87
 
88
 
89
  def _format_info(char_count: int) -> str:
90
+ return (
91
+ f'<p style="font-size: 0.85em; color: #888; margin: 4px 0 0 0;">'
92
+ f"Press Ctrl+Enter to apply · {char_count} / 2,000"
93
+ f"</p>"
94
+ )
95
 
96
 
97
+ def _update_char_count(text: str) -> str:
98
+ return _format_info(len(text))
99
 
100
 
101
  def _translate_with_loading(
 
124
  max_length=2000,
125
  show_label=False,
126
  buttons=["clear"],
 
127
  )
128
+ input_info = gr.HTML(value=_format_info(0))
129
  with gr.Column():
130
  target_language = gr.Dropdown(
131
  choices=language_names,
 
156
  input_text.change(
157
  fn=_update_char_count,
158
  inputs=input_text,
159
+ outputs=input_info,
160
  )
161
 
162
  return demo
tests/test_app.py CHANGED
@@ -185,28 +185,25 @@ def test_translate_handlers_wire_text_and_language(demo):
185
 
186
 
187
  def test_char_count_handler_exists(demo):
188
- """Character count handler should wire input text back to itself."""
189
  char_fns = [
190
  fn
191
  for fn in demo.fns.values()
192
- if [type(i).__name__ for i in fn.inputs] == ["Textbox"]
193
- and [type(o).__name__ for o in fn.outputs] == ["Textbox"]
194
  ]
195
  assert len(char_fns) == 1, f"Expected 1 char count handler, found {len(char_fns)}"
196
 
197
 
198
  def test_char_count_shows_limit(demo):
199
- """Input textbox info should show '0 / 2,000' format."""
200
- textboxes = [b for b in demo.blocks.values() if type(b).__name__ == "Textbox"]
201
- interactive = [t for t in textboxes if t.interactive is not False]
202
- assert "2,000" in interactive[0].info, f"Expected '2,000' in info, got {interactive[0].info!r}"
203
 
204
 
205
  def test_shortcut_hint_exists(demo):
206
- """Input textbox info should include shortcut hint."""
207
- textboxes = [b for b in demo.blocks.values() if type(b).__name__ == "Textbox"]
208
- interactive = [t for t in textboxes if t.interactive is not False]
209
- assert "Press Ctrl+Enter to apply" in interactive[0].info, f"Expected hint in info, got {interactive[0].info!r}"
210
 
211
 
212
  def test_no_title(demo):
 
185
 
186
 
187
  def test_char_count_handler_exists(demo):
188
+ """Character count handler should wire input text to the HTML info element."""
189
  char_fns = [
190
  fn
191
  for fn in demo.fns.values()
192
+ if [type(i).__name__ for i in fn.inputs] == ["Textbox"] and [type(o).__name__ for o in fn.outputs] == ["HTML"]
 
193
  ]
194
  assert len(char_fns) == 1, f"Expected 1 char count handler, found {len(char_fns)}"
195
 
196
 
197
  def test_char_count_shows_limit(demo):
198
+ """HTML info element should show '0 / 2,000' format."""
199
+ html_blocks = [b for b in demo.blocks.values() if type(b).__name__ == "HTML"]
200
+ assert any("2,000" in (h.value or "") for h in html_blocks), "Expected '2,000' in an HTML block"
 
201
 
202
 
203
  def test_shortcut_hint_exists(demo):
204
+ """HTML info element should include shortcut hint."""
205
+ html_blocks = [b for b in demo.blocks.values() if type(b).__name__ == "HTML"]
206
+ assert any("Press Ctrl+Enter to apply" in (h.value or "") for h in html_blocks), "Expected hint in an HTML block"
 
207
 
208
 
209
  def test_no_title(demo):