Spaces:
Running
Running
Daryl Lim Claude Opus 4.6 (1M context) commited on
Commit ·
0164e89
1
Parent(s): 0d20f5b
feat: move translate button into input column
Browse filesPlace the Translate button below the input textbox and info text
so the action is closer to where the user types.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- app.py +1 -2
- tests/test_app.py +22 -0
app.py
CHANGED
|
@@ -126,6 +126,7 @@ def _build_demo() -> gr.Blocks:
|
|
| 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,
|
|
@@ -141,8 +142,6 @@ def _build_demo() -> gr.Blocks:
|
|
| 141 |
buttons=["copy"],
|
| 142 |
)
|
| 143 |
|
| 144 |
-
translate_btn = gr.Button("Translate", variant="primary")
|
| 145 |
-
|
| 146 |
translate_btn.click(
|
| 147 |
fn=_translate_with_loading,
|
| 148 |
inputs=[input_text, target_language],
|
|
|
|
| 126 |
buttons=["clear"],
|
| 127 |
)
|
| 128 |
input_info = gr.HTML(value=_format_info(0))
|
| 129 |
+
translate_btn = gr.Button("Translate", variant="primary")
|
| 130 |
with gr.Column():
|
| 131 |
target_language = gr.Dropdown(
|
| 132 |
choices=language_names,
|
|
|
|
| 142 |
buttons=["copy"],
|
| 143 |
)
|
| 144 |
|
|
|
|
|
|
|
| 145 |
translate_btn.click(
|
| 146 |
fn=_translate_with_loading,
|
| 147 |
inputs=[input_text, target_language],
|
tests/test_app.py
CHANGED
|
@@ -178,6 +178,28 @@ def test_demo_has_translate_button(demo):
|
|
| 178 |
assert any(b.value == "Translate" for b in buttons), "Expected a 'Translate' button"
|
| 179 |
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
def test_translate_handlers_wire_text_and_language(demo):
|
| 182 |
"""Translate click and submit handlers should wire input text and target language."""
|
| 183 |
translate_fns = [fn for fn in demo.fns.values() if [type(i).__name__ for i in fn.inputs] == ["Textbox", "Dropdown"]]
|
|
|
|
| 178 |
assert any(b.value == "Translate" for b in buttons), "Expected a 'Translate' button"
|
| 179 |
|
| 180 |
|
| 181 |
+
def _find_ancestor(block, ancestor_type: str):
|
| 182 |
+
"""Walk up the parent chain to find the nearest ancestor of a given type."""
|
| 183 |
+
node = getattr(block, "parent", None)
|
| 184 |
+
while node is not None:
|
| 185 |
+
if type(node).__name__ == ancestor_type:
|
| 186 |
+
return node
|
| 187 |
+
node = getattr(node, "parent", None)
|
| 188 |
+
return None
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
def test_translate_button_in_input_column(demo):
|
| 192 |
+
"""Translate button should be in the same column as the input textbox."""
|
| 193 |
+
textboxes = [b for b in demo.blocks.values() if type(b).__name__ == "Textbox"]
|
| 194 |
+
input_tb = [t for t in textboxes if t.interactive is not False][0]
|
| 195 |
+
buttons = [b for b in demo.blocks.values() if type(b).__name__ == "Button" and b.value == "Translate"]
|
| 196 |
+
assert len(buttons) == 1
|
| 197 |
+
input_column = _find_ancestor(input_tb, "Column")
|
| 198 |
+
button_column = _find_ancestor(buttons[0], "Column")
|
| 199 |
+
assert input_column is not None, "Input textbox should be inside a Column"
|
| 200 |
+
assert input_column is button_column, "Translate button should be in the input column"
|
| 201 |
+
|
| 202 |
+
|
| 203 |
def test_translate_handlers_wire_text_and_language(demo):
|
| 204 |
"""Translate click and submit handlers should wire input text and target language."""
|
| 205 |
translate_fns = [fn for fn in demo.fns.values() if [type(i).__name__ for i in fn.inputs] == ["Textbox", "Dropdown"]]
|