Skip to content

Commit f1c185d

Browse files
committed
re-organize widgets directory
1 parent 71b7640 commit f1c185d

File tree

15 files changed

+353
-300
lines changed

15 files changed

+353
-300
lines changed

docs/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from sanic import Sanic, response
77

88
import idom
9-
from idom.client.manage import web_modules_dir
9+
from idom.config import IDOM_WED_MODULES_DIR
1010
from idom.server.sanic import PerClientStateServer
11-
from idom.widgets.utils import multiview
11+
from idom.widgets import multiview
1212

1313

1414
HERE = Path(__file__).parent
@@ -18,7 +18,7 @@
1818
def make_app():
1919
app = Sanic(__name__)
2020
app.static("/docs", str(HERE / "build"))
21-
app.static("/_modules", str(web_modules_dir()))
21+
app.static("/_modules", str(IDOM_WED_MODULES_DIR.current))
2222

2323
@app.route("/")
2424
async def forward_to_index(request):

docs/source/auto/api-reference.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ API Reference
5555
.. automodule:: idom.utils
5656
:members:
5757

58-
.. automodule:: idom.widgets.html
58+
.. automodule:: idom.html
5959
:members:
6060

61-
.. automodule:: idom.widgets.utils
61+
.. automodule:: idom.widgets
6262
:members:
6363

6464
Misc Modules

docs/source/examples.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Simply install your javascript library of choice using the ``idom`` CLI:
5959
6060
idom install victory
6161
62-
Then import the module with :class:`~idom.widgets.utils.Module`:
62+
Then import the module with :mod:`~idom.web.module`:
6363

6464
.. example:: victory_chart
6565

scripts/one_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from threading import Thread
66

77
import idom
8-
from idom.widgets.utils import hotswap
8+
from idom.widgets import hotswap
99

1010

1111
here = Path(__file__).parent

src/idom/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010

1111
__author__ = "idom-team"
1212

13-
from . import config, log, web
13+
from . import config, html, log, web
1414
from .core import hooks
1515
from .core.component import Component, component
1616
from .core.events import Events, event
1717
from .core.layout import Layout
1818
from .core.vdom import VdomDict, vdom
1919
from .server.prefab import run
2020
from .utils import Ref, html_to_vdom
21-
from .widgets.html import html
22-
from .widgets.utils import hotswap, multiview
21+
from .widgets import hotswap, multiview
2322

2423

