-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
GoslingWidget
an anywidget (#162)
* Include gosling-widget in project * Update CI * Replace dependency groups naming * Rename build hook * typo * Include warning with missing widget * clear notebooks * remove try catch
- Loading branch information
Showing
13 changed files
with
1,052 additions
and
826 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ concurrency: | |
env: | ||
PYTHONUNBUFFERED: "1" | ||
FORCE_COLOR: "1" | ||
SKIP_DENO_BUILD: "1" | ||
|
||
jobs: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ concurrency: | |
group: "pages" | ||
cancel-in-progress: true | ||
|
||
env: | ||
SKIP_DENO_BUILD: "1" | ||
|
||
jobs: | ||
|
||
Deploy: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ node_modules/ | |
_build/ | ||
generated/ | ||
gallery/ | ||
gosling/static/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"lock": false, | ||
"nodeModulesDir": "auto", | ||
"tasks": { | ||
"dev": "deno run -A --node-modules-dir npm:esbuild --bundle --minify --loader:.css=text --format=esm --outfile=gosling/static/widget.js frontend/widget.ts --sourcemap=inline --watch", | ||
"build": "deno run -A --node-modules-dir npm:esbuild --bundle --minify --loader:.css=text --format=esm --outfile=gosling/static/widget.js frontend/widget.ts" | ||
}, | ||
"imports": { | ||
"@anywidget/types": "npm:@anywidget/types@^0.2.0", | ||
"gosling.js": "npm:gosling.js@^0.17.0" | ||
}, | ||
"fmt": { | ||
"useTabs": true | ||
}, | ||
"lint": { | ||
"rules": { | ||
"exclude": ["prefer-const"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as gosling from "gosling.js"; | ||
|
||
interface Model { | ||
_viewconf: string; | ||
} | ||
|
||
export default { | ||
async render({ model, el }: import("@anywidget/types").RenderProps<Model>) { | ||
const viewconf = JSON.parse(model.get("_viewconf")); | ||
const api = await gosling.embed(el, viewconf, { padding: 0 }); | ||
model.on("msg:custom", (msg) => { | ||
msg = JSON.parse(msg); | ||
console.log(msg); | ||
try { | ||
const [fn, ...args] = msg; | ||
// @ts-expect-error - This is a dynamic call | ||
api[fn](...args); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import json | ||
import pathlib | ||
from typing import Any, Dict | ||
import warnings | ||
|
||
import anywidget | ||
import traitlets as t | ||
|
||
_esm = pathlib.Path(__file__).parent / "static" / "widget.js" | ||
|
||
if not _esm.exists(): | ||
warnings.warn("Widget front-end code not found. Assuming running in CI.") | ||
_esm = None | ||
|
||
|
||
class GoslingWidget(anywidget.AnyWidget): | ||
_esm = _esm | ||
_viewconf = t.Unicode("null").tag(sync=True) | ||
|
||
location = t.List(t.Union([t.Float(), t.Tuple()]), read_only=True).tag(sync=True) | ||
|
||
def __init__(self, viewconf: Dict[str, Any], **kwargs): | ||
super().__init__(_viewconf=json.dumps(viewconf), **kwargs) | ||
|
||
def zoom_to(self, view_id: str, pos: str, padding: float = 0, duration: int = 1000): | ||
msg = json.dumps(["zoomTo", view_id, pos, padding, duration]) | ||
self.send(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.