-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
288 lines (242 loc) · 8.56 KB
/
app.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import json
import os
import subprocess
import platform
from flask import Flask, render_template, jsonify, request
app = Flask(__name__)
@app.route('/api/projects')
def get_projects():
try:
# 确保配置目录存在
config_dir = os.path.join(os.path.dirname(__file__), 'config')
if not os.path.exists(config_dir):
os.makedirs(config_dir)
config_file = os.path.join(config_dir, 'project_setting.json')
# 如果配置文件不存在,返回空列表
if not os.path.exists(config_file):
return jsonify({
'success': True,
'data': []
})
# 读取项目配置
with open(config_file, 'r', encoding='utf-8') as f:
projects = json.load(f)
return jsonify({
'success': True,
'data': projects
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
})
@app.route('/')
def index():
return render_template('index.html')
@app.route('/settings')
def settings():
return render_template('settings.html')
@app.route('/system-settings')
def system_settings():
return render_template('system_settings.html')
@app.route('/add_card')
def add_card():
return render_template('add_card.html')
@app.route('/api/save_card', methods=['POST'])
def save_card():
try:
data = request.get_json()
# 确保配置目录存在
config_dir = os.path.join(os.path.dirname(__file__), 'config')
if not os.path.exists(config_dir):
os.makedirs(config_dir)
config_file = os.path.join(config_dir, 'project_setting.json')
# 读取现有配置
projects = []
if os.path.exists(config_file):
with open(config_file, 'r', encoding='utf-8') as f:
projects = json.load(f)
# 检查项目名称是否已存在
if any(project['name'] == data['name'] for project in projects):
return jsonify({
'success': False,
'error': '项目名称已存在'
})
# 添加新项目
projects.append({
'name': data['name']
})
# 保存更新后的配置
with open(config_file, 'w', encoding='utf-8') as f:
json.dump(projects, f, ensure_ascii=False, indent=2)
return jsonify({
'success': True,
'message': '保存成功'
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
})
@app.route('/api/system_config', methods=['GET', 'POST'])
def system_config():
config_file = os.path.join(os.path.dirname(__file__), 'config', 'system_config.json')
if request.method == 'GET':
try:
if os.path.exists(config_file):
with open(config_file, 'r', encoding='utf-8') as f:
config = json.load(f)
else:
config = {
'project_path': '',
'proxy_address': ''
}
return jsonify({
'success': True,
'data': config
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
})
elif request.method == 'POST':
try:
data = request.get_json()
# 确保配置目录存在
config_dir = os.path.dirname(config_file)
if not os.path.exists(config_dir):
os.makedirs(config_dir)
# 保存配置
with open(config_file, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
return jsonify({
'success': True,
'message': '保存成功'
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
})
@app.route('/project-settings')
def project_settings():
project_name = request.args.get('name')
return render_template('project_settings.html', project_name=project_name)
@app.route('/api/project_config', methods=['GET', 'POST'])
def project_config():
config_file = os.path.join(os.path.dirname(__file__), 'config', 'project_setting.json')
if request.method == 'GET':
try:
project_name = request.args.get('name')
if os.path.exists(config_file):
with open(config_file, 'r', encoding='utf-8') as f:
projects = json.load(f)
project = next((p for p in projects if p['name'] == project_name), None)
if project:
return jsonify({
'success': True,
'data': project
})
return jsonify({
'success': True,
'data': {'name': project_name}
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
})
elif request.method == 'POST':
try:
data = request.get_json()
if os.path.exists(config_file):
with open(config_file, 'r', encoding='utf-8') as f:
projects = json.load(f)
else:
projects = []
project_index = next((i for i, p in enumerate(projects) if p['name'] == data['name']), None)
if project_index is not None:
projects[project_index].update(data)
else:
projects.append(data)
with open(config_file, 'w', encoding='utf-8') as f:
json.dump(projects, f, ensure_ascii=False, indent=2)
return jsonify({
'success': True,
'message': '保存成功'
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
})
@app.route('/api/execute_conda_command', methods=['POST'])
def execute_conda_command():
try:
data = request.get_json()
conda_env = data.get('conda_env')
python_version = data.get('python_version')
source = data.get('source')
if not all([conda_env, python_version, source]):
return jsonify({
'success': False,
'error': '缺少必要参数'
})
# 构建命令
if source == 'china':
command = f'conda create -n {conda_env} python={python_version} -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ -y'
else:
command = f'conda create -n {conda_env} python={python_version} -y'
# 执行命令
process = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = process.communicate()
if process.returncode == 0:
return jsonify({
'success': True,
'message': '环境创建成功',
'output': stdout
})
else:
return jsonify({
'success': False,
'error': f'创建失败: {stderr}'
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
})
@app.route('/api/check_platform')
def check_platform():
return jsonify({
'platform': 'windows' if platform.system().lower() == 'windows' else 'linux'
})
@app.route('/api/open_cmd', methods=['POST'])
def open_cmd():
try:
if platform.system().lower() == 'windows':
# 打开新的CMD窗口
subprocess.Popen('start cmd', shell=True)
return jsonify({
'success': True,
'message': 'CMD窗口已打开'
})
else:
return jsonify({
'success': False,
'error': '不支持的操作系统'
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=1000, debug=True)