2524
__all__ = [

src/idom/core/vdom.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class _VdomDictRequired(TypedDict, total=True):
106106

107107

108108
class VdomDict(_VdomDictRequired, _VdomDictOptional):
109-
"""A VDOM dictionary"""
109+
"""A VDOM dictionary - see :ref:`VDOM Mimetype` for more info"""
110110

111111

112112
_AttributesAndChildrenArg = Union[Mapping[str, Any], str, Iterable[Any], Any]
@@ -201,7 +201,7 @@ def constructor(
201201
qualname_prefix = constructor.__qualname__.rsplit(".", 1)[0]
202202
constructor.__qualname__ = qualname_prefix + f".{tag}"
203203
constructor.__doc__ = (
204-
f"""Create a new ``<{tag}/>`` - returns :ref:`VDOM <VDOM Mimetype>`."""
204+
f"""Create a new ``<{tag}/>`` - returns a :class:`VdomDict`."""
205205
)
206206

207207
return constructor

src/idom/html.py

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
"""
2+
Standard HTML Elements
3+
======================
4+
5+
6+
External sources
7+
----------------
8+
9+
link = make_vdom_constructor("link", allow_children=False)
10+
11+
12+
Content Sectioning
13+
------------------
14+
15+
- :func:`style`
16+
- :func:`address`
17+
- :func:`article`
18+
- :func:`aside`
19+
- :func:`footer`
20+
- :func:`h1`
21+
- :func:`h2`
22+
- :func:`h3`
23+
- :func:`h4`
24+
- :func:`h5`
25+
- :func:`h6`
26+
- :func:`header`
27+
- :func:`hgroup`
28+
- :func:`nav`
29+
- :func:`section`
30+
31+
32+
Text Content
33+
------------
34+
- :func:`blockquote`
35+
- :func:`dd`
36+
- :func:`div`
37+
- :func:`dl`
38+
- :func:`dt`
39+
- :func:`figcaption`
40+
- :func:`figure`
41+
- :func:`hr`
42+
- :func:`li`
43+
- :func:`ol`
44+
- :func:`p`
45+
- :func:`pre`
46+
- :func:`ul`
47+
48+
49+
Inline Text Semantics
50+
---------------------
51+
52+
- :func:`a`
53+
- :func:`abbr`
54+
- :func:`b`
55+
- :func:`br`
56+
- :func:`cite`
57+
- :func:`code`
58+
- :func:`data`
59+
- :func:`em`
60+
- :func:`i`
61+
- :func:`kbd`
62+
- :func:`mark`
63+
- :func:`q`
64+
- :func:`s`
65+
- :func:`samp`
66+
- :func:`small`
67+
- :func:`span`
68+
- :func:`strong`
69+
- :func:`sub`
70+
- :func:`sup`
71+
- :func:`time`
72+
- :func:`u`
73+
- :func:`var`
74+
75+
76+
Image and video
77+
---------------
78+
79+
- :func:`img`
80+
- :func:`audio`
81+
- :func:`video`
82+
- :func:`source`
83+
84+
85+
Table Content
86+
-------------
87+
88+
- :func:`caption`
89+
- :func:`col`
90+
- :func:`colgroup`
91+
- :func:`table`
92+
- :func:`tbody`
93+
- :func:`td`
94+
- :func:`tfoot`
95+
- :func:`th`
96+
- :func:`thead`
97+
- :func:`tr`
98+
99+
100+
Forms
101+
-----
102+
103+
- :func:`meter`
104+
- :func:`output`
105+
- :func:`progress`
106+
- :func:`input`
107+
- :func:`button`
108+
- :func:`label`
109+
- :func:`fieldset`
110+
- :func:`legend`
111+
112+
113+
Interactive Elements
114+
--------------------
115+
116+
- :func:`details`
117+
- :func:`dialog`
118+
- :func:`menu`
119+
- :func:`menuitem`
120+
- :func:`summary`
121+
"""
122+
123+
from .core.vdom import make_vdom_constructor
124+
125+
126+
# External sources
127+
link = make_vdom_constructor("link", allow_children=False)
128+
129+
# Content sectioning
130+
style = make_vdom_constructor("style")
131+
address = make_vdom_constructor("address")
132+
article = make_vdom_constructor("article")
133+
aside = make_vdom_constructor("aside")
134+
footer = make_vdom_constructor("footer")
135+
h1 = make_vdom_constructor("h1")
136+
h2 = make_vdom_constructor("h2")
137+
h3 = make_vdom_constructor("h3")
138+
h4 = make_vdom_constructor("h4")
139+
h5 = make_vdom_constructor("h5")
140+
h6 = make_vdom_constructor("h6")
141+
header = make_vdom_constructor("header")
142+
hgroup = make_vdom_constructor("hgroup")
143+
nav = make_vdom_constructor("nav")
144+
section = make_vdom_constructor("section")
145+
146+
# Text content
147+
blockquote = make_vdom_constructor("blockquote")
148+
dd = make_vdom_constructor("dd")
149+
div = make_vdom_constructor("div")
150+
dl = make_vdom_constructor("dl")
151+
dt = make_vdom_constructor("dt")
152+
figcaption = make_vdom_constructor("figcaption")
153+
figure = make_vdom_constructor("figure")
154+
hr = make_vdom_constructor("hr", allow_children=False)
155+
li = make_vdom_constructor("li")
156+
ol = make_vdom_constructor("ol")
157+
p = make_vdom_constructor("p")
158+
pre = make_vdom_constructor("pre")
159+
ul = make_vdom_constructor("ul")
160+
161+
# Inline text semantics
162+
a = make_vdom_constructor("a")
163+
abbr = make_vdom_constructor("abbr")
164+
b = make_vdom_constructor("b")
165+
br = make_vdom_constructor("br", allow_children=False)
166+
cite = make_vdom_constructor("cite")
167+
code = make_vdom_constructor("code")
168+
data = make_vdom_constructor("data")
169+
em = make_vdom_constructor("em")
170+
i = make_vdom_constructor("i")
171+
kbd = make_vdom_constructor("kbd")
172+
mark = make_vdom_constructor("mark")
173+
q = make_vdom_constructor("q")
174+
s = make_vdom_constructor("s")
175+
samp = make_vdom_constructor("samp")
176+
small = make_vdom_constructor("small")
177+
span = make_vdom_constructor("span")
178+
strong = make_vdom_constructor("strong")
179+
sub = make_vdom_constructor("sub")
180+
sup = make_vdom_constructor("sup")
181+
time = make_vdom_constructor("time")
182+
u = make_vdom_constructor("u")
183+
var = make_vdom_constructor("var")
184+
185+
# Image and video
186+
img = make_vdom_constructor("img", allow_children=False)
187+
audio = make_vdom_constructor("audio")
188+
video = make_vdom_constructor("video")
189+
source = make_vdom_constructor("source", allow_children=False)
190+
191+
# Table content
192+
caption = make_vdom_constructor("caption")
193+
col = make_vdom_constructor("col")
194+
colgroup = make_vdom_constructor("colgroup")
195+
table = make_vdom_constructor("table")
196+
tbody = make_vdom_constructor("tbody")
197+
td = make_vdom_constructor("td")
198+
tfoot = make_vdom_constructor("tfoot")
199+
th = make_vdom_constructor("th")
200+
thead = make_vdom_constructor("thead")
201+
tr = make_vdom_constructor("tr")
202+
203+
# Forms
204+
meter = make_vdom_constructor("meter")
205+
output = make_vdom_constructor("output")
206+
progress = make_vdom_constructor("progress")
207+
input = make_vdom_constructor("input", allow_children=False)
208+
button = make_vdom_constructor("button")
209+
label = make_vdom_constructor("label")
210+
fieldset = make_vdom_constructor("fieldset")
211+
legend = make_vdom_constructor("legend")
212+
213+
# Interactive elements
214+
details = make_vdom_constructor("details")
215+
dialog = make_vdom_constructor("dialog")
216+
menu = make_vdom_constructor("menu")
217+
menuitem = make_vdom_constructor("menuitem")
218+
summary = make_vdom_constructor("summary")

src/idom/server/prefab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Any, Dict, Optional, Tuple, TypeVar
88

99
from idom.core.component import ComponentConstructor
10-
from idom.widgets.utils import MountFunc, MultiViewMount, hotswap, multiview
10+
from idom.widgets import MountFunc, MultiViewMount, hotswap, multiview
1111

1212
from .proto import Server, ServerFactory
1313
from .utils import find_available_port, find_builtin_server_type

src/idom/web/module.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
Web Modules
3+
===========
4+
"""
5+
16
from __future__ import annotations
27

38
from dataclasses import dataclass

src/idom/widgets/utils.py renamed to src/idom/widgets.py

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,65 @@
44
"""
55
from __future__ import annotations
66

7-
from typing import Any, Callable, Dict, Optional, Set, Tuple, TypeVar
7+
from base64 import b64encode
8+
from typing import Any, Callable, Dict, Optional, Set, Tuple, TypeVar, Union
89

9-
from idom.core import hooks
10-
from idom.core.component import ComponentConstructor, component
11-
from idom.utils import Ref
10+
import idom
11+
12+
from . import html
13+
from .core import hooks
14+
from .core.component import ComponentConstructor, component
15+
from .core.vdom import VdomDict
16+
from .utils import Ref
17+
18+
19+
def image(
20+
format: str,
21+
value: Union[str, bytes] = "",
22+
attributes: Optional[Dict[str, Any]] = None,
23+
) -> VdomDict:
24+
"""Utility for constructing an image from a string or bytes
25+
26+
The source value will automatically be encoded to base64
27+
"""
28+
if format == "svg":
29+
format = "svg+xml"
30+
31+
if isinstance(value, str):
32+
bytes_value = value.encode()
33+
else:
34+
bytes_value = value
35+
36+
base64_value = b64encode(bytes_value).decode()
37+
src = f"data:image/{format};base64,{base64_value}"
38+
39+
return {"tagName": "img", "attributes": {"src": src, **(attributes or {})}}
40+
41+
42+
@component
43+
def Input(
44+
callback: Callable[[str], None],
45+
type: str,
46+
value: str = "",
47+
attributes: Optional[Dict[str, Any]] = None,
48+
cast: Optional[Callable[[str], Any]] = None,
49+
ignore_empty: bool = True,
50+
) -> VdomDict:
51+
"""Utility for making an ``<input/>`` with a callback"""
52+
attrs = attributes or {}
53+
value, set_value = idom.hooks.use_state(value)
54+
55+
events = idom.Events()
56+
57+
@events.on("change")
58+
def on_change(event: Dict[str, Any]) -> None:
59+
value = event["value"]
60+
set_value(value)
61+
if not value and ignore_empty:
62+
return
63+
callback(value if cast is None else cast(value))
64+
65+
return html.input({"type": type, "value": value, **attrs}, event_handlers=events)
1266

1367

1468
MountFunc = Callable[[ComponentConstructor], None]

0 commit comments

Comments
 (0)