Skip to content

Misc changes + 0.4.2 version bump #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ dist
*.egg-info
*.pytest_cache
*.mypy_cache
__pycache__/
*.py[cod]
2 changes: 1 addition & 1 deletion examples/introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.6.8"
}
},
"nbformat": 4,
Expand Down
1 change: 0 additions & 1 deletion requirements/docs.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# copied from prod.txt
sanic >=18.12, <19.0
typing-extensions >=3.7.2, <4.0

# copied from dev.txt
pytest
Expand Down
1 change: 0 additions & 1 deletion requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
sanic >=18.12, <19.0
typing-extensions >=3.7.2, <4.0
2 changes: 1 addition & 1 deletion src/py/idom/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.4.1"
__version__ = "0.4.2"

from .core import element, Element, Events, Layout
from .widgets import node, Image, hotswap, display, html, Input
Expand Down
29 changes: 12 additions & 17 deletions src/py/idom/core/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,15 @@
from functools import wraps
import time

from typing_extensions import Protocol
from typing import Dict, Callable, Any, List, Optional, overload, Awaitable, Mapping
from typing import Dict, Callable, Any, List, Optional, overload, Awaitable

import idom

from .utils import bound_id


ElementConstructor = Callable[..., "Element"] # Element constructor


class _EF(Protocol):
"""Element function."""

def __call__(
self, element: "Element", *args: Any, **kwargs: Any
) -> Awaitable[Dict[str, Any]]:
...
ElementRenderFunction = Callable[..., Awaitable[Any]]


@overload
Expand All @@ -30,20 +21,22 @@ def element(function: Callable[..., Any]) -> ElementConstructor:


@overload
def element(*, state: Optional[str] = None) -> Callable[[_EF], ElementConstructor]:
def element(
*, state: Optional[str] = None
) -> Callable[[ElementRenderFunction], ElementConstructor]:
...


def element(
function: Optional[_EF] = None, state: Optional[str] = None
function: Optional[ElementRenderFunction] = None, state: Optional[str] = None
) -> Callable[..., Any]:
"""A decorator for defining an :class:`Element`.

Parameters:
function: The function that will render a :term:`VDOM` model.
"""

def setup(func: _EF) -> ElementConstructor:
def setup(func: ElementRenderFunction) -> ElementConstructor:
@wraps(func)
def constructor(*args: Any, **kwargs: Any) -> Element:
element = Element(func, state)
Expand Down Expand Up @@ -75,7 +68,7 @@ def id(self) -> str:
return self._element_id

@abc.abstractmethod
async def render(self) -> Mapping[str, Any]:
async def render(self) -> Any:
...

def mount(self, layout: "idom.Layout") -> None:
Expand Down Expand Up @@ -134,7 +127,9 @@ class Element(AbstractElement):
"_stop_animation",
)

def __init__(self, function: _EF, state_parameters: Optional[str]):
def __init__(
self, function: ElementRenderFunction, state_parameters: Optional[str]
):
super().__init__()
self._function = function
self._function_signature = inspect.signature(function)
Expand Down Expand Up @@ -205,7 +200,7 @@ async def wrapper() -> None:
else:
return setup(function)

async def render(self) -> Mapping[str, Any]:
async def render(self) -> Any:
"""Render the element's :term:`VDOM` model."""
# load update and reset for next render
state = self._state
Expand Down
71 changes: 35 additions & 36 deletions src/py/idom/tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from html.parser import HTMLParser as _HTMLParser

from typing import List, Tuple, Any, Dict, Union, Optional, TypeVar, Generic, Callable
from typing import List, Tuple, Any, Dict, TypeVar, Generic, Callable


_R = TypeVar("_R", bound=Any) # Var reference
Expand Down Expand Up @@ -41,18 +41,18 @@ def handle():
return idom.node("button", "Use" eventHandlers=events)
"""

__slots__ = ("__current",)
__slots__ = ("_current",)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__current conflicted with something in Generic


def __init__(self, value: _R) -> None:
self.__current = value
self._current = value

def set(self, new: _R) -> _R:
old = self.__current
self.__current = new
old = self._current
self._current = new
return old

def get(self) -> _R:
return self.__current
return self._current

def __eq__(self, other: Any) -> bool:
if isinstance(other, Var):
Expand All @@ -64,38 +64,46 @@ def __repr__(self) -> str:
return "Var(%r)" % self.get()


_ModelOrStr = Union[Dict[str, Any], str]
_ModelTransform = Callable[[_ModelOrStr], None]
_ModelTransform = Callable[[Dict[str, Any]], Any]


def html_to_vdom(
source: str, transform: Optional[_ModelTransform] = None
) -> List[Dict[str, Any]]:
def html_to_vdom(source: str, *transforms: _ModelTransform) -> Dict[str, Any]:
"""Transform HTML into a DOM model

