使用Python开发在线编辑器!

使用Python开发在线编辑器!

 作者:mosquito_lover1
这篇文章主要为大家详细介绍了如何使用Python开发一个在线编辑器,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解一下。

实现效果

2025020510183110

完整代码

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
289
290
291
292
293
294
295
296
297
298
299
300
301
from flask import Flask, render_template, request, jsonify
import sys
from io import StringIO
import contextlib
import subprocess
import importlib
import threading
import time
import ast
import re
app = Flask(__name__)
RESTRICTED_PACKAGES = {
'tkinter': '抱歉,在线编译器不支持 tkinter,因为它需要图形界面环境。请在本地运行需要GUI的代码。',
'tk': '抱歉,在线编译器不支持 tk/tkinter,因为它需要图形界面环境。请在本地运行需要GUI的代码。',
'pygame': 'pygame将被转换为Web版本运行'  # 不再限制pygame,而是转换它
}
def convert_tkinter_to_web(code):
"""将tkinter代码转换为Web等效实现"""
# 解析Python代码
tree = ast.parse(code)
# 提取窗口属性
window_props = {
'title': 'Python GUI',
'width': '700',
'height': '500',
'buttons': [],
'labels': [],
'entries': [],
'layout': []
}
# 用于存储函数定义
functions = {}
# 首先收集所有函数定义
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
functions[node.name] = ast.unparse(node)
# 分析代码中的tkinter组件
for node in ast.walk(tree):
if isinstance(node, ast.Assign):
if isinstance(node.value, ast.Call):
# 提取窗口标题
if hasattr(node.value.func, 'attr') and node.value.func.attr == 'Tk':
for subnode in ast.walk(tree):
if isinstance(subnode, ast.Call) and hasattr(subnode.func, 'attr'):
if subnode.func.attr == 'title' and len(subnode.args) > 0:
window_props['title'] = ast.literal_eval(subnode.args[0])
elif subnode.func.attr == 'geometry' and len(subnode.args) > 0:
geom = ast.literal_eval(subnode.args[0])
match = re.match(r'(\d+)x(\d+)', geom)
if match:
window_props['width'] = match.group(1)
window_props['height'] = match.group(2)
# 提取按钮
elif hasattr(node.value.func, 'attr') and node.value.func.attr == 'Button':
button = {'text': 'Button', 'command': None}
for kw in node.value.keywords:
if kw.arg == 'text':
button['text'] = ast.literal_eval(kw.value)
elif kw.arg == 'command':
# 处理不同类型的command
if isinstance(kw.value, ast.Name):
# 简单的函数名
button['command'] = kw.value.id
elif isinstance(kw.value, ast.Lambda):
# Lambda表达式
button['command'] = f"lambda_{len(window_props['buttons'])}"
functions[button['command']] = ast.unparse(kw.value)
else:
# 其他情况,尝试转换为字符串
try:
button['command'] = ast.unparse(kw.value)
except:
button['command'] = 'unknown_command'
window_props['buttons'].append(button)
# 提取标签
elif hasattr(node.value.func, 'attr') and node.value.func.attr == 'Label':
label = {'text': ''}
for kw in node.value.keywords:
if kw.arg == 'text':
try:
label['text'] = ast.literal_eval(kw.value)
except:
# 如果不是字面量,尝试直接转换为字符串
label['text'] = ast.unparse(kw.value)
window_props['labels'].append(label)
# 提取输入框
elif hasattr(node.value.func, 'attr') and node.value.func.attr == 'Entry':
try:
entry_id = node.targets[0].id
except:
entry_id = f"entry_{len(window_props['entries'])}"
window_props['entries'].append({'id': entry_id})
# 生成Web等效代码
web_code = f"""
<!DOCTYPE html>
<div class="tk-window" style="width: {window_props['width']}px; height: {window_props['height']}px;">
<div class="tk-title-bar">{window_props['title']}</div>
<div class="tk-content">
"""
# 添加标签
for label in window_props['labels']:
web_code += f'        <div class="tk-label">{label["text"]}</div>\n'
# 添加输入框
for entry in window_props['entries']:
web_code += f'        <input type="text" class="tk-entry" id="{entry["id"]}">\n'
# 添加按钮
for button in window_props['buttons']:
command = button['command'] if button['command'] else ''
web_code += f'        <button class="tk-button" onclick="tkButtonClick(\'{command}\')">{button["text"]}</button>\n'
web_code += """    </div>
</div>
<script>
window.pythonFunctions = {
"""
# 添加Python函数定义
for func_name, func_code in functions.items():
web_code += f"    '{func_name}': {func_code},\n"
web_code += """};
</script>
"""
return web_code
def convert_pygame_to_web(code):
"""将pygame代码转换为Web Canvas实现"""
web_code = """
<canvas id="pygame-canvas" style="border: 1px solid #000;"></canvas>
<script>
const canvas = document.getElementById('pygame-canvas');
const ctx = canvas.getContext('2d');
// 设置画布大小
canvas.width = 800;
canvas.height = 600;
// 模拟 pygame 的基本功能
const pygame = {
display: {
set_mode: (size) => {
canvas.width = size[0];
canvas.height = size[1];
return canvas;
},
update: () => {
// Canvas 自动更新
},
flip: () => {
// Canvas 自动更新
}
},
draw: {
rect: (surface, color, rect) => {
ctx.fillStyle = `rgb(${color[0]},${color[1]},${color[2]})`;
ctx.fillRect(rect[0], rect[1], rect[2], rect[3]);
},
circle: (surface, color, pos, radius) => {
ctx.beginPath();
ctx.fillStyle = `rgb(${color[0]},${color[1]},${color[2]})`;
ctx.arc(pos[0], pos[1], radius, 0, Math.PI * 2);
ctx.fill();
}
},
event: {
get: () => [],  // 简化的事件处理
pump: () => {}
},
init: () => {},
quit: () => {},
time: {
Clock: function() {
return {
tick: (fps) => 1000/fps
};
}
}
};
// 转换后的Python代码
function runGame() {
try {
// 这里将插入转换后的游戏代码
%PYTHON_CODE%
} catch (error) {
console.error('Game error:', error);
}
}
// 启动游戏循环
runGame();
</script>
"""
# 处理 Python 代码
try:
tree = ast.parse(code)
# 转换 Python 代码为 JavaScript
js_code = convert_pygame_code_to_js(tree)
web_code = web_code.replace('%PYTHON_CODE%', js_code)
return web_code
except Exception as e:
return f"<div class='error'>转换错误: {str(e)}</div>"
def convert_pygame_code_to_js(tree):
"""将 Python AST 转换为 JavaScript 代码"""
js_code = []
for node in ast.walk(tree):
if isinstance(node, ast.Import):
continue  # 跳过导入语句
elif isinstance(node, ast.Assign):
# 转换赋值语句
if hasattr(node.value, 'func') and isinstance(node.value.func, ast.Attribute):
if node.value.func.attr == 'set_mode':
js_code.append(f"const screen = pygame.display.set_mode([{node.value.args[0].elts[0].n}, {node.value.args[0].elts[1].n}]);")
elif isinstance(node, ast.While):
# 转换游戏主循环
js_code.append("function gameLoop() {")
# ... 处理循环体
js_code.append("    requestAnimationFrame(gameLoop);")
js_code.append("}")
js_code.append("gameLoop();")
return "\n".join(js_code)
def install_package(package):
"""自动安装缺失的包"""
# 检查是否是受限制的包
if package.lower() in RESTRICTED_PACKAGES:
raise ImportError(RESTRICTED_PACKAGES[package.lower()])
try:
importlib.import_module(package)
except ImportError:
try:
# 尝试使用 pip 安装包
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
except subprocess.CalledProcessError as e:
raise Exception(f"安装包 {package} 失败: {str(e)}")
def timeout_handler():
"""强制终止超时的代码执行"""
raise TimeoutError("代码执行超时(最大执行时间:5秒)")
@app.route('/')
def index():
return render_template('index.html')
@app.route('/execute', methods=['POST'])
def execute_code():
code = request.json.get('code', '')
try:
# 检测是否包含pygame代码
if 'pygame' in code:
web_code = convert_pygame_to_web(code)
return jsonify({
'status': 'success',
'output': '',
'gui': web_code
})
# 检测是否包含tkinter代码
elif 'tkinter' in code or 'tk' in code:
web_code = convert_tkinter_to_web(code)
return jsonify({
'status': 'success',
'output': '',
'gui': web_code
})
# 非GUI代码正常执行
output_buffer = StringIO()
with contextlib.redirect_stdout(output_buffer):
exec(code, globals(), {})
output = output_buffer.getvalue()
return jsonify({
'status': 'success',
'output': output if output else '程序执行完成,没有输出'
})
except Exception as e:
return jsonify({
'status': 'error',
'output': f'错误: {str(e)}'
})
if __name__ == '__main__':
app.run(debug=True)

以上就是使用Python开发在线编辑器的详细内容。

 

 

学习资料见知识星球。

以上就是今天要分享的技巧,你学会了吗?若有什么问题,欢迎在下方留言。

快来试试吧,小琥 my21ke007。获取 1000个免费 Excel模板福利​​​​!

更多技巧, www.excelbook.cn

欢迎 加入 零售创新 知识星球,知识星球主要以数据分析、报告分享、数据工具讨论为主;

Excelbook.cn Excel技巧 SQL技巧 Python 学习!

你将获得:

1、价值上万元的专业的PPT报告模板。

2、专业案例分析和解读笔记。

3、实用的Excel、Word、PPT技巧。

4、VIP讨论群,共享资源。

5、优惠的会员商品。

6、一次付费只需129元,即可下载本站文章涉及的文件和软件。

文章版权声明 1、本网站名称:Excelbook
2、本站永久网址:http://www.excelbook.cn
3、本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长王小琥进行删除处理。
4、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报。
6、本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。

THE END
分享
二维码
< <上一篇
下一篇>>