feat: 启动开发服务器时自动打开浏览器
- 服务器成功启动后自动打开 Tasks 页面 - 开发服务器已运行时重新打开对应页面 - 无法自动打开浏览器时显示手动访问地址
This commit is contained in:
@@ -6,6 +6,7 @@ import argparse
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import webbrowser
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
ROOT = Path(__file__).resolve().parent
|
ROOT = Path(__file__).resolve().parent
|
||||||
@@ -82,6 +83,12 @@ def server_url(port: int) -> str:
|
|||||||
return f"http://localhost:{port}/"
|
return f"http://localhost:{port}/"
|
||||||
|
|
||||||
|
|
||||||
|
def open_browser(port: int) -> None:
|
||||||
|
url = server_url(port)
|
||||||
|
if not webbrowser.open(url):
|
||||||
|
print(f"无法自动打开浏览器,请手动访问:{url}")
|
||||||
|
|
||||||
|
|
||||||
def start(port: int) -> None:
|
def start(port: int) -> None:
|
||||||
if not validate_project():
|
if not validate_project():
|
||||||
return
|
return
|
||||||
@@ -90,6 +97,7 @@ def start(port: int) -> None:
|
|||||||
if listener:
|
if listener:
|
||||||
if is_tasks_server(listener, port):
|
if is_tasks_server(listener, port):
|
||||||
print(f"Tasks 开发服务器已运行:{server_url(port)}(PID {listener})")
|
print(f"Tasks 开发服务器已运行:{server_url(port)}(PID {listener})")
|
||||||
|
open_browser(port)
|
||||||
return
|
return
|
||||||
|
|
||||||
answer = input(
|
answer = input(
|
||||||
@@ -127,6 +135,7 @@ def start(port: int) -> None:
|
|||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
if listener_pid(port) == process.pid:
|
if listener_pid(port) == process.pid:
|
||||||
print(f"Tasks 开发服务器已启动:{server_url(port)}(PID {process.pid})")
|
print(f"Tasks 开发服务器已启动:{server_url(port)}(PID {process.pid})")
|
||||||
|
open_browser(port)
|
||||||
else:
|
else:
|
||||||
print("服务启动失败,请确认 Python 可用且端口未被占用。")
|
print("服务启动失败,请确认 Python 可用且端口未被占用。")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user