in src/huggingface_hub/_login.py [0:0]
def notebook_login(*, new_session: bool = True, write_permission: bool = False) -> None:
"""
Displays a widget to log in to the HF website and store the token.
This is equivalent to [`login`] without passing a token when run in a notebook.
[`notebook_login`] is useful if you want to force the use of the notebook widget
instead of a prompt in the terminal.
For more details, see [`login`].
Args:
new_session (`bool`, defaults to `True`):
If `True`, will request a token even if one is already saved on the machine.
write_permission (`bool`):
Ignored and deprecated argument.
"""
try:
import ipywidgets.widgets as widgets # type: ignore
from IPython.display import display # type: ignore
except ImportError:
raise ImportError(
"The `notebook_login` function can only be used in a notebook (Jupyter or"
" Colab) and you need the `ipywidgets` module: `pip install ipywidgets`."
)
if not new_session and get_token() is not None:
logger.info("User is already logged in.")
return
box_layout = widgets.Layout(display="flex", flex_flow="column", align_items="center", width="50%")
token_widget = widgets.Password(description="Token:")
git_checkbox_widget = widgets.Checkbox(value=True, description="Add token as git credential?")
token_finish_button = widgets.Button(description="Login")
login_token_widget = widgets.VBox(
[
widgets.HTML(NOTEBOOK_LOGIN_TOKEN_HTML_START),
token_widget,
git_checkbox_widget,
token_finish_button,
widgets.HTML(NOTEBOOK_LOGIN_TOKEN_HTML_END),
],
layout=box_layout,
)
display(login_token_widget)
# On click events
def login_token_event(t):
"""Event handler for the login button."""
token = token_widget.value
add_to_git_credential = git_checkbox_widget.value
# Erase token and clear value to make sure it's not saved in the notebook.
token_widget.value = ""
# Hide inputs
login_token_widget.children = [widgets.Label("Connecting...")]
try:
with capture_output() as captured:
_login(token, add_to_git_credential=add_to_git_credential)
message = captured.getvalue()
except Exception as error:
message = str(error)
# Print result (success message or error)
login_token_widget.children = [widgets.Label(line) for line in message.split("\n") if line.strip()]
token_finish_button.on_click(login_token_event)