Skip to content

Commit

Permalink
fix launch some bugs (#658)
Browse files Browse the repository at this point in the history
* set background

* fix layout

* version: 0.3.15-alpha.4
  • Loading branch information
SAKURA-CAT authored Jul 27, 2024
1 parent 4c36010 commit f74e89b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 9 deletions.
8 changes: 5 additions & 3 deletions swanlab/api/auth/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
from swanlab.package import get_user_setting_path, get_host_api
from swanlab.api.info import LoginInfo
from swanlab.log import swanlog
from swanlab.env import in_jupyter
from swanlab.env import in_jupyter, SwanLabEnv
import getpass
import requests
import sys
import os


def login_request(api_key: str, timeout: int = 20) -> requests.Response:
Expand Down Expand Up @@ -75,13 +76,14 @@ def input_api_key(

def code_login(api_key: str) -> LoginInfo:
"""
代码内登录,此时会覆盖本地token文件
代码内登录,此时会覆盖本地token文件(非task模式下)
:param api_key: 用户的api_key
:return: 登录信息
:raises ValidationError: 登录失败
"""
tip = "Waiting for the swanlab cloud response."
login_info: LoginInfo = FONT.loading(tip, login_by_key, args=(api_key,), interval=0.5)
save_key = os.environ.get(SwanLabEnv.RUNTIME.value) != 'task'
login_info: LoginInfo = FONT.loading(tip, login_by_key, args=(api_key, 20, save_key), interval=0.5)
if login_info.is_fail:
swanlog.error("Login failed: " + str(login_info).lower())
raise ValidationError("Login failed: " + str(login_info))
Expand Down
2 changes: 1 addition & 1 deletion swanlab/api/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def __before_request(self):
if (self.sid_expired_at - datetime.utcnow()).total_seconds() <= self.REFRESH_TIME:
# 刷新sid,新建一个会话
swanlog.debug("Refresh sid...")
self.__login_info = login_by_key(self.__login_info.api_key)
self.__login_info = login_by_key(self.__login_info.api_key, save=False)
self.__session.headers["cookie"] = f"sid={self.__login_info.sid}"

def __create_session(self):
Expand Down
4 changes: 2 additions & 2 deletions swanlab/cli/commands/task/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ def __init__(self, ltm: ListTasksModel):
Layout(name="main")
)
self.layout["main"].split_row(
Layout(name="task_table", ratio=4),
Layout(name="term_output", ratio=1)
Layout(name="task_table", ratio=16),
Layout(name="term_output", ratio=5)
)
self.layout["header"].update(ListTaskHeader())
self.layout["task_table"].update(Panel(ltm.table(), border_style="magenta"))
Expand Down
2 changes: 1 addition & 1 deletion swanlab/cli/commands/task/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ def search(cuid):
tm.finished_at is not None and console.print(f"[bold]Finished At:[/bold] {tm.finished_at}")
if tm.status == 'CRASHED':
console.print(f"[bold][red]Task Error[/red]:[/bold]\n")
console.print(Syntax(tm.msg, 'python', background_color="default"))
console.print(Syntax(tm.msg, 'python'))
print("") # 加一行空行,与开头一致
2 changes: 1 addition & 1 deletion swanlab/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swanlab",
"version": "0.3.15-alpha.3",
"version": "0.3.15-alpha.4",
"description": "",
"python": "true"
}
4 changes: 3 additions & 1 deletion swanlab/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ def __exit__(self, exc_type, exc_val, exc_tb):


def is_login() -> bool:
"""判断是否已经登录,与当前的host相关,与get_key不同,不考虑环境变量的因素
"""判断是否已经登录,与当前的host相关
如果环境变量中有api key,则认为已经登录
但不会检查key的有效性
FIXME 目前存在一些bug,此函数只能在未登录前判断,登录后判断会有一些bug
:return: 是否已经登录
"""
with LoginCheckContext() as checker:
Expand Down
14 changes: 14 additions & 0 deletions test/unit/api/auth/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
@Description:
测试登录
"""
import os
from swanlab.env import SwanLabEnv
from swanlab.api.auth.login import login_by_key, terminal_login, code_login
from swanlab.error import ValidationError
from swanlab.package import is_login
Expand Down Expand Up @@ -69,3 +71,15 @@ def test_code_login():
assert login_info.__str__() == "Login success"
with pytest.raises(ValidationError):
_ = code_login("wrong-key")


@pytest.mark.skipif(T.is_skip_cloud_test, reason="skip cloud test")
def test_code_login_task_runtime():
# task模式下不保存token
os.environ[SwanLabEnv.RUNTIME.value] = 'task'
login_info = code_login(T.API_KEY)
assert not login_info.is_fail
# 没有保存在本地
del os.environ[SwanLabEnv.API_KEY.value]
assert not is_login()

0 comments on commit f74e89b

Please sign in to comment.