Skip to content

Commit 676e1bd

Browse files
authored
refactor: tidy swankit (#1111)
1 parent 592ae42 commit 676e1bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+402
-247
lines changed

docs/插件化设计.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ swanlab一切的起点都是`swanlab.init`,此函数允许传入`callbacks`参
2525
`SwanKitCallback`,类似下面这样:
2626

2727
```python
28-
from swankit.callback import SwanKitCallback
28+
from swanlab.toolkit import SwanKitCallback
2929

3030

3131
class CustomCallback(SwanKitCallback):
@@ -41,7 +41,7 @@ class CustomCallback(SwanKitCallback):
4141

4242
```python
4343
import swanlab
44-
from swankit.callback import SwanKitCallback
44+
from swanlab.toolkit import SwanKitCallback
4545

4646

4747
class CustomCallback(SwanKitCallback):

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ typing_extensions; python_version < '3.9'
1515
protobuf>=3.12.0,!=4.21.0,!=5.28.0,<7; python_version < '3.9' and sys_platform == 'linux'
1616
protobuf>=3.15.0,!=4.21.0,!=5.28.0,<7; python_version == '3.9' and sys_platform == 'linux'
1717
protobuf>=3.19.0,!=4.21.0,!=5.28.0,<7; python_version > '3.9' and sys_platform == 'linux'
18-
protobuf>=3.19.0,!=4.21.0,!=5.28.0,<7; sys_platform != 'linux'
18+
protobuf>=3.19.0,!=4.21.0,!=5.28.0,<7; sys_platform != 'linux'
19+
rich>=13.6.0, <14.0.0

swanlab/cli/commands/auth/login.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
import os
1111

1212
import click
13-
from swankit.log import FONT
13+
from rich.text import Text
1414

1515
from swanlab.core_python import auth
1616
from swanlab.env import SwanLabEnv
17+
from swanlab.log import swanlog
1718
from swanlab.package import has_api_key, HostFormatter
1819

1920

@@ -51,9 +52,12 @@ def login(api_key: str, relogin: bool, host: str, web_host: str):
5152
"""Login to the swanlab cloud."""
5253
if not relogin and has_api_key():
5354
# 此时代表token已经获取,需要打印一条信息:已经登录
54-
command = FONT.bold("swanlab login --relogin")
55-
tip = FONT.swanlab("You are already logged in. Use `" + command + "` to force relogin.")
56-
return print(tip)
55+
return swanlog.info(
56+
"You are already logged in. Use `",
57+
Text("swanlab login --relogin", "bold"),
58+
"` to force relogin.",
59+
sep='',
60+
)
5761
# 清除环境变量
5862
if relogin:
5963
del os.environ[SwanLabEnv.API_HOST.value]
@@ -64,4 +68,4 @@ def login(api_key: str, relogin: bool, host: str, web_host: str):
6468
raise click.BadParameter(str(e))
6569
# 进行登录,此时将直接覆盖本地token文件
6670
login_info = auth.terminal_login(api_key)
67-
print(FONT.swanlab("Login successfully. Hi, " + FONT.bold(FONT.default(login_info.username))) + "!")
71+
swanlog.info("Login successfully. Hi, ", Text(login_info.username, "bold"), "!", sep='')

swanlab/cli/commands/auth/logout.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,30 @@
1111
import sys
1212

1313
import click
14-
from swankit.log import FONT
14+
from rich.text import Text
1515

1616
from swanlab.env import get_save_dir
17+
from swanlab.log import swanlog
1718
from swanlab.package import has_api_key
1819

1920

2021
@click.command()
2122
def logout():
2223
"""Logout to the swanlab cloud."""
23-
command = FONT.bold("swanlab login")
24+
command = Text("swanlab login", "bold")
2425
if has_api_key():
2526
# 如果已经是登录状态,那么则询问用户是否确认,如果确认则删除token文件夹
26-
confirm = input(FONT.swanlab("Are you sure you want to logout? (y/N): "))
27+
28+
swanlog.info("Are you sure you want to logout? (y/N): ", end='')
29+
confirm = input()
2730
if confirm.lower() == "y":
2831
try:
2932
shutil.rmtree(get_save_dir())
30-
return print(FONT.swanlab("Logout successfully. You can use `" + command + "` to login again."))
33+
return swanlog.info(" Logout successfully. You can use `", command, "` to login again.", sep="")
3134
except Exception as e:
32-
return print(FONT.swanlab("Logout failed. Reason:" + str(e)))
35+
return swanlog.info("Logout failed. Reason:" + str(e))
3336
else:
34-
return print(FONT.swanlab("Logout canceled."))
37+
return swanlog.info("Logout canceled.")
3538
# 如果还未登录,则不做任何处理,并告知用户如何登录
36-
tip = FONT.swanlab("You are not logged in. If you want to login in, please use `" + command + "` to login.")
37-
print(tip)
39+
swanlog.info("You are not logged in. If you want to login in, please use `", command, "` to login.", sep="")
3840
return sys.exit(1)

swanlab/core_python/auth/providers/api_key.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
from typing import Union
1111

1212
import requests
13-
from swankit.env import is_windows
14-
from swankit.log import FONT
13+
from rich.status import Status
14+
from rich.text import Text
1515

16-
from swanlab.env import in_jupyter
16+
from swanlab.env import is_windows
1717
from swanlab.error import ValidationError, APIKeyFormatError
1818
from swanlab.log import swanlog
1919
from swanlab.package import get_setting_url, get_host_api, get_host_web, fmt_web_host, save_key as sk
@@ -143,19 +143,21 @@ def input_api_key(
143143
_t = sys.excepthook
144144
sys.excepthook = _abort_tip
145145
if not again:
146-
swanlog.info("You can find your API key at: " + FONT.yellow(get_setting_url()))
146+
swanlog.info("You can find your API key at:", Text(get_setting_url(), "yellow"))
147+
swanlog.info(tip, end='')
147148
# windows 额外打印提示信息
148149
if is_windows():
149-
tip += (
150-
'\nOn Windows, '
151-
f'use {FONT.yellow("Ctrl + Shift + V")} or {FONT.yellow("right-click")} '
152-
f'to paste the API key'
150+
swanlog.console.print(
151+
'\nOn Windows, ',
152+
'use',
153+
Text("Ctrl + Shift + V", 'yellow'),
154+
'or',
155+
Text("right-click", 'yellow'),
156+
'to paste the API key',
157+
end='',
153158
)
154-
tip += ': '
155-
tip = FONT.swanlab(tip)
156-
ij = in_jupyter()
157-
ij and print(tip)
158-
key = getpass.getpass("" if ij else tip)
159+
swanlog.console.print(': ', end='')
160+
key = getpass.getpass("")
159161
sys.excepthook = _t
160162
return key
161163

@@ -170,8 +172,8 @@ def code_login(api_key: str, save_key: bool = True) -> LoginInfo:
170172
:raise APIKeyFormatError: api_key格式错误
171173
"""
172174
APIKeyFormatError.check(api_key)
173-
tip = "Waiting for the swanlab cloud response."
174-
login_info: LoginInfo = FONT.loading(tip, login_by_key, args=(api_key, 20, save_key), interval=0.5)
175+
with Status("Waiting for the swanlab cloud response.", spinner="dots"):
176+
login_info = login_by_key(api_key, 20, save_key)
175177
if login_info.is_fail:
176178
raise ValidationError("Login failed: " + str(login_info))
177179
return login_info

swanlab/core_python/client/__init__.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
import requests
1313
from requests.adapters import HTTPAdapter
14-
from swankit.core import MediaBuffer
15-
from swankit.log import FONT
14+
from rich.status import Status
1615
from urllib3.util.retry import Retry
1716

1817
from swanlab.error import NetworkError, ApiError
1918
from swanlab.log import swanlog
2019
from swanlab.package import get_package_version
20+
from swanlab.toolkit import MediaBuffer
2121
from .cos import CosClient
2222
from .model import ProjectInfo, ExperimentInfo
2323
from .. import auth
@@ -255,8 +255,7 @@ def mount_project(self, name: str, username: str = None, public: bool = None):
255255
:param public: 项目是否公开
256256
:return: 项目信息
257257
"""
258-
259-
def _():
258+
with Status("Getting project...", spinner="dots"):
260259
try:
261260
data = {"name": name}
262261
if username is not None:
@@ -294,9 +293,7 @@ def _():
294293
self.__groupname = resp['username']
295294
# 获取详细信息
296295
resp = self.get(f"/project/{self.groupname}/{name}")
297-
return ProjectInfo(resp)
298-
299-
project: ProjectInfo = FONT.loading("Getting project...", _)
296+
project = ProjectInfo(resp)
300297
self.__proj = project
301298

302299
def mount_exp(self, exp_name, colors: Tuple[str, str], description: str = None, tags: List[str] = None):
@@ -307,11 +304,7 @@ def mount_exp(self, exp_name, colors: Tuple[str, str], description: str = None,
307304
:param description: 实验描述
308305
:param tags: 实验标签
309306
"""
310-
311-
def _():
312-
"""
313-
先创建实验,后生成cos凭证
314-
"""
307+
with Status("Getting experiment...", spinner="dots"):
315308
post_data = {
316309
"name": exp_name,
317310
"colors": list(colors),
@@ -326,21 +319,15 @@ def _():
326319
# 获取cos信息
327320
self.__get_cos()
328321

329-
FONT.loading("Creating experiment...", _)
330-
331322
def update_state(self, success: bool):
332323
"""
333324
更新实验状态
334325
:param success: 实验是否成功
335326
"""
336-
337-
def _():
338-
self.put(
339-
f"/project/{self.groupname}/{self.projname}/runs/{self.exp_id}/state",
340-
{"state": "FINISHED" if success else "CRASHED", "from": "sdk"},
341-
)
342-
343-
FONT.loading("Updating experiment status...", _)
327+
self.put(
328+
f"/project/{self.groupname}/{self.projname}/runs/{self.exp_id}/state",
329+
{"state": "FINISHED" if success else "CRASHED", "from": "sdk"},
330+
)
344331

345332

346333
client: Optional["Client"] = None

swanlab/core_python/uploader/model.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
from enum import Enum
1111
from typing import List, Optional, TypedDict, Literal
1212

13-
from swankit.callback.models import ColumnClass, ColumnConfig
14-
1513
from swanlab.data.modules import MediaBuffer
14+
from swanlab.toolkit import ColumnClass, ColumnConfig
1615

1716

1817
class ColumnModel:

swanlab/data/callbacker/cloud.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import sys
1010
from typing import Literal
1111

12-
from swankit.callback.models import RuntimeInfo, MetricInfo, ColumnInfo
13-
from swankit.env import create_time
14-
from swankit.log import FONT
12+
from rich.text import Text
1513

1614
from swanlab.env import in_jupyter, is_interactive
1715
from swanlab.error import KeyFileError
@@ -21,6 +19,12 @@
2119
get_package_latest_version,
2220
get_key,
2321
)
22+
from swanlab.toolkit import (
23+
RuntimeInfo,
24+
MetricInfo,
25+
ColumnInfo,
26+
create_time,
27+
)
2428
from ..run import get_run, SwanLabRunState
2529
from ..run.callback import SwanLabRunCallback
2630
from ...core_python import *
@@ -71,8 +75,8 @@ def _get_package_latest_version():
7175
def _view_web_print():
7276
http = get_client()
7377
proj_url, exp_url = http.web_proj_url, http.web_exp_url
74-
swanlog.info("🏠 View project at " + FONT.blue(FONT.underline(proj_url)))
75-
swanlog.info("🚀 View run at " + FONT.blue(FONT.underline(exp_url)))
78+
swanlog.info("🏠 View project at", Text(proj_url, "blue underline"))
79+
swanlog.info("🚀 View run at", Text(exp_url, "blue underline"))
7680
return exp_url
7781

7882
def _clean_handler(self):
@@ -129,8 +133,9 @@ def on_run(self, *args, **kwargs):
129133
# 打印实验开始信息,在 cloud 模式下如果没有开启 backup 的话不打印“数据保存在 xxx”的信息
130134
swanlab_settings = get_settings()
131135
self._train_begin_print(save_dir=self.settings.run_dir if swanlab_settings.backup else None)
132-
swanlog.info("👋 Hi " + FONT.bold(FONT.default(http.username)) + ", welcome to swanlab!")
133-
swanlog.info("Syncing run " + FONT.yellow(self.settings.exp_name) + " to the cloud")
136+
137+
swanlog.info(" 👋 Hi ", Text(http.username, "bold default"), ",welcome to swanlab!", sep="")
138+
swanlog.info("Syncing run", Text(self.settings.exp_name, "yellow"), "to the cloud")
134139
experiment_url = self._view_web_print()
135140
# 在Jupyter Notebook环境下,显示按钮
136141
if in_jupyter():

swanlab/data/callbacker/local.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
local模式(目前)将自动调用swanboard,如果不存在则报错
77
"""
88

9-
from swankit.callback import ColumnInfo
10-
from swankit.log import FONT
9+
from rich.text import Text
1110

1211
from swanlab.log.backup import backup
1312
from swanlab.log.backup.writer import write_media_buffer, write_runtime_info
1413
from swanlab.log.type import LogData
14+
from swanlab.toolkit import ColumnInfo
1515
from ..run import get_run
1616

1717
try:
@@ -34,9 +34,7 @@
3434
from datetime import datetime
3535
from typing import Tuple, Optional, TextIO
3636

37-
from swankit.callback.models import RuntimeInfo, MetricInfo
38-
from swankit.core import SwanLabSharedSettings
39-
37+
from swanlab.toolkit import RuntimeInfo, MetricInfo, SwanLabSharedSettings
4038
from swanlab.data.run.callback import SwanLabRunCallback
4139
from swanlab.log import swanlog
4240

@@ -57,9 +55,10 @@ def _watch_tip_print(self):
5755
watch命令提示打印
5856
"""
5957
swanlog.info(
60-
"🌟 Run `"
61-
+ FONT.bold("swanlab watch {}".format(self.fmt_windows_path(self.settings.swanlog_dir)))
62-
+ "` to view SwanLab Experiment Dashboard locally"
58+
" 🌟 Run `",
59+
Text("swanlab watch {}".format(self.fmt_windows_path(self.settings.swanlog_dir)), "bold"),
60+
"` to view SwanLab Experiment Dashboard locally",
61+
sep="",
6362
)
6463

6564
@backup("terminal")

swanlab/data/callbacker/offline.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
@description: 日志备份回调
66
"""
77

8-
from swankit.callback import ColumnInfo, MetricInfo, RuntimeInfo
9-
from swankit.log import FONT
8+
from rich.text import Text
109

1110
from swanlab.data.run.callback import SwanLabRunCallback
1211
from swanlab.log import swanlog
1312
from swanlab.log.backup import backup
1413
from swanlab.log.type import LogData
14+
from swanlab.toolkit import ColumnInfo, MetricInfo, RuntimeInfo
1515
from ..run import get_run
1616

1717

@@ -28,9 +28,10 @@ def _sync_tip_print(self):
2828
提示用户可以通过命令上传日志到远程服务器
2929
"""
3030
swanlog.info(
31-
"☁️ Run `"
32-
+ FONT.bold("swanlab sync {}".format(self.fmt_windows_path(self.settings.run_dir)))
33-
+ "` to sync logs to remote server"
31+
" ☁️ Run `",
32+
Text("swanlab sync {}".format(self.fmt_windows_path(self.settings.run_dir))),
33+
"` to sync logs to remote server",
34+
sep="",
3435
)
3536

3637
@backup("terminal")
@@ -51,7 +52,7 @@ def on_init(self, proj_name: str, workspace: str, public: bool = None, logdir: s
5152
def on_run(self, *args, **kwargs):
5253
self.handle_run()
5354
self._train_begin_print(self.settings.run_dir)
54-
swanlog.info("Backing up run " + FONT.yellow(self.settings.exp_name) + " locally")
55+
swanlog.info("Backing up run", Text(self.settings.exp_name, "yellow"), "locally")
5556
self._sync_tip_print()
5657

5758
@backup('runtime')

0 commit comments

Comments
 (0)