Repository: SwanHubX/SwanLab (language: python)

Issue:
[QUESTION] 发布版本时将包文件放在Release中
发布版本时可以将 python 的 `whl` 包和前端编译后的文件打一个压缩包放在 Release 中，方便直接下载。


Discussion / hints:
找了下，没看到能快速使用的 action
可以先本地打包，在 release 界面里面选择上传
[pypi](https://pypi.org/project/swanlab/#files)上有2个包，1个whl和1个tar.gz，是不是指的tar.gz？
> [pypi](https://pypi.org/project/swanlab/#files)上有2个包，1个whl和1个tar.gz，是不是指的tar.gz？

whl就可以了，前端也可以把编译后的打一个包
> 找了下，没看到能快速使用的 action 可以先本地打包，在 release 界面里面选择上传

可参考：
- https://github.com/SwanHubX/req-preprocess/blob/main/.github/workflows/release.yml
- https://github.com/softprops/action-gh-release

Fix patch:
diff --git a/.github/workflows/publish-to-pypi.yml b/.github/workflows/publish-to-pypi.yml
index 79f9bbae4..f9e72dcbb 100644
--- a/.github/workflows/publish-to-pypi.yml
+++ b/.github/workflows/publish-to-pypi.yml
@@ -18,6 +18,10 @@ jobs:
         with:
           python-version: "3.10"
 
+      - name: Checkout Version
+        run: |
+          export VERSION=$(echo $GITHUB_REF | sed -n 's/refs\/tags\/v//p')
+
       - name: Install Dependencies
         run: |
           pip install -r requirements.txt
@@ -30,14 +34,17 @@ jobs:
           TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
 
         run: |
-          python -m build
-          python -m twine upload dist/*
-
-      # - run: cp dist/*.whl .
-      # - name: Release
-      #   uses: softprops/action-gh-release@v1
-      #   if: startsWith(github.ref, 'refs/tags/')
-      #   with:
-      #     body: ${{ github.event.head_commit.message }}
-      #     files: |
-      #       *.whl
+          python -m script/build_pypi.py
+      #          python -m twine upload dist/*
+
+      - run: cp dist/*.whl .
+
+      - run: cat swanlab/package.json
+
+      - name: Release
+        uses: softprops/action-gh-release@v2
+        if: startsWith(github.ref, 'refs/tags/')
+        with:
+          body: ${{ github.event.head_commit.message }}
+          files: |
+            *.whl
diff --git a/.vscode/launch.json b/.vscode/launch.json
index 02238d78c..eff7c520f 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -70,7 +70,7 @@
       "name": "构建项目",
       "type": "debugpy",
       "request": "launch",
-      "program": "${workspaceFolder}/build_pypi.py",
+      "program": "${workspaceFolder}/script/build_pypi.py",
       "console": "integratedTerminal",
       "justMyCode": true,
       "cwd": "${workspaceFolder}"
diff --git a/build_pypi.py b/script/build_pypi.py
similarity index 50%
rename from build_pypi.py
rename to script/build_pypi.py
index 107d6af6d..f7d6f08fc 100644
--- a/build_pypi.py
+++ b/script/build_pypi.py
@@ -9,10 +9,21 @@
 """
 import subprocess
 import shutil
+import json
 import os
 
 # 如果dist文件夹存在则删除
-if os.path.exists("dist"):
-    shutil.rmtree("dist")
+if os.path.exists("../dist"):
+    shutil.rmtree("../dist")
+
+# 设置版本号
+version = os.getenv("VERSION")
+if not version:
+    raise ValueError("尚未指定构建版本号")
+with open("../swanlab/package.json", 'r+') as f:
+    p = json.load(f)
+    p["version"] = version
+    json.dump(p, f, indent=4)
+
 # 构建python项目
 subprocess.run("python -m build", shell=True)
diff --git a/swanlab/package.json b/swanlab/package.json
index da7a952d1..814c1fd62 100644
--- a/swanlab/package.json
+++ b/swanlab/package.json
@@ -1,6 +1,6 @@
 {
   "name": "swanlab",
-  "version": "0.3.16",
+  "version": "development",
   "description": "",
   "python": "true"
 }


Test patch:
diff --git a/script/test.py b/script/test.py
deleted file mode 100644
index 7a594ad11..000000000
--- a/script/test.py
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-r"""
-@DATE: 2024/4/3 16:41
-@File: test.py.py
-@IDE: pycharm
-@Description:
-    运行单元测试，运行时应该在当前项目根目录下运行
-"""
-import os
-import sys
-
-sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
-os.system("pytest test/unit")