Parameters:
source:
The raw HTML as a string
transform:
A function for transforming each model as it is created. For example,
you might use a transform function to add highlighting to a ``<code/>``
block.
transforms:
Functions of the form ``transform(old) -> new`` where ``old`` is a VDOM
dictionary which will be replaced by ``new``. You might use a transform
function to add highlighting to a ``<code/>`` block.
"""
parser = HtmlParser(transform)
parser = HtmlParser()
parser.feed(source)
return parser.model()
root = parser.model()
to_visit = [root]
while to_visit:
node = to_visit.pop(0)
if isinstance(node, dict) and "children" in node:
transformed = []
for child in node["children"]:
if isinstance(child, dict):
for t in transforms:
child = t(child)
if child is not None:
transformed.append(child)
to_visit.append(child)
node["children"] = transformed
if "attributes" in node and not node["attributes"]:
del node["attributes"]
if "children" in node and not node["children"]:
del node["children"]
return root


class HtmlParser(_HTMLParser):
def __init__(self, transform: Optional[_ModelTransform] = None):
super().__init__()
if transform is not None:
self._transform = transform

def model(self) -> List[Dict[str, Any]]:
root: Dict[str, Any] = self._node_stack[0]
root_children: List[Dict[str, Any]] = root["children"]
return root_children
def model(self) -> Dict[str, Any]:
return self._node_stack[0]

def feed(self, data: str) -> None:
self._node_stack.append(self._make_node("div", {}))
Expand All @@ -112,12 +120,7 @@ def handle_starttag(self, tag: str, attrs: List[Tuple[str, str]]) -> None:
self._node_stack.append(new)

def handle_endtag(self, tag: str) -> None:
node = self._node_stack.pop(-1)
self._transform(node)
if not node["attributes"]:
del node["attributes"]
if not node["children"]:
del node["children"]
del self._node_stack[-1]

def handle_data(self, data: str) -> None:
self._node_stack[-1]["children"].append(data)
Expand All @@ -134,7 +137,3 @@ def _make_node(tag: str, attrs: Dict[str, Any]) -> Dict[str, Any]:
style_dict[camel_case_key] = v
attrs["style"] = style_dict
return {"tagName": tag, "attributes": attrs, "children": []}

@staticmethod
def _transform(node: Dict[str, Any]) -> None:
...
47 changes: 27 additions & 20 deletions src/py/tests/test_tools.py → src/py/tests/test_idom/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ def test_var_get():
],
)
def test_html_to_vdom(case):
assert html_to_vdom(case["source"]) == [case["model"]]
assert html_to_vdom(case["source"]) == {
"tagName": "div",
"children": [case["model"]],
}


def test_html_to_vdom_transform():
Expand All @@ -59,23 +62,27 @@ def test_html_to_vdom_transform():
def make_links_blue(node):
if node["tagName"] == "a":
node["attributes"]["style"] = {"color": "blue"}
return node

assert html_to_vdom(source, make_links_blue) == [
{
"tagName": "p",
"children": [
"hello ",
{
"tagName": "a",
"children": ["world"],
"attributes": {"style": {"color": "blue"}},
},
" and ",
{
"tagName": "a",
"children": ["universe"],
"attributes": {"style": {"color": "blue"}},
},
],
}
]
expected = {
"tagName": "p",
"children": [
"hello ",
{
"tagName": "a",
"children": ["world"],
"attributes": {"style": {"color": "blue"}},
},
" and ",
{
"tagName": "a",
"children": ["universe"],
"attributes": {"style": {"color": "blue"}},
},
],
}

assert html_to_vdom(source, make_links_blue) == {
"tagName": "div",
"children": [expected],
}