Skip to content

Commit d6be33e

Browse files
ujjwaltwrigradio-pr-botfreddyaboulton
authored
feat: add link_target parameter to Button component (#12524)
* feat: add link_target parameter to Button component - Add link_target parameter with options: _self, _blank, _parent, _top - Default to _self for backward compatibility - Add rel='noopener noreferrer' for security when using _blank - Update documentation - Closes #12509 * add changeset * Lint + fix * Lint * fix: add link_target parameter to Button template components - Add link_target to ClearButton, DuplicateButton, LoginButton, DeepLinkButton - Fixes test_template_component_configs test failure - Ensures all Button templates have the same parameters as Button --------- Co-authored-by: gradio-pr-bot <[email protected]> Co-authored-by: Freddy Boulton <[email protected]>
1 parent e8efab2 commit d6be33e

File tree

8 files changed

+40
-1
lines changed

8 files changed

+40
-1
lines changed

.changeset/empty-jobs-shake.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gradio/button": minor
3+
"gradio": minor
4+
---
5+
6+
feat:feat: add link_target parameter to Button component

gradio/components/button.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(
3434
size: Literal["sm", "md", "lg"] = "lg",
3535
icon: str | Path | None = None,
3636
link: str | None = None,
37+
link_target: Literal["_self", "_blank", "_parent", "_top"] = "_self",
3738
visible: bool | Literal["hidden"] = True,
3839
interactive: bool = True,
3940
elem_id: str | None = None,
@@ -53,6 +54,7 @@ def __init__(
5354
size: size of the button. Can be "sm", "md", or "lg".
5455
icon: URL or path to the icon file to display within the button. If None, no icon will be displayed.
5556
link: URL to open when the button is clicked. If None, no link will be used.
57+
link_target: determines where to open the linked URL. "_self" (default, same tab), "_blank" (new tab), "_parent" (parent frame), "_top" (top frame).
5658
visible: If False, component will be hidden. If "hidden", component will be visually hidden and not take up space in the layout but still exist in the DOM
5759
interactive: if False, the Button will be in a disabled state.
5860
elem_id: an optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
@@ -81,6 +83,7 @@ def __init__(
8183
self.variant = variant
8284
self.size = size
8385
self.link = link
86+
self.link_target = link_target
8487

8588
@property
8689
def skip_api(self):

gradio/components/clear_button.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __init__(
4040
size: Literal["sm", "md", "lg"] = "lg",
4141
icon: str | Path | None = None,
4242
link: str | None = None,
43+
link_target: Literal["_self", "_blank", "_parent", "_top"] = "_self",
4344
visible: bool | Literal["hidden"] = True,
4445
interactive: bool = True,
4546
elem_id: str | None = None,
@@ -52,6 +53,25 @@ def __init__(
5253
api_name: str | None = None,
5354
api_visibility: Literal["public", "private", "undocumented"] = "undocumented",
5455
):
56+
"""
57+
Parameters:
58+
value: default text for the button to display. If a function is provided, the function will be called each time the app loads to set the initial value of this component.
59+
every: continuously calls `value` to recalculate it if `value` is a function (has no effect otherwise). Can provide a Timer whose tick resets `value`, or a float that provides the regular interval for the reset Timer.
60+
inputs: components that are used as inputs to calculate `value` if `value` is a function (has no effect otherwise). `value` is recalculated any time the inputs change.
61+
variant: sets the background and text color of the button. Use 'primary' for main call-to-action buttons, 'secondary' for a more subdued style, 'stop' for a stop button, 'huggingface' for a black background with white text, consistent with Hugging Face's button styles.
62+
size: size of the button. Can be "sm", "md", or "lg".
63+
icon: URL or path to the icon file to display within the button. If None, no icon will be displayed.
64+
link: URL to open when the button is clicked. If None, no link will be used.
65+
visible: If False, component will be hidden. If "hidden", component will be visually hidden and not take up space in the layout but still exist in the DOM
66+
interactive: if False, the Button will be in a disabled state.
67+
elem_id: an optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
68+
elem_classes: an optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
69+
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.
70+
key: in a gr.render, Components with the same key across re-renders are treated as the same component, not a new component. Properties set in 'preserved_by_key' are not reset across a re-render.
71+
preserved_by_key: A list of parameters from this component's constructor. Inside a gr.render() function, if a component is re-rendered with the same key, these (and only these) parameters will be preserved in the UI (if they have been changed by the user or an event listener) instead of re-rendered based on the values provided during constructor.
72+
scale: relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.
73+
min_width: minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
74+
"""
5575
super().__init__(
5676
value,
5777
every=every,
@@ -60,6 +80,7 @@ def __init__(
6080
size=size,
6181
icon=icon,
6282
link=link,
83+
link_target=link_target,
6384
visible=visible,
6485
interactive=interactive,
6586
elem_id=elem_id,

gradio/components/deep_link_button.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def __init__(
3939
size: Literal["sm", "md", "lg"] = "lg",
4040
icon: str | Path | None = utils.get_icon_path("link.svg"),
4141
link: str | None = None,
42+
link_target: Literal["_self", "_blank", "_parent", "_top"] = "_self",
4243
visible: bool | Literal["hidden"] = True,
4344
interactive: bool = True,
4445
elem_id: str | None = None, # noqa: ARG002
@@ -63,6 +64,7 @@ def __init__(
6364
size=size,
6465
icon=icon,
6566
link=link,
67+
link_target=link_target,
6668
visible=visible,
6769
interactive=interactive,
6870
elem_id=f"gradio-share-link-button-{self.n_created}",

gradio/components/duplicate_button.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(
3434
size: Literal["sm", "md", "lg"] = "sm",
3535
icon: str | Path | None = None,
3636
link: str | None = None,
37+
link_target: Literal["_self", "_blank", "_parent", "_top"] = "_self",
3738
visible: bool | Literal["hidden"] = True,
3839
interactive: bool = True,
3940
elem_id: str | None = None,
@@ -73,6 +74,7 @@ def __init__(
7374
size=size,
7475
icon=icon,
7576
link=link,
77+
link_target=link_target,
7678
visible=visible,
7779
interactive=interactive,
7880
elem_id=elem_id,

gradio/components/login_button.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(
4949
size: Literal["sm", "md", "lg"] = "lg",
5050
icon: str | Path | None = utils.get_icon_path("huggingface-logo.svg"),
5151
link: str | None = None,
52+
link_target: Literal["_self", "_blank", "_parent", "_top"] = "_self",
5253
visible: bool | Literal["hidden"] = True,
5354
interactive: bool = True,
5455
elem_id: str | None = None,
@@ -72,6 +73,7 @@ def __init__(
7273
size=size,
7374
icon=icon,
7475
link=link,
76+
link_target=link_target,
7577
visible=visible,
7678
interactive=interactive,
7779
elem_id=elem_id,

js/button/Index.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
min_width={gradio.shared.min_width}
2929
visible={gradio.shared.visible}
3030
disabled={!gradio.shared.interactive}
31+
link_target={gradio.props.link_target}
3132
on:click={handle_click}
3233
>
3334
{gradio.props.value ?? ""}

js/button/shared/Button.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
export let size: "sm" | "md" | "lg" = "lg";
1111
export let value: string | null = null;
1212
export let link: string | null = null;
13+
export let link_target: "_self" | "_blank" | "_parent" | "_top" = "_self";
1314
export let icon: FileData | null = null;
1415
export let disabled = false;
1516
export let scale: number | null = null;
@@ -19,7 +20,8 @@
1920
{#if link && link.length > 0}
2021
<a
2122
href={link}
22-
rel="noopener noreferrer"
23+
target={link_target}
24+
rel={link_target === "_blank" ? "noopener noreferrer" : undefined}
2325
class:hidden={visible === false || visible === "hidden"}
2426
class:disabled
2527
aria-disabled={disabled}

0 commit comments

Comments
 (0)