Skip to content

Commit

Permalink
feat(unstable): Await return from Jupyter.display (#20807)
Browse files Browse the repository at this point in the history
Allows `Jupyter.display` to return a promise.

Example:

```javascript
class WikiPage {
    constructor(public name) {}
    async [Symbol.for("Jupyter.display")]() {
        let response = await fetch("https://en.wikipedia.org/wiki/" + this.name);
        return { "text/html": await response.text() }
    }
}

new WikiPage("Deno_(software)")
```
  • Loading branch information
manzt committed Oct 6, 2023
1 parent ceecd8c commit 48bb3b2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
37 changes: 37 additions & 0 deletions cli/tests/testdata/jupyter/integration_test.ipynb
Expand Up @@ -827,6 +827,43 @@
"let sc = new SuperColor()\n",
"sc"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "c1296291-a3e8-457b-8329-6cc58a1e528a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div style=\"width: 32px; height: 32px; background-color: #239814\" />"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class SuperColorAsync {\n",
" constructor() {\n",
" this.color = \"#239814\"\n",
" }\n",
" hex() {\n",
" return this.color\n",
" }\n",
" \n",
" async [Symbol.for(\"Jupyter.display\")]() {\n",
" return {\n",
" \"text/html\": `<div style=\"width: 32px; height: 32px; background-color: ${this.hex()}\" />`\n",
" }\n",
" }\n",
"}\n",
"\n",
"let sc = new SuperColorAsync()\n",
"sc"
]
}
],
"metadata": {
Expand Down
6 changes: 3 additions & 3 deletions cli/tools/jupyter/server.rs
Expand Up @@ -551,13 +551,13 @@ async fn get_jupyter_display(
.post_message_with_event_loop(
"Runtime.callFunctionOn",
Some(json!({
"functionDeclaration": r#"function (object) {
"functionDeclaration": r#"async function (object) {
if (typeof object[Symbol.for("Jupyter.display")] !== "function") {
return null;
}
try {
const representation = object[Symbol.for("Jupyter.display")]();
const representation = await object[Symbol.for("Jupyter.display")]();
return JSON.stringify(representation);
} catch {
return null;
Expand Down

0 comments on commit 48bb3b2

Please sign in to comment.