Skip to content

Commit

Permalink
adapt for new chatbot extension system
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanguage committed Jan 13, 2024
1 parent 1359ca8 commit 3142c91
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ The console is integrated with ImJoy, you can use the ImJoy API to control the c
| `.get_variable` | Get a variable from the console | `name: string` |
| `.set_variable` | Set a variable in the console | `name: string; value: any` |
| `.mount_native_fs` | Mount a native file system to the console | `mount_point: string;` |
| `.ready` | A promise that resolves when the console is ready | |

### Using ImJoy API in the console

Expand Down Expand Up @@ -123,7 +124,7 @@ Then click the menu again and click `web-python-console` to open the console, or
Install and run the following plugin to register the extension for BioImage.IO ChatBot:

```
https://nanguage.github.io/web-python-console/chatbot-launcher.imjoy.html
https://nanguage.github.io/web-python-console/chatbot-extension.imjoy.html
```

A demo:
Expand Down
20 changes: 13 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@
async function restart() {
const body = $("body");
$(".terminal")[0].remove();
await initConsole(false);
window.console_ready = initConsole(false);
await window.console_ready;
}

async function mountNativeFs(mountPoint = "/mount_dir") {
Expand All @@ -188,10 +189,10 @@
const term = window.term;
term.echo(msg);
}
function exec(code, show_code = true) {
async function exec(code, show_code = true) {
const term = window.term;
code = code.replaceAll("\r\n", "\n");
term.exec(code, !show_code);
await term.exec(code, !show_code);
}
function clear() {
const term = window.term;
Expand All @@ -216,6 +217,9 @@
function mount_native_fs(mount_point = "/mount_dir") {
return mountNativeFs(mount_point);
}
async function consoleReady() {
await window.console_ready;
}

return {
_rintf: true,
Expand All @@ -228,6 +232,7 @@
set_variable: setVariable,
get_content: getConsoleContent,
mount_native_fs: mount_native_fs,
ready: consoleReady,
}
}

Expand Down Expand Up @@ -283,7 +288,8 @@
let text = await response.text();
text = text.replaceAll("\r\n", "\n");
text = "\n" + text;
term.exec(text, !showCode);
const p = term.exec(text, !showCode);
await p;
}

function toJs(value) {
Expand Down Expand Up @@ -595,22 +601,22 @@
await initImJoy();
}

term.resume();
const installRpc = toBool(searchParams.get("install_rpc"), true);
if (installRpc) {
term.exec("import micropip; await micropip.install('imjoy-rpc')", true)
await term.exec("import micropip; await micropip.install('imjoy-rpc')", true)
const currentUrl = new URL(window.location.href);
const libUrl = currentUrl.origin + currentUrl.pathname + "/imjoy_encode.py";
await runFile(libUrl, false)
pyodide.globals.set("api", new ImjoyApiWrapper(window.app.imjoy));
}
term.resume();

const showCode = toBool(urlParams.get("show_code"), true);
const code = urlParams.get("code");
if (code) {
term.echo("--- Running code ---")
$.terminal.syntax('python')
term.exec(code, !showCode);
await term.exec(code, !showCode);
} else {
const file = urlParams.get("file");
if (file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"ui": "",
"version": "0.1.0",
"cover": "",
"description": "Register bioengine web client to chatbot",
"description": "Register web python console to Bioimage.IO chatbot",
"icon": "extension",
"inputs": null,
"outputs": null,
Expand All @@ -25,31 +25,34 @@
<script lang="javascript">
class ImJoyPlugin {
async setup() {
this.chatbot = await api.getWindow("BioImage.IO Chatbot")
if (this.chatbot) {
await this.registerExtensions()
if (api.registerChatbotExtension) {
await this.registerExtensions(api.registerChatbotExtension)
} else {
const chatbot = await api.getWindow("BioImage.IO Chatbot")
if (chatbot) {
await this.registerExtensions(chatbot.registerExtension)
}
}
}

async registerExtensions() {
const chatbot = this.chatbot
await chatbot.registerExtension({
async registerExtensions(register) {
await register({
_rintf: true,
name: "runPythonCode",
description: "Run python code in the python-web-console",
description: "The chatbot extension for execute python code using a pyodide python console.",
async get_schema() {
return {
type: "object",
title: "RunPythonCode",
description: "Run python code in the python-web-console",
description: "Run python code using the web-python-console",
properties: {
code: {
type: "string",
description: "The Python code to run",
description: "The Python code to run.",
},
result_variable: {
type: "string",
description: "The variable name to store the result, if not needed, leave it empty",
description: "The variable name for fetching the result. If there is no result needed to fetch from the console, use `_null`.",
},
},
};
Expand All @@ -62,12 +65,15 @@
name: "web-python-console",
});
}
await pycon.ready()
const code = config.code
await pycon.exec(code)
const result_variable = config.result_variable
if (result_variable) {
if (result_variable && result_variable !== "_null") {
const result = await pycon.get_variable(result_variable)
return result
} else {
return "Done"
}
},
})
Expand All @@ -83,8 +89,7 @@
name: "BioImage.IO Chatbot",
});
}
this.chatbot = chatbot
await this.registerExtensions()
await this.registerExtensions(chatbot.registerExtension)
}
}

Expand Down

0 comments on commit 3142c91

Please sign in to comment.