Skip to content

Commit f025f41

Browse files
authored
Merge pull request #84 from ut-code/copilot/update-pyodide-file-system-access
Use Pyodide FS API instead of Python code for file system operations
2 parents a4ff71d + a46ce38 commit f025f41

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

public/pyodide.worker.js

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
let pyodide;
33
let pyodideOutput = [];
44

5+
// Helper function to read all files from the Pyodide file system
6+
function readAllFiles() {
7+
const dirFiles = pyodide.FS.readdir(HOME);
8+
const updatedFiles = [];
9+
for (const filename of dirFiles) {
10+
if (filename === "." || filename === "..") continue;
11+
const filepath = HOME + filename;
12+
const stat = pyodide.FS.stat(filepath);
13+
if (pyodide.FS.isFile(stat.mode)) {
14+
const content = pyodide.FS.readFile(filepath, { encoding: "utf8" });
15+
updatedFiles.push([filename, content]);
16+
}
17+
}
18+
return updatedFiles;
19+
}
20+
521
async function init(id, payload) {
622
const { PYODIDE_CDN, interruptBuffer } = payload;
723
if (!pyodide) {
@@ -73,8 +89,7 @@ async function runPython(id, payload) {
7389
}
7490
}
7591

76-
const pyReadFile = pyodide.runPython(READALLFILE_CODE); /* as PyCallable*/
77-
const updatedFiles = pyReadFile().toJs(); /* as [string, string][]*/
92+
const updatedFiles = readAllFiles();
7893

7994
const output = [...pyodideOutput];
8095
pyodideOutput = []; // 出力をクリア
@@ -92,17 +107,16 @@ async function runFile(id, payload) {
92107
return;
93108
}
94109
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-
110+
// Use Pyodide FS API to write files to the file system
101111
for (const filename of Object.keys(files)) {
102112
if (files[filename]) {
103-
pyWriteFile(HOME + filename, files[filename]);
113+
pyodide.FS.writeFile(HOME + filename, files[filename], {
114+
encoding: "utf8",
115+
});
104116
}
105117
}
118+
119+
const pyExecFile = pyodide.runPython(EXECFILE_CODE); /* as PyCallable*/
106120
pyExecFile(HOME + name);
107121
} catch (e) {
108122
console.log(e);
@@ -136,8 +150,7 @@ async function runFile(id, payload) {
136150
}
137151
}
138152

139-
const pyReadFile = pyodide.runPython(READALLFILE_CODE); /* as PyCallable*/
140-
const updatedFiles = pyReadFile().toJs(); /* as [string, string][]*/
153+
const updatedFiles = readAllFiles();
141154

142155
const output = [...pyodideOutput];
143156
pyodideOutput = []; // 出力をクリア
@@ -226,22 +239,3 @@ def __execfile(filepath):
226239
227240
__execfile
228241
`;
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)