This repository has been archived by the owner on Dec 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
118 lines (101 loc) · 2.77 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class apploader {
constructor(modules, invocation){
window.languagePluginLoader.then(() => {
// If you require for pre-loaded Python packages, enable the promise below.
//window.pyodide.runPythonAsync("import setuptools, micropip").then(()=>{
this.fetchSources(modules).then(() => {
window.pyodide.runPythonAsync("import " + Object.keys(modules).join("\nimport ") + "\n" + invocation + "\n").then(() => this.initializingComplete());
});
//});
});
}
loadSources(module, baseURL, files) {
let promises = [];
for (let f in files) {
promises.push(
new Promise((resolve, reject) => {
let file = files[f];
let url = (baseURL ? baseURL + "/" : "") + file;
fetch(url, {}).then((response) => {
if (response.status === 200)
return response.text().then((code) => {
let path = ("/lib/python3.7/site-packages/" + module + "/" + file).split("/");
let lookup = "";
for (let i in path) {
if (!path[i]) {
continue;
}
lookup += (lookup ? "/" : "") + path[i];
if (parseInt(i) === path.length - 1) {
window.pyodide._module.FS.writeFile(lookup, code);
console.debug(`fetched ${lookup}`);
} else {
try {
window.pyodide._module.FS.lookupPath(lookup);
} catch {
window.pyodide._module.FS.mkdir(lookup);
console.debug(`created ${lookup}`);
}
}
}
resolve();
});
else
reject();
});
})
);
}
return Promise.all(promises);
}
fetchSources(modules) {
let promises = [];
for( let module of Object.keys(modules) )
{
promises.push(
new Promise((resolve, reject) => {
let mapfile = `${modules[module]}/files.json`;
fetch(mapfile, {}).then((response) => {
if (response.status === 200) {
response.text().then((list) => {
let files = [];
try {
files = JSON.parse(list);
}
catch (e) {
console.error(`Unable to parse ${mapfile} properly, check for correct config of ${module}`);
return reject();
}
this.loadSources(module, modules[module], files).then(() => {
resolve();
})
})
} else {
reject();
}
})
}));
}
return Promise.all(promises).then(() => {
for( let module of Object.keys(modules) ) {
window.pyodide.loadedPackages[module] = "default channel";
}
window.pyodide.runPython(
'import importlib as _importlib\n' +
'_importlib.invalidate_caches()\n'
);
});
}
initializingComplete() {
document.body.classList.remove("is-loading")
}
}
window.addEventListener(
"load",
(event) => {
window.apploader = new apploader({
"app": ".",
},
"app.start()");
}
);