Skip to content

Commit 2d8bb3c

Browse files
Copilotna-trium-144
andcommitted
Refactor pyodide.worker.js to use Pyodide FS API instead of Python code
Co-authored-by: na-trium-144 <[email protected]>
1 parent dbec361 commit 2d8bb3c

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

public/pyodide.worker.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,18 @@ async function runPython(id, payload) {
7373
}
7474
}
7575

76-
const pyReadFile = pyodide.runPython(READALLFILE_CODE); /* as PyCallable*/
77-
const updatedFiles = pyReadFile().toJs(); /* as [string, string][]*/
76+
// Use Pyodide FS API to read all files
77+
const dirFiles = pyodide.FS.readdir(HOME);
78+
const updatedFiles = [];
79+
for (const filename of dirFiles) {
80+
if (filename === "." || filename === "..") continue;
81+
const filepath = HOME + filename;
82+
const stat = pyodide.FS.stat(filepath);
83+
if (pyodide.FS.isFile(stat.mode)) {
84+
const content = pyodide.FS.readFile(filepath, { encoding: "utf8" });
85+
updatedFiles.push([filename, content]);
86+
}
87+
}
7888

7989
const output = [...pyodideOutput];
8090
pyodideOutput = []; // 出力をクリア
@@ -92,17 +102,16 @@ async function runFile(id, payload) {
92102
return;
93103
}
94104
try {
95-
// TODO: 手動でファイルの内容を書き込む代わりにファイルシステムのマウントができるらしい
96-
// https://pyodide.org/en/stable/usage/file-system.html
97-
98-
const pyWriteFile = pyodide.runPython(WRITEFILE_CODE); /* as PyCallable*/
99-
const pyExecFile = pyodide.runPython(EXECFILE_CODE); /* as PyCallable*/
100-
105+
// Use Pyodide FS API to write files to the file system
101106
for (const filename of Object.keys(files)) {
102107
if (files[filename]) {
103-
pyWriteFile(HOME + filename, files[filename]);
108+
pyodide.FS.writeFile(HOME + filename, files[filename], {
109+
encoding: "utf8",
110+
});
104111
}
105112
}
113+
114+
const pyExecFile = pyodide.runPython(EXECFILE_CODE); /* as PyCallable*/
106115
pyExecFile(HOME + name);
107116
} catch (e) {
108117
console.log(e);
@@ -136,8 +145,18 @@ async function runFile(id, payload) {
136145
}
137146
}
138147

139-
const pyReadFile = pyodide.runPython(READALLFILE_CODE); /* as PyCallable*/
140-
const updatedFiles = pyReadFile().toJs(); /* as [string, string][]*/
148+
// Use Pyodide FS API to read all files
149+
const dirFiles = pyodide.FS.readdir(HOME);
150+
const updatedFiles = [];
151+
for (const filename of dirFiles) {
152+
if (filename === "." || filename === "..") continue;
153+
const filepath = HOME + filename;
154+
const stat = pyodide.FS.stat(filepath);
155+
if (pyodide.FS.isFile(stat.mode)) {
156+
const content = pyodide.FS.readFile(filepath, { encoding: "utf8" });
157+
updatedFiles.push([filename, content]);
158+
}
159+
}
141160

142161
const output = [...pyodideOutput];
143162
pyodideOutput = []; // 出力をクリア
@@ -226,22 +245,3 @@ def __execfile(filepath):
226245
227246
__execfile
228247
`;
229-
const WRITEFILE_CODE = `
230-
def __writefile(filepath, content):
231-
with open(filepath, 'w') as f:
232-
f.write(content)
233-
234-
__writefile
235-
`;
236-
const READALLFILE_CODE = `
237-
def __readallfile():
238-
import os
239-
files = []
240-
for file in os.listdir():
241-
if os.path.isfile(file):
242-
with open(file, 'r') as f:
243-
files.append((file, f.read()))
244-
return files
245-
246-
__readallfile
247-
`;

0 commit comments

Comments
 (0)