Skip to content

Commit 522b5a9

Browse files
committed
move jupyter out of base idom and into idom-jupyter
1 parent 40f7005 commit 522b5a9

File tree

15 files changed

+213
-557
lines changed

15 files changed

+213
-557
lines changed

binder/postBuild

Lines changed: 0 additions & 6 deletions
This file was deleted.

binder/requirements.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/source/api.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,21 @@ Useful Tools
5757
:members:
5858

5959

60-
Jupyter Widget
61-
--------------
62-
63-
.. automodule:: idom.widgets.jupyter
64-
:members:
65-
66-
6760
HTML Widgets
6861
------------
6962

7063
.. automodule:: idom.widgets.html
7164
:members:
7265
:undoc-members:
7366

67+
68+
Import Javascript Modules
69+
-------------------------
70+
71+
.. automodule:: idom.widgets.module
72+
:members:
73+
74+
7475
Widget Tools
7576
------------
7677

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@ cause the widget to change 🖱️
8989
.. _pull request: https://github.com/rmorshea/idom/pulls
9090
.. _IDOM Sandbox: https://idom-sandbox.herokuapp.com
9191
.. |launch-binder| image:: https://mybinder.org/badge_logo.svg
92-
:target: https://mybinder.org/v2/gh/rmorshea/idom/master?filepath=notebooks%2Fintroduction.ipynb
92+
:target: https://mybinder.org/v2/gh/idom-team/idom-jupyter/master?filepath=notebooks%2Fintroduction.ipynb

idom/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from .core import hooks
1111

1212
from .widgets.html import html
13-
from .widgets.utils import Module, Import, hotswap, multiview
14-
from .widgets.jupyter import JupyterDisplay
13+
from .widgets.module import Module, Import
14+
from .widgets.utils import hotswap, multiview
1515

1616
from .utils import Ref, html_to_vdom
1717

idom/widgets/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from .utils import Module, Import, hotswap, multiview
2-
from .jupyter import JupyterDisplay
1+
from .utils import hotswap, multiview
2+
from .module import Module, Import
33
from .html import html, image, Input
44

55
__all__ = [
6-
"JupyterDisplay",
76
"node",
87
"hotswap",
98
"multiview",

idom/widgets/jupyter.py

Lines changed: 0 additions & 102 deletions
This file was deleted.

idom/widgets/module.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
from pathlib import Path
2+
from typing import Any, Optional, Union
3+
4+
from idom import client
5+
from idom.core.vdom import VdomDict, ImportSourceDict, make_vdom_constructor
6+
7+
8+
class Module:
9+
"""A Javascript module
10+
11+
Parameters:
12+
name:
13+
The module's name. If ``install`` or ``source`` are provided omit the ``.js``
14+
file extension. Otherwise this is the exact import path and could be anything
15+
including a URL.
16+
install:
17+
If a string, then the dependency string used to install a module with
18+
the given ``name`` (e.g. ``[email protected]``). If ``True`` then the given
19+
``name`` will be used as the dependency string.
20+
source:
21+
Create a module of the given name using the given source code.
22+
23+
Returns:
24+
An :class:`Import` element for the newly defined module.
25+
"""
26+
27+
__slots__ = ("_module", "_name", "_installed")
28+
29+
def __init__(
30+
self,
31+
name: str,
32+
install: Union[bool, str] = False,
33+
source: Optional[Union[str, Path]] = None,
34+
replace: bool = False,
35+
) -> None:
36+
self._installed = False
37+
if install and source:
38+
raise ValueError("Both 'install' and 'source' were given.")
39+
elif (install or source) and not replace and client.web_module_exists(name):
40+
self._module = client.web_module_url(name)
41+
self._installed = True
42+
self._name = name
43+
elif source is not None:
44+
self._module = client.register_web_module(name, source)
45+
self._installed = True
46+
self._name = name
47+
elif isinstance(install, str):
48+
client.install([install], [name])
49+
self._module = client.web_module_url(name)
50+
self._installed = True
51+
self._name = name
52+
elif install is True:
53+
client.install(name)
54+
self._module = client.web_module_url(name)
55+
self._installed = True
56+
self._name = name
57+
elif client.web_module_exists(name):
58+
self._module = client.web_module_url(name)
59+
else:
60+
self._module = name
61+
62+
@property
63+
def name(self) -> str:
64+
if not self._installed:
65+
raise ValueError("Module is not installed locally")
66+
return self._name
67+
68+
@property
69+
def url(self) -> str:
70+
return self._module
71+
72+
def Import(self, name: str, *args: Any, **kwargs: Any) -> "Import":
73+
return Import(self._module, name, *args, **kwargs)
74+
75+
def delete(self) -> None:
76+
if not self._installed:
77+
raise ValueError("Module is not installed locally")
78+
client.delete_web_modules([self._name])
79+
80+
def __repr__(self) -> str: # pragma: no cover
81+
return f"{type(self).__name__}({self._module!r})"
82+
83+
84+
class Import:
85+
"""Import a react module
86+
87+
Once imported, you can instantiate the library's components by calling them
88+
via attribute-access.
89+
90+
Examples:
91+
92+
.. code-block:: python
93+
94+
victory = idom.Import("victory", "VictoryBar" install=True)
95+
style = {"parent": {"width": "500px"}}
96+
victory.VictoryBar({"style": style}, fallback="loading...")
97+
"""
98+
99+
__slots__ = ("_constructor", "_import_source")
100+
101+
def __init__(
102+
self,
103+
module: str,
104+
name: str,
105+
has_children: bool = True,
106+
fallback: Optional[str] = "",
107+
) -> None:
108+
self._constructor = make_vdom_constructor(name, has_children)
109+
self._import_source = ImportSourceDict(source=module, fallback=fallback)
110+
111+
def __call__(
112+
self,
113+
*args: Any,
114+
**kwargs: Any,
115+
) -> VdomDict:
116+
return self._constructor(import_source=self._import_source, *args, **kwargs)
117+
118+
def __repr__(self) -> str: # pragma: no cover
119+
items = ", ".join(f"{k}={v!r}" for k, v in self._import_source.items())
120+
return f"{type(self).__name__}({items})"

0 commit comments

Comments
 (0)