Skip to content

Commit

Permalink
Include gosling-widget in project
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt committed Nov 20, 2024
1 parent 134b34a commit 28d01b9
Show file tree
Hide file tree
Showing 8 changed files with 1,007 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ node_modules/
_build/
generated/
gallery/
gosling/static/
20 changes: 20 additions & 0 deletions deno.json
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"]
}
}
}
23 changes: 23 additions & 0 deletions frontend/widget.ts
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);
}
});
},
};
20 changes: 20 additions & 0 deletions gosling/_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import json
import pathlib
from typing import Any, Dict

import anywidget
import traitlets as t


class GoslingWidget(anywidget.AnyWidget):
_esm = pathlib.Path(__file__).parent / "static" / "widget.js"
_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)
2 changes: 1 addition & 1 deletion gosling/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def save(

def widget(self):
try:
from gosling_widget import GoslingWidget
from ._widget import GoslingWidget
except ImportError:
raise ImportError(
"The 'gosling-widget' package is required to use the widget() method."
Expand Down
13 changes: 9 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ license = { text = "MIT" }
readme = "README.md"
dynamic = ["version"]
urls = { homepage = "https://github.com/gosling-lang/gos" }
dependencies = ["jsonschema>=3.0", "jinja2", "pandas"]
dependencies = ["jsonschema>=3.0", "jinja2", "pandas", "anywidget>=0.9.13"]

[project.optional-dependencies]
all = ["servir>=0.2.1", "gosling-widget>=0.0.2", "clodius>=0.20.1"]
all = ["servir>=0.2.1", "clodius>=0.20.1"]

[tool.uv]
dev-dependencies = [
"clodius>=0.20.1",
"gosling-widget>=0.0.2",
"jupyterlab>=4.3.1",
"pytest>=6.0",
"requests>=2.0",
"servir>=0.2.1",
Expand All @@ -28,17 +28,22 @@ dev-dependencies = [
]

[tool.hatch.build]
only-packages = true
include = ["/gosling", "/tests"]
artifacts = ["gosling/static/widget.js", "scripts/hatch_build.py"]

[tool.hatch.version]
source = "vcs"

[tool.hatch.build.hooks.vcs]
version-file = "gosling/_version.py"

[tool.hatch.build.hooks.custom]
path = "scripts/hatch_build.py"

[tool.pytest.ini_options]
addopts = [
"--ignore=gosling/examples",
"--ignore=tools/altair",
"--doctest-modules"
"--doctest-modules",
]
25 changes: 25 additions & 0 deletions scripts/hatch_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""A Hatchling plugin to build the gosling-widget frontend."""

import os
import pathlib
import subprocess

from hatchling.builders.hooks.plugin.interface import BuildHookInterface

ROOT = pathlib.Path(__file__).parent / ".."


class QuakBuildHook(BuildHookInterface):
"""Hatchling plugin to build the quak frontend."""

PLUGIN_NAME = "gosling-widget"

def initialize(self, version: str, build_data: dict) -> None:
"""Initialize the plugin."""
if os.getenv("SKIP_DENO_BUILD", "0") == "1":
# Skip the build if the environment variable is set
# Useful in CI/CD pipelines
return

if not (ROOT / "gosling/static/widget.js").exists():
subprocess.check_call(["deno", "task", "build"], cwd=ROOT)
925 changes: 908 additions & 17 deletions uv.lock

Large diffs are not rendered by default.

0 comments on commit 28d01b9

Please sign in to comment.