Skip to content

Add rtl to gr.HighlightedText #10924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/long-candies-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@gradio/atoms": minor
"@gradio/highlightedtext": minor
"gradio": minor
---

feat:Add `rtl` to gr.HighlightedText
3 changes: 3 additions & 0 deletions gradio/components/highlighted_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(
render: bool = True,
key: int | str | None = None,
interactive: bool | None = None,
rtl: bool = False,
):
"""
Parameters:
Expand All @@ -81,12 +82,14 @@ def __init__(
render: If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
key: if assigned, will be used to assume identity across a re-render. Components that have the same key across a re-render will have their value preserved.
interactive: If True, the component will be editable, and allow user to select spans of text and label them.
rtl: If True, will display the text in right-to-left direction, and the labels in the legend will also be aligned to the right.
"""
self.color_map = color_map
self.show_legend = show_legend
self.show_inline_category = show_inline_category
self.combine_adjacent = combine_adjacent
self.adjacent_separator = adjacent_separator
self.rtl = rtl
super().__init__(
label=label,
every=every,
Expand Down
3 changes: 3 additions & 0 deletions js/atoms/src/Block.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
export let min_width = 0;
export let flex = false;
export let resizable = false;
export let rtl = false;

let element: HTMLElement;

let tag = type === "fieldset" ? "fieldset" : "div";
Expand Down Expand Up @@ -80,6 +82,7 @@
style:min-width={`calc(min(${min_width}px, 100%))`}
style:border-width="var(--block-border-width)"
class:auto-margin={scale === null}
dir={rtl ? "rtl" : "ltr"}
>
<slot />
{#if resizable}
Expand Down
13 changes: 13 additions & 0 deletions js/highlightedtext/HighlightedText.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,16 @@
interactive: false
}}
/>

<Story
name="Highlighted Text with RTL"
args={{
value: [
{ token: "مرحبا", class_or_confidence: "greeting" },
{ token: " ", class_or_confidence: null },
{ token: "بالعالم", class_or_confidence: "subject" }
],
rtl: true,
color_map: { greeting: "green", subject: "blue" }
}}
/>
2 changes: 2 additions & 0 deletions js/highlightedtext/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
export let combine_adjacent = false;
export let interactive: boolean;
export let show_label = true;
export let rtl = false;

$: if (!color_map && Object.keys(color_map).length) {
color_map = color_map;
Expand Down Expand Up @@ -67,6 +68,7 @@
{container}
{scale}
{min_width}
{rtl}
>
<StatusTracker
autoscroll={gradio.autoscroll}
Expand Down
1 change: 1 addition & 0 deletions test/components/test_highlighted_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def test_component_functions(self):
"visible": True,
"value": None,
"proxy_url": None,
"rtl": False,
"_selectable": False,
"key": None,
"combine_adjacent": False,
Expand Down
Loading