Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/desktop/electron/src/python-frontend.ts
1067 views
1
export default async function python() {
2
const element = document.createElement("pre");
3
document.body.appendChild(element);
4
console.log("This is python...!");
5
let t0 = performance.now();
6
await window.electronAPI.pythonExec("import os, sys");
7
element.innerText = `Python startup time: ${performance.now() - t0}ms\n`;
8
t0 = performance.now();
9
const v = await window.electronAPI.pythonRepr("sys.version");
10
console.log(v);
11
element.innerText +=
12
"\n" + v.slice(1, -1) + `\nTime: ${performance.now() - t0}ms\n`;
13
14
// filesystem
15
element.innerText +=
16
"\nNative Files:\n" +
17
(await window.electronAPI.pythonRepr("' '.join(os.listdir('.'))")) +
18
"\n";
19
20
await window.electronAPI.pythonExec(`
21
import pandas
22
from contextlib import redirect_stdout
23
import io
24
f = io.StringIO()
25
with redirect_stdout(f):
26
print("PYTHONHOME = ", os.environ.get('PYTHONHOME'))
27
print("sys.path = ", sys.path)
28
pandas.show_versions()
29
s = f.getvalue()
30
`);
31
let pv = await window.electronAPI.pythonRepr("s");
32
console.log(pv);
33
pv = pv.split("\\n").join("\n").slice(1, -1);
34
console.log(pv);
35
pv = `Time to import pandas and get versions: ${Math.round(
36
performance.now() - t0
37
)}ms\n${pv}`;
38
element.innerText += "\n" + pv;
39
}
40
41