Skip to content

Commit

Permalink
deploy: 7ca590a
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanguage committed Dec 28, 2023
1 parent a11cfcd commit 6d6a5a4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
66 changes: 65 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@
menu_container: "menu-container",
window_manager_container: null,
expose_api: true,
imjoy_api: {},
imjoy_api: {
async getConsole(caller, config) {
return createApi();
}
},
});
const imjoy_api = app.imjoy.api;
window.app.imjoy = imjoy_api;
Expand All @@ -110,6 +114,38 @@
},
});

app.addMenuItem({
label: "📝 Edit Code",
async callback() {
let plugin = null;
app.loadedPlugins.forEach((p) => {
if (p.config.name === "PyEditor") {
plugin = p;
}
});
if (!plugin) {
plugin = await app.loadPlugin(
"https://nanguage.github.io/web-python-console/python-editor.imjoy.html"
);
}
await app.runPlugin(plugin);
}
});

app.addMenuItem({
label: "📂 Mount Local Folder",
async callback() {
const mountPoint = prompt(
`Please type a mount point`,
"/mount_dir"
);
if (mountPoint) {
await mountNativeFs(mountPoint);
await imjoy_api.showMessage(`Mounted at ${mountPoint}`);
}
}
})

return app;
}

Expand All @@ -121,6 +157,19 @@
await initConsole(false);
}

async function mountNativeFs(mountPoint = "/mount_dir") {
const pyodide = window.pyodide;
const dirHandle = await showDirectoryPicker();
if ((await dirHandle.queryPermission({ mode: "readwrite" })) !== "granted") {
if (
(await dirHandle.requestPermission({ mode: "readwrite" })) !== "granted"
) {
throw Error("Unable to read and write directory");
}
}
const nativefs = await pyodide.mountNativeFS(mountPoint, dirHandle);
}

async function createApi() {
function setup() {
console.log("web-python-console is ready.")
Expand Down Expand Up @@ -149,15 +198,26 @@
const res = toImjoyObj(value);
return res;
}
function setVariable(name, value) {
const pyodide = window.pyodide;
const res = toPyObj(value);
pyodide.globals.set(name, res);
}
function mount_native_fs(mount_point = "/mount_dir") {
return mountNativeFs(mount_point);
}

return {
_rintf: true,
setup: setup,
echo: echo,
exec: exec,
clear: clear,
restart: restart,
get_variable: getVariable,
set_variable: setVariable,
get_content: getConsoleContent,
mount_native_fs: mount_native_fs,
}
}

Expand Down Expand Up @@ -271,6 +331,10 @@
iframe.style.overflow = 'auto'
term.echo(iframe);
}

async mountNativeFs(mountPoint = "/mount_dir") {
await mountNativeFs(mountPoint);
}
}

function toImjoyObj(obj) {
Expand Down
21 changes: 13 additions & 8 deletions python-editor.imjoy.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@
class ImJoyPlugin {
async setup() {
api.log('initialized')
let pycon = await api.getWindow("web-python-console")
if (!pycon) {
pycon = await api.createWindow({
src: 'https://nanguage.github.io/web-python-console/',
name: 'web-python-console',
w: 30,
h: 40,
})
let pycon;
if ("getConsole" in api) {
pycon = await api.getConsole("python")
} else {
pycon = await api.getWindow("web-python-console")
if (!pycon) {
pycon = await api.createWindow({
src: 'https://nanguage.github.io/web-python-console/',
name: 'web-python-console',
w: 30,
h: 40,
})
}
}
this.pycon = pycon
}
Expand Down

0 comments on commit 6d6a5a4

Please sign in to comment.