Skip to content

Commit 3eecc6b

Browse files
author
Marty
committed
Test test.html
1 parent 682e896 commit 3eecc6b

File tree

7 files changed

+275
-113
lines changed

7 files changed

+275
-113
lines changed

_daily.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## 本機測試
2+
將 gptbot_local.html 覆蓋 gptbot.html
3+
測試後再將 gptbot_prod.html 覆蓋 gptbot.html
4+
5+
6+
# 2024/11/04

index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
<head>
55
<meta charset="utf-8">
66
<title>WebBit-GPT</title>
7-
<!-- Pyodide -->
7+
<!-- Pyodide
8+
<script src="https://cdn.jsdelivr.net/pyodide/v0.25.0/full/pyodide.js"></script>
9+
-->
810
<script src="https://cdn.jsdelivr.net/pyodide/v0.25.0/full/pyodide.js"></script>
911
<!-- MQTT -->
1012
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.1.0/paho-mqtt.min.js"></script>

prod/coms/wa-run-python.js

Lines changed: 9 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class RunPython extends LitElement {
5656
await this.pyodide.runPythonAsync(code);
5757
return null;
5858
} catch (err) {
59-
if (err instanceof this.pyodide.PythonError) {
59+
if (this.pyodide && err.constructor.name === 'PythonError') {
6060
var errMsg = err.message.split("\n").slice(-3).join(" ");
6161
var result = errMsg.replace(/(line )(\d+)/, function (match, p1, p2) {
6262
return p1 + (parseInt(p2) - 2);
@@ -85,137 +85,36 @@ export class RunPython extends LitElement {
8585
output.scrollBottom();
8686
}
8787

88-
function convertCode(code) {
89-
var imp = "import js\nimport asyncio\n";
90-
var convertCode =
91-
imp + code.replaceAll(/input\(/g, "await js.window.Main.input(");
92-
console.log(">>>>>>\n", convertCode);
93-
return convertCode;
94-
}
95-
96-
function convertCode_sheet(code) {
97-
// replace input
98-
const pattern = /(?<!_)input\(([^)]*)\)/g;
99-
const replacement = "await js.window.Main.input($1)";
100-
code = code.replace(pattern, replacement);
101-
// fix await await
102-
code = code.replace(/await await/g, "await");
103-
104-
var imp = "import js\nimport asyncio\n";
105-
code = code.replace(/import json, sheet/g, "import json");
106-
code = code.replace(/import json,sheet/g, "import json");
107-
code = code.replace(/import random, json, sheet/g, "import random, json");
108-
code = code.replace(/import sheet/g, "");
109-
//code = code.replace(/= input\(/g, '= await input(');
110-
111-
code = code.replace(/asyncio.run(main())/g, "await main()");
112-
var convertCode = imp + code;
113-
convertCode = convertCode.replaceAll(
114-
/await sheet.select\(/g,
115-
"await js.window.Main.select("
116-
);
117-
console.log("==============", "\n" + convertCode);
118-
return convertCode;
88+
function stdin_func() {
89+
var rtn = prompt("Python input() 请输入:") || "";
90+
console.log("rtn:", rtn);
91+
return rtn;
11992
}
12093

12194
run.addEventListener("click", async function () {
12295
output.cls();
123-
var newCode = convertCode(editor.getCode());
124-
self.runPythonCode(newCode).then((result) => {
125-
if (result != null) {
126-
result = result.substring(result.indexOf(",") + 1);
127-
output.showErr(result);
128-
}
129-
});
130-
//pyodide.runPython(editor.getCode());
96+
var newCode = editor.getCode();
97+
//await self.runPythonCode(newCode);
98+
await pyodide.runPythonAsync(newCode);
13199
});
132100

133101
let pyodide;
134102
console.log("init pyodide....");
135103
pyodide = await loadPyodide({
136-
//stdin: stdin_func,
104+
stdin: stdin_func,
137105
stdout: stdout_func,
138106
stderr: stderr_func,
139107
});
140108
run.style["color"] = "#eee";
141109
icon.style["fill"] = "#eee";
142110

143-
window.Main.input = async function (msg) {
144-
return new Promise((resolve, reject) => {
145-
output.addInput(msg, function (rtnData) {
146-
resolve(rtnData);
147-
});
148-
});
149-
};
150111
this.pyodide = pyodide;
151112
this.output = output;
152113
// Pyodide is now ready to use...
153114
// console.log("pyodide ready !");
154115
run.removeAttribute("disabled");
155116
}
156117

157-
testCase(idx, newCode, sampleinput, sampleoutput) {
158-
console.log("testcase #" + idx);
159-
// console.log("sampleinput:" + sampleinput);
160-
// console.log("sampleoutput:" + sampleoutput);
161-
let allInputData = ""; // for debug
162-
this.pyodide.setStdin({
163-
stdin: function () {
164-
var anInput = sampleinput.shift();
165-
if (typeof anInput == "undefined") {
166-
throw "EOF";
167-
} else {
168-
anInput = anInput + "\n";
169-
}
170-
allInputData += anInput;
171-
return anInput;
172-
},
173-
//autoEOF: true
174-
});
175-
var self = this;
176-
return new Promise((resolve, reject) => {
177-
this.output.cls();
178-
self.runPythonCode(newCode).then((result) => {
179-
if (result != null) {
180-
result = result.substring(result.indexOf(",") + 1);
181-
output.showErr(result);
182-
}
183-
var outputData = self.output.getOutputData();
184-
//cut of last \n
185-
outputData = outputData.substring(0, outputData.length - 1);
186-
//console.log("sampleinput=", sampleinput, " \noutputData=" + outputData);
187-
let correctOutput = sampleoutput.join("\n");
188-
let resultOutput = outputData;
189-
//console.log("input:", allInputData, " , check [" + correctOutput + "]==[" + resultOutput + "]");
190-
//console.log(correctOutput, correctOutput.length);
191-
//console.log(resultOutput, resultOutput.length);
192-
resolve(correctOutput == resultOutput);
193-
});
194-
});
195-
}
196-
197-
async frontTest(exam) {
198-
//var exam = document.getElementById("exam");
199-
var info = exam.getInfo();
200-
// copy testdata
201-
var sample = JSON.parse(info["sample"]);
202-
this.output.cls();
203-
var newCode = editor.getCode();
204-
var success = true;
205-
for (var i = 0; i < sample.length; i = i + 2) {
206-
var idx = i / 2 + 1;
207-
var input = sample[i];
208-
var out = sample[i + 1];
209-
// console.log(">>>", input, out);
210-
var result = await this.testCase(idx, newCode, input, out);
211-
if (!result) {
212-
success = false;
213-
break;
214-
}
215-
}
216-
alert("測試" + (success ? "成功" : "失敗"));
217-
}
218-
219118
hide(state) {
220119
if (state) {
221120
this.run.style["display"] = "none";

prod/html/gptbot.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<script src="./oauth.js"></script>
3939
<script>
4040
(async () => {
41-
//*
41+
/*
4242
if (!(await isLogin())) {
4343
parent.Main.popup(
4444
"還差一步!",
@@ -72,7 +72,7 @@
7272
});
7373
}
7474
//*/
75-
/*/ 測試用
75+
//*/ 測試用
7676
parent.window.user = {
7777
id: 4,
7878
name: "xxx",

prod/html/gptbot_local.html

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.1.0/paho-mqtt.min.js"></script>
7+
<!-- MQTT -->
8+
<script src="../js/mqttapp.js"></script>
9+
<!-- Flow Enhance -->
10+
<script src="../js/fixFlow.js"></script>
11+
<script src="../js/codeBlock.js"></script>
12+
<!-- Google Font Material Symbol -->
13+
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Icons&display=swap" />
14+
<script type="module" src="../coms/wa-select-prompt.js"></script>
15+
<script type="module" src="../coms/wa-voice.js"></script>
16+
<script type="module" src="../coms/wa-gpt-bot.js"></script>
17+
<script type="module" src="../coms/wa-carousel.js"></script>
18+
<link rel="stylesheet" href="./gptbot.css" />
19+
</head>
20+
21+
<body>
22+
<div id="gptbot-content">
23+
<div class="quiz-panel">
24+
<wa-carousel id="carousel"></wa-carousel>
25+
</div>
26+
<div class="chat-panel">
27+
<wa-select-prompt id="select_prompt" style="width: 100%"></wa-select-prompt><br />
28+
<wa-gpt-bot id="gpt"></wa-gpt-bot>
29+
<br />
30+
<div class="menuBar">
31+
<wa-voice id="voice" class="menuBtn" callback="voiceToTextarea"></wa-voice>
32+
<wa-gpt-btn id="sendBtn" class="menuBtn" callback="send"></wa-gpt-btn>
33+
</div>
34+
</div>
35+
</div>
36+
</body>
37+
38+
<script src="./oauth.js"></script>
39+
<script>
40+
(async () => {
41+
/*
42+
if (!(await isLogin())) {
43+
parent.Main.popup(
44+
"還差一步!",
45+
"註冊帳號以獲取免費體驗",
46+
"info",
47+
"前往註冊",
48+
).then(() => {
49+
logout();
50+
});
51+
return;
52+
}
53+
//*/
54+
//*
55+
const user = await getUser();
56+
console.log(">>>", user);
57+
setCookies("userId", user?.id);
58+
59+
// 將 user 資訊存到父視窗的 window.user 物件中
60+
if (parent && parent.window) {
61+
parent.window.user = user;
62+
}
63+
64+
if (!localStorage.getItem("isReadTerm")) {
65+
parent.Main.popup(
66+
"歡迎加入!",
67+
"您可以免費體驗直到 2024/12/31",
68+
"info",
69+
"知道了",
70+
).then(() => {
71+
localStorage.setItem("isReadTerm", true)
72+
});
73+
}
74+
//*/
75+
//*/ 測試用
76+
parent.window.user = {
77+
id: 4,
78+
name: "xxx",
79+
80+
role: { id: 2, name: 'user' },
81+
};
82+
//*/
83+
})();
84+
</script>
85+
<script src="./gptbot-lib.js"></script>
86+
<script src="./gptbot.js"></script>
87+
88+
</html>

prod/html/gptbot_prod.html

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.1.0/paho-mqtt.min.js"></script>
7+
<!-- MQTT -->
8+
<script src="../js/mqttapp.js"></script>
9+
<!-- Flow Enhance -->
10+
<script src="../js/fixFlow.js"></script>
11+
<script src="../js/codeBlock.js"></script>
12+
<!-- Google Font Material Symbol -->
13+
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Icons&display=swap" />
14+
<script type="module" src="../coms/wa-select-prompt.js"></script>
15+
<script type="module" src="../coms/wa-voice.js"></script>
16+
<script type="module" src="../coms/wa-gpt-bot.js"></script>
17+
<script type="module" src="../coms/wa-carousel.js"></script>
18+
<link rel="stylesheet" href="./gptbot.css" />
19+
</head>
20+
21+
<body>
22+
<div id="gptbot-content">
23+
<div class="quiz-panel">
24+
<wa-carousel id="carousel"></wa-carousel>
25+
</div>
26+
<div class="chat-panel">
27+
<wa-select-prompt id="select_prompt" style="width: 100%"></wa-select-prompt><br />
28+
<wa-gpt-bot id="gpt"></wa-gpt-bot>
29+
<br />
30+
<div class="menuBar">
31+
<wa-voice id="voice" class="menuBtn" callback="voiceToTextarea"></wa-voice>
32+
<wa-gpt-btn id="sendBtn" class="menuBtn" callback="send"></wa-gpt-btn>
33+
</div>
34+
</div>
35+
</div>
36+
</body>
37+
38+
<script src="./oauth.js"></script>
39+
<script>
40+
(async () => {
41+
//*
42+
if (!(await isLogin())) {
43+
parent.Main.popup(
44+
"還差一步!",
45+
"註冊帳號以獲取免費體驗",
46+
"info",
47+
"前往註冊",
48+
).then(() => {
49+
logout();
50+
});
51+
return;
52+
}
53+
//*/
54+
//*
55+
const user = await getUser();
56+
console.log(">>>", user);
57+
setCookies("userId", user?.id);
58+
59+
// 將 user 資訊存到父視窗的 window.user 物件中
60+
if (parent && parent.window) {
61+
parent.window.user = user;
62+
}
63+
64+
if (!localStorage.getItem("isReadTerm")) {
65+
parent.Main.popup(
66+
"歡迎加入!",
67+
"您可以免費體驗直到 2024/12/31",
68+
"info",
69+
"知道了",
70+
).then(() => {
71+
localStorage.setItem("isReadTerm", true)
72+
});
73+
}
74+
//*/
75+
/*/ 測試用
76+
parent.window.user = {
77+
id: 4,
78+
name: "xxx",
79+
email: "yyy@kingkit101.com",
80+
role: { id: 2, name: 'user' },
81+
};
82+
//*/
83+
})();
84+
</script>
85+
<script src="./gptbot-lib.js"></script>
86+
<script src="./gptbot.js"></script>
87+
88+
</html>

0 commit comments

Comments
 (0)