Ensure LoginButton value text is displayed (#6808)

* fix LoginButton value

* add changeset

* formatting

* add `signed_in_value` parameter

* tweak

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
Hannah 2023-12-15 20:48:25 +01:00 committed by GitHub
parent c352811f76
commit 6b130e26b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
fix:Ensure LoginButton `value` text is displayed

View File

@ -24,6 +24,7 @@ class LoginButton(Button):
def __init__(
self,
value: str = "Sign in with Hugging Face",
signed_in_value: str = "Signed in as {}",
*,
every: float | None = None,
variant: Literal["primary", "secondary", "stop"] = "secondary",
@ -39,6 +40,13 @@ class LoginButton(Button):
scale: int | None = 0,
min_width: int | None = None,
):
"""
Parameters:
signed_in_value: The text to display when the user is signed in. The string should contain a placeholder for the username, e.g. "Signed in as {}".
"""
if signed_in_value is None:
signed_in_value = "Signed in as {}"
self.signed_in_value = signed_in_value
super().__init__(
value,
every=every,
@ -75,10 +83,11 @@ class LoginButton(Button):
request.request, "session", None
)
if session is None or "oauth_info" not in session:
return LoginButton("Sign in with Hugging Face", interactive=True)
return LoginButton(value=self.value, interactive=True)
else:
username = session["oauth_info"]["userinfo"]["preferred_username"]
return LoginButton(f"Signed in as {username}", interactive=False)
signed_in_text = self.signed_in_value.format(username)
return LoginButton(signed_in_text, interactive=False)
# JS code to redirects to /login/huggingface if user is not logged in.