-
Notifications
You must be signed in to change notification settings - Fork 184
feat: replace the workspace field in the project object with a workspace object #1429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dca98b1
974ee4f
e84f819
6480312
126e29c
5325e28
943f227
c820aab
fc07300
c43a8a4
bfc0333
6e32ba1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,15 +1,141 @@ | ||||||
| #!/usr/bin/env python | ||||||
| # -*- coding: utf-8 -*- | ||||||
| r""" | ||||||
| @DATE: 2025/4/29 9:40 | ||||||
| @File: __init__.py | ||||||
| @IDE: pycharm | ||||||
| @Description: | ||||||
| SwanLab OpenAPI包 | ||||||
| """ | ||||||
| @author: Zhou QiYang | ||||||
| @file: __init__.py | ||||||
| @time: 2026/1/5 17:58 | ||||||
| @description: SwanLab OpenAPI包 | ||||||
| """ | ||||||
|
|
||||||
| from typing import Optional, List, Dict | ||||||
|
|
||||||
| from swanlab.core_python import auth, Client | ||||||
| from swanlab.core_python.api.experiment import get_single_experiment, get_project_experiments | ||||||
| from swanlab.error import KeyFileError | ||||||
| from swanlab.log import swanlog | ||||||
| from swanlab.package import HostFormatter, get_key | ||||||
| from .deprecated import OpenApi | ||||||
| from .experiment import Experiment | ||||||
| from .experiments import Experiments | ||||||
| from .projects import Projects | ||||||
| from .user import User | ||||||
| from .workspace import Workspace | ||||||
| from .workspaces import Workspaces | ||||||
|
|
||||||
|
|
||||||
| class Api: | ||||||
| def __init__(self, api_key: Optional[str] = None, host: Optional[str] = None, web_host: Optional[str] = None): | ||||||
| """ | ||||||
| 初始化 OpenApi 实例,用户需提前登录,或者提供API密钥 | ||||||
| :param api_key: API 密钥,可选 | ||||||
| :param host: API 主机地址,可选 | ||||||
| :param web_host: Web 主机地址,可选 | ||||||
| """ | ||||||
| if host or web_host: | ||||||
| HostFormatter(host, web_host)() | ||||||
| if api_key: | ||||||
| swanlog.debug("Using API key", api_key) | ||||||
| else: | ||||||
| swanlog.debug("Using existing key") | ||||||
| try: | ||||||
| api_key = get_key() | ||||||
| except KeyFileError as e: | ||||||
| swanlog.error("To use SwanLab OpenAPI, please login first.") | ||||||
| raise RuntimeError("Not logged in.") from e | ||||||
|
|
||||||
| self._login_info = auth.code_login(api_key, save_key=False) | ||||||
| # 一个OpenApi对应一个client,可创建多个api获取从不同的client获取不同账号下的实验信息 | ||||||
| self._client: Client = Client(self._login_info) | ||||||
| self._web_host = self._login_info.web_host | ||||||
| self._login_user = self._login_info.username | ||||||
|
|
||||||
| def user(self, username: str = None) -> User: | ||||||
| """ | ||||||
| 获取用户实例,用于操作用户相关信息 | ||||||
| :param username: 指定用户名,如果为 None,则返回当前登录用户 | ||||||
| :return: User 实例,可对当前/指定用户进行操作 | ||||||
| """ | ||||||
| return User(client=self._client, login_user=self._login_user, username=username) | ||||||
|
|
||||||
| def projects( | ||||||
| self, | ||||||
| workspace: str, | ||||||
| sort: Optional[List[str]] = None, | ||||||
| search: Optional[str] = None, | ||||||
| detail: Optional[bool] = True, | ||||||
| ) -> Projects: | ||||||
| """ | ||||||
| 获取指定工作空间(组织)下的所有项目信息 | ||||||
| :param workspace: 工作空间(组织)名称 | ||||||
| :param sort: 排序方式,可选 | ||||||
| :param search: 搜索关键词,可选 | ||||||
| :param detail: 是否返回详细信息,可选 | ||||||
| :return: Projects 实例,可遍历获取项目信息 | ||||||
| """ | ||||||
| return Projects( | ||||||
| self._client, | ||||||
| web_host=self._web_host, | ||||||
| workspace=workspace, | ||||||
| sort=sort, | ||||||
| search=search, | ||||||
| detail=detail, | ||||||
| ) | ||||||
|
|
||||||
| def runs(self, path: str, filters: Dict[str, object] = None) -> Experiments: | ||||||
| """ | ||||||
| 获取指定项目下的所有实验信息 | ||||||
| :param path: 项目路径,格式为 'username/project' | ||||||
| :return: Experiments 实例,可遍历获取实验信息 | ||||||
| :param filters: 筛选实验的条件,可选 | ||||||
| """ | ||||||
| return Experiments(self._client, path=path, login_info=self._login_info, filters=filters) | ||||||
|
|
||||||
| def run( | ||||||
| self, | ||||||
| path: str, | ||||||
| ) -> Experiment: | ||||||
| """ | ||||||
| 获取指定实验的信息 | ||||||
| :param path: 实验路径,格式为 'username/project/run_id' | ||||||
| :return: Experiment 实例,包含实验信息 | ||||||
| """ | ||||||
| # TODO: 待后端完善后替换成专用的接口 | ||||||
| if len(path.split('/')) != 3: | ||||||
| raise ValueError(f"User's {path} is invaded. Correct path should be like 'username/project/run_id'") | ||||||
| _data = get_single_experiment(self._client, path=path) | ||||||
| proj_path = path.rsplit('/', 1)[0] | ||||||
| data = get_project_experiments( | ||||||
| self._client, path=proj_path, filters={'name': _data['name'], 'created_at': _data['createdAt']} | ||||||
| ) | ||||||
| return Experiment( | ||||||
| self._client, | ||||||
| data=data[0], | ||||||
| path=proj_path, | ||||||
| web_host=self._web_host, | ||||||
| login_user=self._login_user, | ||||||
| line_count=1, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
| ) | ||||||
|
|
||||||
| def workspaces( | ||||||
| self, | ||||||
| username: str = None, | ||||||
| ): | ||||||
| """ | ||||||
| 获取当前登录用户的工作空间迭代器 | ||||||
| 当username为其他用户时,可以作为visitor访问其工作空间 | ||||||
| """ | ||||||
| if username is None: | ||||||
| username = self._login_user | ||||||
| return Workspaces(self._client, username=username) | ||||||
|
|
||||||
| def workspace( | ||||||
| self, | ||||||
| username: str = None, | ||||||
| ): | ||||||
| """ | ||||||
| 获取当前登录用户的工作空间 | ||||||
| """ | ||||||
| if username is None: | ||||||
| username = self._login_user | ||||||
| return Workspace(client=self._client, workspace=username) | ||||||
|
|
||||||
| from swanlab.api.main import OpenApi | ||||||
|
|
||||||
| __all__ = [ | ||||||
| "OpenApi" | ||||||
| ] | ||||||
| __all__ = ["Api", "OpenApi"] | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| """ | ||
| @author: Zhou QiYang | ||
| @file: __init__.py | ||
| @time: 2025/12/30 20:54 | ||
| @description: 旧版OpenApi,即将遗弃 | ||
| """ | ||
|
|
||
| from .main import OpenApi | ||
|
|
||
| __all__ = ["OpenApi"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
TODOcomment indicates that the backend needs to be improved for therunmethod. This suggests a potential future refactoring or an incomplete implementation that might affect the reliability or functionality of this method. It's important to address suchTODOs to ensure the API is fully robust.