Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
feat: Migrate to anywidget (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt authored May 11, 2023
1 parent 3ab6003 commit 1bf5bfc
Show file tree
Hide file tree
Showing 19 changed files with 177 additions and 18,001 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI

on:
push:
branches:
- main
pull_request:

jobs:
Lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.x"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e .[dev]
- name: Check formatting with Black
run: black --check .

- name: Check linting with Ruff
run: ruff .

Build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.x"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build
run: python -m build .
52 changes: 0 additions & 52 deletions .github/workflows/publish.yml

This file was deleted.

38 changes: 38 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Release

permissions:
contents: write

on:
push:
tags:
- "v*" # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
Release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: actions/setup-python@v4
with:
python-version: "3.x"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build
run: python -m build .

- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.TWINE_API_KEY }}

- run: npx changelogithub@0.12
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
12 changes: 0 additions & 12 deletions MANIFEST.in

This file was deleted.

5 changes: 0 additions & 5 deletions higlass-widget.json

This file was deleted.

29 changes: 5 additions & 24 deletions higlass_widget/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
import sys

from ._version import __version__
from .widget import HiGlassWidget
import importlib.metadata

try:
if "google.colab" in sys.modules:
from google.colab import output

output.enable_custom_widget_manager()
except ImportError:
pass


def _jupyter_labextension_paths():
return [{"src": "labextension", "dest": "higlass-widget"}]

__version__ = importlib.metadata.version("higlass-widget")
except importlib.metadata.PackageNotFoundError:
__version__ = "unknown"

def _jupyter_nbextension_paths():
return [
{
"section": "notebook",
"src": "nbextension",
"dest": "higlass-widget",
"require": "higlass-widget/extension",
}
]
from .widget import HiGlassWidget # noqa
10 changes: 0 additions & 10 deletions higlass_widget/_version.py

This file was deleted.

15 changes: 0 additions & 15 deletions higlass_widget/nbextension/extension.js

This file was deleted.

40 changes: 40 additions & 0 deletions higlass_widget/widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import hglib from "https://esm.sh/higlass@1.12?deps=react@17,react-dom@17,pixi.js@6"

/**
* @param {{
* xDomain: [number, number],
* yDomain: [number, number],
* }} location
*/
function toPts({ xDomain, yDomain }) {
let [x, xe] = xDomain;
let [y, ye] = yDomain;
return [x, xe, y, ye];
}

export async function render(view) {
let viewconf = JSON.parse(view.model.get("_viewconf"));
let api = await hglib.viewer(view.el, viewconf);

view.model.on("msg:custom", (msg) => {
msg = JSON.parse(msg);
let [fn, ...args] = msg;
api[fn](...args);
});

if (viewconf.views.length === 1) {
api.on("location", (loc) => {
view.model.set("location", toPts(loc));
view.model.save_changes();
}, viewconf.views[0].uid);
} else {
viewconf.views.forEach((view, idx) => {
api.on("location", (loc) => {
let copy = view.model.get("location").slice();
copy[idx] = toPts(loc);
view.model.set("location", copy);
view.model.save_changes();
}, view.uid);
});
}
}
27 changes: 10 additions & 17 deletions higlass_widget/widget.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
from __future__ import annotations
import json
from typing import Any, Dict
import pathlib

import ipywidgets
import traitlets.traitlets as t
import anywidget
import traitlets as t

from ._version import __version__


class HiGlassWidget(ipywidgets.DOMWidget):
_model_name = t.Unicode("HiGlassModel").tag(sync=True)
_model_module = t.Unicode("higlass-widget").tag(sync=True)
_model_module_version = t.Unicode(__version__).tag(sync=True)

_view_name = t.Unicode("HiGlassView").tag(sync=True)
_view_module = t.Unicode("higlass-widget").tag(sync=True)
_view_module_version = t.Unicode(__version__).tag(sync=True)

class HiGlassWidget(anywidget.AnyWidget):
_esm = pathlib.Path(__file__).parent / "widget.js"
_css = "https://esm.sh/higlass@1.12/dist/hglib.css"
_viewconf = t.Unicode("null").tag(sync=True)

# readonly properties
location = t.List(t.Union([t.Float(), t.Tuple()]), read_only=True).tag(sync=True)

def __init__(self, viewconf: Dict[str, Any], **kwargs):
def __init__(self, viewconf: dict, **kwargs):
super().__init__(_viewconf=json.dumps(viewconf), **kwargs)

def reload(self, *items):
Expand All @@ -33,8 +26,8 @@ def zoom_to(
view_id: str,
start1: int,
end1: int,
start2: int = None,
end2: int = None,
start2: int | None = None,
end2: int | None = None,
animate_time: int = 500,
):
msg = json.dumps(["zoomTo", view_id, start1, end1, start2, end2, animate_time])
Expand Down
6 changes: 3 additions & 3 deletions notebooks/Widget.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"\n",
"\n",
"# Install if in Google colab notebook\n",
"if 'google.colab' in sys.modules:\n",
" os.system('python -m pip install --upgrade higlass-widget')\n",
"if \"google.colab\" in sys.modules:\n",
" os.system(\"python -m pip install --upgrade higlass-widget\")\n",
"\n",
"\n",
"def load_config(url):\n",
Expand Down Expand Up @@ -45,7 +45,7 @@
"source": [
"from higlass_widget import HiGlassWidget\n",
"\n",
"url = 'https://gist.githubusercontent.com/manzt/c2c498dac3ca9804a2b8e4bc1af3b55b/raw/ee8426c9728e875b6f4d65030c61181c6ba29b53/example.json'\n",
"url = \"https://gist.githubusercontent.com/manzt/c2c498dac3ca9804a2b8e4bc1af3b55b/raw/ee8426c9728e875b6f4d65030c61181c6ba29b53/example.json\"\n",
"conf = load_config(url)\n",
"w = HiGlassWidget(conf)\n",
"w"
Expand Down
Loading

0 comments on commit 1bf5bfc

Please sign in to comment.