Skip to content

Commit

Permalink
move jupyter out of base idom and into idom-jupyter
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Sep 15, 2020
1 parent 40f7005 commit 522b5a9
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 557 deletions.
6 changes: 0 additions & 6 deletions binder/postBuild

This file was deleted.

1 change: 0 additions & 1 deletion binder/requirements.txt

This file was deleted.

15 changes: 8 additions & 7 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,21 @@ Useful Tools
:members:


Jupyter Widget
--------------

.. automodule:: idom.widgets.jupyter
:members:


HTML Widgets
------------

.. automodule:: idom.widgets.html
:members:
:undoc-members:


Import Javascript Modules
-------------------------

.. automodule:: idom.widgets.module
:members:


Widget Tools
------------

Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ cause the widget to change 🖱️
.. _pull request: https://github.com/rmorshea/idom/pulls
.. _IDOM Sandbox: https://idom-sandbox.herokuapp.com
.. |launch-binder| image:: https://mybinder.org/badge_logo.svg
:target: https://mybinder.org/v2/gh/rmorshea/idom/master?filepath=notebooks%2Fintroduction.ipynb
:target: https://mybinder.org/v2/gh/idom-team/idom-jupyter/master?filepath=notebooks%2Fintroduction.ipynb
4 changes: 2 additions & 2 deletions idom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from .core import hooks

from .widgets.html import html
from .widgets.utils import Module, Import, hotswap, multiview
from .widgets.jupyter import JupyterDisplay
from .widgets.module import Module, Import
from .widgets.utils import hotswap, multiview

from .utils import Ref, html_to_vdom

Expand Down
5 changes: 2 additions & 3 deletions idom/widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from .utils import Module, Import, hotswap, multiview
from .jupyter import JupyterDisplay
from .utils import hotswap, multiview
from .module import Module, Import
from .html import html, image, Input

__all__ = [
"JupyterDisplay",
"node",
"hotswap",
"multiview",
Expand Down
102 changes: 0 additions & 102 deletions idom/widgets/jupyter.py

This file was deleted.

120 changes: 120 additions & 0 deletions idom/widgets/module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from pathlib import Path
from typing import Any, Optional, Union

from idom import client
from idom.core.vdom import VdomDict, ImportSourceDict, make_vdom_constructor


class Module:
"""A Javascript module
Parameters:
name:
The module's name. If ``install`` or ``source`` are provided omit the ``.js``
file extension. Otherwise this is the exact import path and could be anything
including a URL.
install:
If a string, then the dependency string used to install a module with
the given ``name`` (e.g. ``my-module@1.2.3``). If ``True`` then the given
``name`` will be used as the dependency string.
source:
Create a module of the given name using the given source code.
Returns:
An :class:`Import` element for the newly defined module.
"""

__slots__ = ("_module", "_name", "_installed")

def __init__(
self,
name: str,
install: Union[bool, str] = False,
source: Optional[Union[str, Path]] = None,
replace: bool = False,
) -> None:
self._installed = False
if install and source:
raise ValueError("Both 'install' and 'source' were given.")
elif (install or source) and not replace and client.web_module_exists(name):
self._module = client.web_module_url(name)
self._installed = True
self._name = name
elif source is not None:
self._module = client.register_web_module(name, source)
self._installed = True
self._name = name
elif isinstance(install, str):
client.install([install], [name])
self._module = client.web_module_url(name)
self._installed = True
self._name = name
elif install is True:
client.install(name)
self._module = client.web_module_url(name)
self._installed = True
self._name = name
elif client.web_module_exists(name):
self._module = client.web_module_url(name)
else:
self._module = name

@property
def name(self) -> str:
if not self._installed:
raise ValueError("Module is not installed locally")
return self._name

@property
def url(self) -> str:
return self._module

def Import(self, name: str, *args: Any, **kwargs: Any) -> "Import":
return Import(self._module, name, *args, **kwargs)

def delete(self) -> None:
if not self._installed:
raise ValueError("Module is not installed locally")
client.delete_web_modules([self._name])

def __repr__(self) -> str: # pragma: no cover
return f"{type(self).__name__}({self._module!r})"


class Import:
"""Import a react module
Once imported, you can instantiate the library's components by calling them
via attribute-access.
Examples:
.. code-block:: python
victory = idom.Import("victory", "VictoryBar" install=True)
style = {"parent": {"width": "500px"}}
victory.VictoryBar({"style": style}, fallback="loading...")
"""

__slots__ = ("_constructor", "_import_source")

def __init__(
self,
module: str,
name: str,
has_children: bool = True,
fallback: Optional[str] = "",
) -> None:
self._constructor = make_vdom_constructor(name, has_children)
self._import_source = ImportSourceDict(source=module, fallback=fallback)

def __call__(
self,
*args: Any,
**kwargs: Any,
) -> VdomDict:
return self._constructor(import_source=self._import_source, *args, **kwargs)

def __repr__(self) -> str: # pragma: no cover
items = ", ".join(f"{k}={v!r}" for k, v in self._import_source.items())
return f"{type(self).__name__}({items})"
Loading

0 comments on commit 522b5a9

Please sign in to comment.