Skip to content

Commit

Permalink
Merge 4a767ed into 3cd9d5b
Browse files Browse the repository at this point in the history
  • Loading branch information
hainm committed Jun 16, 2019
2 parents 3cd9d5b + 4a767ed commit 15d6e56
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions nglview/__init__.py
Expand Up @@ -26,6 +26,7 @@ def _jupyter_nbextension_paths():
from .adaptor import *
from .show import *
from . import datafiles
from .data_source import DatasourceRegistry

# utils
from .utils import widget_utils, js_utils
Expand Down
27 changes: 27 additions & 0 deletions nglview/data_source.py
@@ -0,0 +1,27 @@
from .utils import js_utils


class DatasourceRegistry:
sources = {}

@classmethod
def add(cls, name, url):
"""
Examples
--------
>>> import nglview as nv
>>> from nglview.data_source import DatasourceRegistry # doctest: +SKIP
... DatasourceRegistry.add("data", "//cdn.rawgit.com/arose/ngl/v2.0.0-dev.32/data/")
... view = nv.NGLWidget()
... v.add_component('data://1CRN.cif') # 1CRN.cif is from above url
... v
"""
cls.sources[name] = url

js_utils.run(f"""
var NGL = require("nglview-js-widgets").NGL;
NGL.DatasourceRegistry.add("{name}",
new NGL.StaticDatasource("{url}"))
""")
7 changes: 5 additions & 2 deletions nglview/utils/py_utils.py
Expand Up @@ -6,6 +6,7 @@
from contextlib import contextmanager
import tempfile
from shutil import rmtree
from ..data_source import DatasourceRegistry

__all__ = [
'encode_base64', 'decode_base64', 'seq_to_string', '_camelize',
Expand Down Expand Up @@ -258,5 +259,7 @@ def is_binary(self):

@property
def is_url(self):
return (isinstance(self.src, str) and (
self.src.startswith('http') or self.src.startswith('rcsb://')))
url_ext = ['http', 'rcsb://', 'data://'] + \
[f"{k}://" for k in DatasourceRegistry.sources]
return (isinstance(self.src, str) and
self.src.startswith(tuple(url_ext)))

0 comments on commit 15d6e56

Please sign in to comment.