Skip to content

Commit 5c8fafa

Browse files
author
Marty
committed
test ok (Test.html)
1 parent 2655aee commit 5c8fafa

File tree

1 file changed

+79
-39
lines changed

1 file changed

+79
-39
lines changed

test.html

Lines changed: 79 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!DOCTYPE html>
22
<html lang="zh-Hant">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>Pyodide Python 執行器</title>
@@ -8,6 +9,7 @@
89
font-family: Arial, sans-serif;
910
margin: 20px;
1011
}
12+
1113
textarea {
1214
width: 100%;
1315
height: 200px;
@@ -16,6 +18,7 @@
1618
padding: 10px;
1719
box-sizing: border-box;
1820
}
21+
1922
#output {
2023
width: 100%;
2124
height: 400px;
@@ -26,86 +29,123 @@
2629
white-space: pre-wrap;
2730
border: 1px solid #ccc;
2831
}
32+
2933
button {
3034
padding: 10px 20px;
3135
font-size: 16px;
3236
margin-top: 10px;
3337
cursor: pointer;
3438
}
39+
40+
#input-container {
41+
display: none;
42+
margin: 10px 0;
43+
}
44+
45+
#input-box {
46+
width: 70%;
47+
padding: 5px;
48+
margin-right: 10px;
49+
}
3550
</style>
3651
</head>
52+
3753
<body>
3854
<h1>Pyodide Python 執行器</h1>
39-
<textarea id="python-code" placeholder="在此輸入您的 Python 程式碼,例如:&#10;print('Hello, Pyodide!')">for i in range(3):
40-
user_input = input("請輸入字串")
41-
print(user_input + "!")</textarea>
55+
<textarea id="python-code">async def main():
56+
for i in range(2):
57+
user_input = await input("請輸入字串")
58+
print(user_input + "$")
59+
60+
await main()</textarea>
4261
<br>
4362
<button id="run-button">執行程式碼</button>
63+
<div id="input-container">
64+
<input type="text" id="input-box">
65+
<button id="input-submit">提交</button>
66+
</div>
4467
<h2>輸出結果:</h2>
4568
<div id="output"></div>
4669

47-
<!-- 加載 Pyodide -->
4870
<script src="https://cdn.jsdelivr.net/pyodide/v0.23.4/full/pyodide.js"></script>
4971
<script>
5072
let pyodideReadyPromise = loadPyodide();
73+
let inputResolve = null;
5174

5275
async function runPython() {
5376
let pyodide = await pyodideReadyPromise;
5477
let code = document.getElementById("python-code").value;
5578
let outputElement = document.getElementById("output");
79+
let inputContainer = document.getElementById("input-container");
80+
5681
outputElement.textContent = "";
82+
83+
// 輸出函數
84+
function writeOutput(text) {
85+
const p = document.createElement('pre');
86+
p.style.margin = '0';
87+
p.textContent = text;
88+
outputElement.appendChild(p);
89+
outputElement.scrollTop = outputElement.scrollHeight;
90+
}
91+
92+
// 自定義輸入處理
93+
async function customInput(message) {
94+
writeOutput(message);
95+
inputContainer.style.display = 'block';
96+
document.getElementById('input-box').focus();
97+
98+
return new Promise(resolve => {
99+
inputResolve = resolve;
100+
});
101+
}
102+
103+
// 將函數暴露給 Pyodide
104+
globalThis.writeOutput = writeOutput;
105+
globalThis.customInput = customInput;
57106

58-
// 修改输出处理,实现即时显示
59-
let printOutput = (text) => {
60-
if (text && text.trim()) { // 只处理非空输出
61-
outputElement.textContent += text + "\n";
62-
outputElement.scrollTop = outputElement.scrollHeight;
107+
// 處理輸入提交
108+
document.getElementById('input-submit').onclick = () => {
109+
if (inputResolve) {
110+
const value = document.getElementById('input-box').value;
111+
document.getElementById('input-box').value = '';
112+
inputContainer.style.display = 'none';
113+
writeOutput(value);
114+
inputResolve(value);
115+
inputResolve = null;
63116
}
64117
};
65118

66-
// 将自定义的 prompt 函数添加到全局作用域
67-
globalThis.customPrompt = (message) => {
68-
printOutput(message); // 立即显示输入提示
69-
const result = prompt(message);
70-
if (result !== null) {
71-
printOutput(result + "!"); // 立即显示用户输入的结果
119+
// 處理按下 Enter 鍵
120+
document.getElementById('input-box').onkeypress = (e) => {
121+
if (e.key === 'Enter') {
122+
document.getElementById('input-submit').click();
72123
}
73-
return result;
74124
};
75125

76-
// 修改 Python 的标准输出和错误输出处理
77-
pyodide.setStdout({
78-
write: (text) => {
79-
printOutput(text);
80-
},
81-
flush: () => {}
82-
});
83-
84-
pyodide.setStderr({
85-
write: (text) => {
86-
printOutput(text);
87-
},
88-
flush: () => {}
89-
});
90-
91-
// 修改输入函数实现
126+
// Python 端設置
92127
pyodide.runPython(`
93-
from js import customPrompt
128+
from js import customInput, writeOutput
94129
95-
def custom_input(prompt_text=""):
96-
return customPrompt(prompt_text)
97-
98-
input = custom_input
130+
async def input(prompt_text=""):
131+
return await customInput(prompt_text)
132+
133+
def print(*args, **kwargs):
134+
end = kwargs.get('end', '\\n')
135+
sep = kwargs.get('sep', ' ')
136+
text = sep.join(str(arg) for arg in args) + end
137+
writeOutput(text)
99138
`);
100139

101140
try {
102141
await pyodide.runPythonAsync(code);
103142
} catch (err) {
104-
printOutput(err.toString());
143+
writeOutput("Error: " + err.toString());
105144
}
106145
}
107146

108147
document.getElementById("run-button").addEventListener("click", runPython);
109148
</script>
110149
</body>
111-
</html>
150+
151+
</html>

0 commit comments

Comments
 (0)