Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into imessage
Browse files Browse the repository at this point in the history
  • Loading branch information
hainm committed Nov 29, 2024
2 parents 962a204 + 36f0a91 commit 6417247
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 58 deletions.
19 changes: 11 additions & 8 deletions .github/workflows/build-extension.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
name: Build extension

on:
push:
branches:
- '*'
pull_request:
branches:
- '*'
Expand All @@ -14,11 +11,15 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install node

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20.x'
npm-version: '10.9.0'
node-version: 'lts/*'

- name: Install Yarn
run: corepack enable

- name: Install Python
uses: actions/setup-python@v4
with:
Expand All @@ -37,6 +38,7 @@ jobs:
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- name: Setup yarn cache
uses: actions/cache@v3
id: yarn-cache
Expand All @@ -48,9 +50,10 @@ jobs:
- name: Install dependencies
run: python -m pip install -U jupyterlab jupyter_packaging jupyterlab_widgets

- name: Use existing lockfile
run: yarn install --frozen-lockfile

- name: Build the extension
working-directory: js
run: |
npm install
run: npm install
15 changes: 14 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: Test

on:
push:
pull_request:
workflow_dispatch:

Expand Down Expand Up @@ -44,6 +43,20 @@ jobs:
python-version: ${{ matrix.python-version }}
use-only-tar-bz2: false # https://github.com/conda-incubator/setup-miniconda/issues/264

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 'lts/*'

- name: Install Yarn
run: corepack enable

