Skip to content

Commit 113761b

Browse files
author
Marty
committed
test.html fixing...
1 parent 3eecc6b commit 113761b

File tree

2 files changed

+76
-19
lines changed

2 files changed

+76
-19
lines changed

prod/coms/wa-run-python.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ export class RunPython extends LitElement {
5050

5151
async runPythonCode(code) {
5252
try {
53-
var self = this;
5453
window.mqtt = MQTTAppV2;
5554
mqtt.disconnectAll();
56-
await this.pyodide.runPythonAsync(code);
55+
56+
let lines = code.split('\n');
57+
for (let line of lines) {
58+
line = line.trim();
59+
if (line) {
60+
await this.pyodide.runPythonAsync(line);
61+
}
62+
}
5763
return null;
5864
} catch (err) {
5965
if (this.pyodide && err.constructor.name === 'PythonError') {
@@ -76,26 +82,35 @@ export class RunPython extends LitElement {
7682
this.run = run;
7783

7884
function stdout_func(msg) {
79-
output.show(msg);
85+
output.show(msg + "\n");
8086
output.scrollBottom();
8187
}
8288

8389
function stderr_func(msg) {
84-
output.show(msg);
90+
output.show(msg + "\n");
8591
output.scrollBottom();
8692
}
8793

8894
function stdin_func() {
89-
var rtn = prompt("Python input() 请输入:") || "";
90-
console.log("rtn:", rtn);
95+
var rtn = prompt("请输入:") || "";
9196
return rtn;
9297
}
9398

9499
run.addEventListener("click", async function () {
95100
output.cls();
96101
var newCode = editor.getCode();
97-
//await self.runPythonCode(newCode);
98-
await pyodide.runPythonAsync(newCode);
102+
103+
await self.pyodide.runPython(`
104+
import sys
105+
from js import prompt
106+
107+
def custom_input(*args):
108+
return prompt("请输入:")
109+
110+
input = custom_input
111+
`);
112+
113+
await self.runPythonCode(newCode);
99114
});
100115

101116
let pyodide;
@@ -105,13 +120,12 @@ export class RunPython extends LitElement {
105120
stdout: stdout_func,
106121
stderr: stderr_func,
107122
});
123+
108124
run.style["color"] = "#eee";
109125
icon.style["fill"] = "#eee";
110126

111127
this.pyodide = pyodide;
112128
this.output = output;
113-
// Pyodide is now ready to use...
114-
// console.log("pyodide ready !");
115129
run.removeAttribute("disabled");
116130
}
117131

test.html

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,38 @@
3636
</head>
3737
<body>
3838
<h1>Pyodide Python 執行器</h1>
39-
<textarea id="python-code" placeholder="在此輸入您的 Python 程式碼,例如:&#10;print('Hello, Pyodide!')">print(input())</textarea>
39+
<textarea id="python-code" placeholder="在此輸入您的 Python 程式碼,例如:&#10;print('Hello, Pyodide!')">
40+
import random
41+
def guess_number_game():
42+
while True:
43+
# 顯示遊戲開始的提示
44+
print("遊戲開始,請從1到50中,隨意輸入一個數字。")
45+
46+
# 隨機生成1到50的數字
47+
correct_number = random.randint(1, 50)
48+
49+
while True:
50+
# 玩家輸入數字
51+
player_guess = int(input("請輸入你的猜測: "))
52+
53+
# 比較玩家的猜測與正確答案
54+
if player_guess > correct_number:
55+
print("再小一點")
56+
elif player_guess < correct_number:
57+
print("再大一點")
58+
else:
59+
print("恭喜你答對")
60+
break # 猜對了,跳出內部迴圈
61+
62+
# 詢問玩家是否要繼續遊戲
63+
continue_game = input("是否要繼續遊戲? (是/否): ")
64+
if continue_game.lower() != '是':
65+
print("感謝遊玩,遊戲結束!")
66+
break # 結束遊戲
67+
# 開始遊戲
68+
guess_number_game()
69+
70+
</textarea>
4071
<br>
4172
<button id="run-button">執行程式碼</button>
4273
<h2>輸出結果:</h2>
@@ -48,25 +79,37 @@ <h2>輸出結果:</h2>
4879
let pyodideReadyPromise = loadPyodide();
4980

5081
async function runPython() {
51-
// 等待 Pyodide 加載完成
5282
let pyodide = await pyodideReadyPromise;
5383
let code = document.getElementById("python-code").value;
5484
let outputElement = document.getElementById("output");
5585
outputElement.textContent = "";
5686

57-
// 捕捉 Python 的標準輸出
87+
// 修改输出处理,添加换行符
88+
let printOutput = (text) => {
89+
outputElement.textContent += text + "\n"; // 在每次输出后添加换行符
90+
outputElement.scrollTop = outputElement.scrollHeight;
91+
};
92+
5893
pyodide.setStdout({
59-
batched: (text) => {
60-
outputElement.textContent += text;
61-
}
94+
batched: printOutput
6295
});
6396
pyodide.setStderr({
64-
batched: (text) => {
65-
outputElement.textContent += text;
66-
}
97+
batched: printOutput
6798
});
6899

100+
// 实现交互式输入
101+
pyodide.runPython(`
102+
import sys
103+
from js import prompt
104+
105+
def custom_input(*args):
106+
return prompt("请输入:")
107+
108+
input = custom_input
109+
`);
110+
69111
try {
112+
// 修改执行代码逻辑,直接执行整个代码块
70113
await pyodide.runPythonAsync(code);
71114
} catch (err) {
72115
outputElement.textContent += err;

0 commit comments

Comments
 (0)