-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathRun-app-zh.bat
More file actions
107 lines (92 loc) · 2.3 KB
/
Run-app-zh.bat
File metadata and controls
107 lines (92 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
@echo off
setlocal enabledelayedexpansion
:: 初始化变量
set count=0
set use_conda=0
:: 首先尝试使用 where python 查找 Python 环境
for /f "tokens=*" %%a in ('where python 2^>nul') do (
set /a count+=1
set "python!count!=%%a"
set "python_path_!count!=%%a"
)
:: 如果 where python 没有找到,尝试 conda env list
if !count! equ 0 (
echo 正在查找 conda 环境...
for /f "tokens=1* delims= " %%a in ('conda env list ^| findstr /r /v "^#"') do (
if not "%%a"=="" if not "%%a"=="base" (
set /a count+=1
set "python!count!=%%a"
set "python_path_!count!=%%b"
set use_conda=1
)
)
)
:: 如果没有找到任何 Python 环境
if !count! equ 0 (
echo 没有可用的 Python 环境
echo 请先安装 Python 或 Anaconda/Miniconda
pause
exit /b 1
)
:: 如果只有一个 Python 环境,直接使用
if !count! equ 1 (
if !use_conda! equ 1 (
echo 找到一个 conda 环境: !python1!
call conda activate "!python1!"
) else (
echo 找到一个 Python 环境: !python1!
set selected_python=!python1!
)
goto :start_server
)
:: 如果有多个环境,让用户选择
echo 找到多个环境,请选择一个:
echo.
for /l %%i in (1,1,!count!) do (
echo [%%i] !python%%i!
)
echo.
:select
set /p choice="请输入数字选择 python 运行环境 (1-!count!): "
if !choice! lss 1 (
echo 选择无效
goto :select
)
if !choice! gtr !count! (
echo 选择无效
goto :select
)
:: 处理用户选择
for /l %%i in (1,1,!count!) do (
if !choice! equ %%i (
if !use_conda! equ 1 (
echo 正在激活 conda 环境: !python%%i!
call conda activate "!python%%i!"
) else (
set selected_python=!python%%i!
)
)
)
:start_server
:: 如果要打开其他文件,可以修改这里
set "default_page=index.html"
set "HTTP_PORT=8000"
set "HTTP_IP=127.0.0.1"
set "server_url=http://%HTTP_IP%:%HTTP_PORT%/%default_page%"
echo.
echo ========================================
echo 本地 HTTP 服务已启动!
echo 访问地址: %server_url%
echo
echo 按 Ctrl+C 停止运行
echo ========================================
echo.
:: 先打开浏览器(服务器启动后会自动加载)
start "" "%server_url%"
:: 在当前窗口启动服务器
if !use_conda! equ 1 (
python -m http.server %HTTP_PORT% --bind %HTTP_IP%
) else (
"!selected_python!" -m http.server %HTTP_PORT% --bind %HTTP_IP%
)
pause