- name: Build labextension
run: |
cd js
npm install
cd ..
- name: Run tests
run: |
python -m pytest -vs tests --cov=nglview --cov-report=html --disable-pytest-warnings
Expand Down
4 changes: 2 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include nglview/datafiles/*
recursive-include nglview/static *.*
recursive-include nglview/staticlab *.*
recursive-include nglview/nbextension *.*
recursive-include nglview/labextension *.*
include nglview/theme/*
include LICENSE README.md DEVELOPMENT.md CHANGELOG.md
include setup.py
Expand Down
56 changes: 56 additions & 0 deletions devtools/create_labextension_symlink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os
import sys
import argparse
from pathlib import Path


def get_extension_paths():
base_dir = Path(sys.prefix)
nglview_js_widgets = "nglview-js-widgets"
nbextension_dest = base_dir / "share" / "jupyter" / "nbextensions" / nglview_js_widgets
labextension_dest = base_dir / "share" / "jupyter" / "labextensions" / nglview_js_widgets
return nbextension_dest, labextension_dest


def create_symlink(source, destination):
try:
if os.path.islink(destination):
os.remove(destination)
os.symlink(source, destination)
print(f"Created symlink: {destination} -> {source}")
except OSError as e:
print(f"Failed to create symlink: {e}", file=sys.stderr)


def remove_symlink(destination):
try:
if os.path.islink(destination):
os.remove(destination)
print(f"Removed symlink: {destination}")
else:
print(f"No symlink to remove at: {destination}")
except OSError as e:
print(f"Failed to remove symlink: {e}", file=sys.stderr)


def main():
parser = argparse.ArgumentParser(description="Create or remove symlinks for nbextension and labextension.")
parser.add_argument('-d', '--delete', action='store_true', help="Remove the symlinks instead of creating them.")
args = parser.parse_args()

nbextension_dest, labextension_dest = get_extension_paths()

base_dir = Path(__file__).resolve().parent.parent
nbextension_source = base_dir / 'nglview' / 'nbextension'
labextension_source = base_dir / 'nglview' / 'labextension'

if args.delete:
remove_symlink(nbextension_dest)
remove_symlink(labextension_dest)
else:
create_symlink(nbextension_source, nbextension_dest)
create_symlink(labextension_source, labextension_dest)


if __name__ == "__main__":
main()
14 changes: 0 additions & 14 deletions devtools/release.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
#!/usr/bin/env python

"""
Steps:
- cd nglview/static
- git rm -rf ./*
- cd nglview/staticlab
- git rm -rf ./*
- cd js
- npm install
- cd ../nglview
- git add static staticlab
"""

import subprocess
import sys
from pathlib import Path
Expand Down
2 changes: 2 additions & 0 deletions js/DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yarn==1.22.19
node==22.11.0
6 changes: 4 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"scripts": {
"prepublish": "yarn build:prod && webpack && yarn run copy:labextension",
"copy:labextension": "cp -r nglview-js-widgets/labextension/* ../nglview/staticlab/",
"copy:labextension": "mkdir -p ../nglview/labextension && cp -r nglview-js-widgets/labextension/* ../nglview/labextension/",
"build": "yarn run build:lib && yarn run build:labextension:dev",
"build:prod": "yarn run build:lib && yarn run build:labextension",
"build:labextension": "jupyter labextension build .",
Expand All @@ -52,6 +52,7 @@
"watch": "run-p watch:src watch:labextension",
"watch:src": "tsc -w",
"watch:labextension": "jupyter labextension watch .",
"watch:install": "nodemon --watch src/widget_ngl.ts --exec 'npm install'",
"test": "mocha"
},
"dependencies": {
Expand Down Expand Up @@ -89,7 +90,8 @@
"webpack": "^5.96.1",
"webpack-cli": "^5.1.4",
"babel-loader": "^8.2.2",
"@babel/plugin-transform-object-rest-spread": "^7.14.5"
"@babel/plugin-transform-object-rest-spread": "^7.14.5",
"nodemon": "^2.0.22"
},
"sideEffects": [
"style/*.css",
Expand Down
7 changes: 5 additions & 2 deletions js/src/widget_ngl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,16 @@ export
}, this);

this.stage.viewerControls.signals.changed.add(function () {
this.serialize_camera_orientation();
setTimeout(() => {
// https://github.com/nglviewer/nglview/issues/948#issuecomment-898121063
this.serialize_camera_orientation();
}, 100);

var m = this.stage.viewerControls.getOrientation();
if (that._synced_model_ids.length > 0 && that._ngl_focused == 1) {
that._synced_model_ids.forEach(async function (mid) {
var model = await that.model.widget_manager.get_model(mid)
for (var k in model.views) {
var pview = model.views[k];
var view = await model.views[k]
if (view.uuid != that.uuid) {
view.stage.viewerControls.orient(m);
Expand Down
4 changes: 2 additions & 2 deletions js/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = [
entry: './lib/extension.js',
output: {
filename: 'extension.js',
path: path.resolve(__dirname, '..', 'nglview', 'static'),
path: path.resolve(__dirname, '..', 'nglview', 'nbextension'),
libraryTarget: 'amd'
},
mode: 'production'
Expand All @@ -44,7 +44,7 @@ module.exports = [
entry: './lib/index.js',
output: {
filename: 'index.js',
path: path.resolve(__dirname, '..', 'nglview', 'static'),
path: path.resolve(__dirname, '..', 'nglview', 'nbextension'),
libraryTarget: 'amd'
},
devtool: 'source-map',
Expand Down
2 changes: 1 addition & 1 deletion nglview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
def _jupyter_nbextension_paths():
return [{
'section': 'notebook',
'src': 'static',
'src': 'nbextension',
'dest': 'nglview-js-widgets',
'require': 'nglview-js-widgets/extension'
}]
Expand Down
10 changes: 5 additions & 5 deletions nglview/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,15 @@ def wrap_2(*args, **kwargs):


def write_html(fp, views, frame_range=None):
# type: (str, List[NGLWidget]) -> None
"""EXPERIMENTAL. Likely will be changed.
Make html file to display a list of views. For further options, please
check `ipywidgets.embed` module.
"""
Write html file to display a list of views.
Parameters
----------
fp : str or file handle
views : a DOMWidget view or a list of views.
frame_range : None or a tuple of int
if None, do not serialize coordinates
Examples
--------
Expand Down Expand Up @@ -286,6 +284,8 @@ def on_change_layout(change):
def _set_serialization(self, frame_range=None):
self._ngl_serialize = True
resource = self._ngl_coordinate_resource
if frame_range is None and self._trajlist:
print("frame_range is not provided. Do not serialize coordinates. Default to use current frame.")
if frame_range is not None:
for t_index, traj in enumerate(self._trajlist):
resource[t_index] = []
Expand Down
119 changes: 119 additions & 0 deletions notebooks/orientation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 20,
"id": "82035655-b580-4e4d-86bf-5a90563c2840",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c7cd4ef8a8ca410c96f8cad4d48928b2",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"NGLWidget()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import nglview as nv\n",
"\n",
"view = nv.demo()\n",
"view"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "7bf1502e-ce30-4b5c-9a72-5a2360322c82",
"metadata": {},
"outputs": [],
"source": [
"view.control.rotate([0.6, -0.4, 0.3, -0.4])"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "d07a8c2b-806d-42c6-b841-e49460688348",
"metadata": {},
"outputs": [],
"source": [
"nv.write_html('index.html', view)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "f397830a-2c05-4275-bdbe-40e0b58f621a",
"metadata": {},
"outputs": [],
"source": [
"!open index.html"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "5c00397d-cdd4-4196-91e4-fb55d98774a0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[65.20023905320915,\n",
" -93.88834423662117,\n",
" 5.2160191242567215,\n",
" 0,\n",
" -31.29611474554039,\n",
" 13.040047810641841,\n",
" -93.88834423662117,\n",
" 0,\n",
" 88.67232511236445,\n",
" 31.29611474554039,\n",
" -5.216019124256737,\n",
" 0,\n",
" -37.284461975097656,\n",
" -42.47041130065918,\n",
" -44.55349826812744,\n",
" 1]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"view._camera_orientation"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 6417247

Please sign in to comment.