Skip to content

Commit 1fa1329

Browse files
committed
Add new example notebooks demonstrating various template features
1 parent 7162f53 commit 1fa1329

35 files changed

+1817
-37
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ build-playground: check-jq
2828
WHL_FILE=$$(ls pypi | grep .whl) ;\
2929
python tools/patch_jlite_json.py \
3030
public/lite/jupyter-lite.json \
31-
--whl-url "pypi/$$WHL_FILE" && \
31+
--whl-url "pypi/$$WHL_FILE" \
32+
--disable-dirty && \
3233
cp -frpv pyodide public/lite/ && \
3334
cp -frpv pypi public/lite/extensions/@jupyterlite/pyodide-kernel-extension/static/
3435

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "1051a8e7",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"\"\"\"Boolean attribute values are reduced during rendering.\"\"\""
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "394eacaf",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"from tdom import html"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"id": "7b4d8476",
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"def main():\n",
31+
" \"\"\"Main entry point.\"\"\"\n",
32+
" result = html(t\"<div editable={True}>Hello World</div>\")\n",
33+
" return result"
34+
]
35+
}
36+
],
37+
"metadata": {
38+
"jupytext": {
39+
"cell_metadata_filter": "-all",
40+
"main_language": "python",
41+
"notebook_metadata_filter": "-all",
42+
"text_representation": {
43+
"extension": ".py",
44+
"format_name": "light"
45+
}
46+
}
47+
},
48+
"nbformat": 4,
49+
"nbformat_minor": 5
50+
}

content/call_function.ipynb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "eb0e8234",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"\"\"\"Call a function from inside a template expression.\"\"\""
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "f508f146",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"from tdom import html"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"id": "368c5c9f",
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"def make_bigly(name: str) -> str:\n",
31+
" \"\"\"A function returning a string, rather than a component.\"\"\"\n",
32+
" return f\"BIGLY: {name.upper()}\""
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"id": "2d56b175",
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"def main():\n",
43+
" \"\"\"Main entry point.\"\"\"\n",
44+
" name = \"viewdom\"\n",
45+
" result = html(t\"<div>Hello {make_bigly(name)}</div>\")\n",
46+
" return result"
47+
]
48+
}
49+
],
50+
"metadata": {
51+
"jupytext": {
52+
"cell_metadata_filter": "-all",
53+
"main_language": "python",
54+
"notebook_metadata_filter": "-all",
55+
"text_representation": {
56+
"extension": ".py",
57+
"format_name": "light"
58+
}
59+
}
60+
},
61+
"nbformat": 4,
62+
"nbformat_minor": 5
63+
}

content/child_nodes.ipynb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "19a08004",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"\"\"\"Child nodes become part of the structure.\"\"\"\n",
11+
"from tdom import html"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"id": "f8bf6dee",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"def main():\n",
22+
" \"\"\"Main entry point.\"\"\"\n",
23+
" structure = html(t\"<div>Hello <span>World<em>!</em></span></div>\")\n",
24+
" return structure"
25+
]
26+
}
27+
],
28+
"metadata": {
29+
"jupytext": {
30+
"cell_metadata_filter": "-all",
31+
"main_language": "python",
32+
"notebook_metadata_filter": "-all",
33+
"text_representation": {
34+
"extension": ".py",
35+
"format_name": "light"
36+
}
37+
}
38+
},
39+
"nbformat": 4,
40+
"nbformat_minor": 5
41+
}

content/children_props.ipynb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "14ec6992",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"\"\"\"Children as props.\"\"\""
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "faf088d2",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"from tdom import html"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"id": "89f33397",
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"def Heading(title, children):\n",
31+
" \"\"\"The default heading.\"\"\"\n",
32+
" return html(t\"<h1>{title}</h1><div>{children}</div>\")"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"id": "7a01befa",
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"def main():\n",
43+
" \"\"\"Main entry point.\"\"\"\n",
44+
" result = html(t'<{Heading} title=\"My Title\">Child<//>')\n",
45+
" return result"
46+
]
47+
}
48+
],
49+
"metadata": {
50+
"jupytext": {
51+
"cell_metadata_filter": "-all",
52+
"main_language": "python",
53+
"notebook_metadata_filter": "-all",
54+
"text_representation": {
55+
"extension": ".py",
56+
"format_name": "light"
57+
}
58+
}
59+
},
60+
"nbformat": 4,
61+
"nbformat_minor": 5
62+
}

content/conditional_default.ipynb

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "e46ec9e8",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"\"\"\"An expression which chooses subcomponent based on condition.\"\"\""
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "d4bc6a1f",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"from tdom import html"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"id": "25e7eb68",
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"def DefaultHeading():\n",
31+
" \"\"\"The default heading.\"\"\"\n",
32+
" return html(t\"<h1>Default Heading</h1>\")"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"id": "6820086a",
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"def OtherHeading():\n",
43+
" \"\"\"Another heading used in another condition.\"\"\"\n",
44+
" return html(t\"<h1>Other Heading</h1>\")"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"id": "cbc747a0",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"def Body(heading):\n",
55+
" \"\"\"Render the body with a heading based on which is passed in.\"\"\"\n",
56+
" return html(t\"<body>{heading if heading else DefaultHeading}</body>\")"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"id": "9461dc1c",
63+
"metadata": {},
64+
"outputs": [],
65+
"source": [
66+
"def main():\n",
67+
" \"\"\"Main entry point.\"\"\"\n",
68+
" result = html(t\"<{Body} heading={OtherHeading}/>\")\n",
69+
" return result"
70+
]
71+
}
72+
],
73+
"metadata": {
74+
"jupytext": {
75+
"cell_metadata_filter": "-all",
76+
"main_language": "python",
77+
"notebook_metadata_filter": "-all",
78+
"text_representation": {
79+
"extension": ".py",
80+
"format_name": "light"
81+
}
82+
}
83+
},
84+
"nbformat": 4,
85+
"nbformat_minor": 5
86+
}

content/dataclass_component.ipynb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "5d789b9f",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"\"\"\"Components can be any kind of dataclass_component.\"\"\""
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "006e1b15",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"from dataclasses import dataclass"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"id": "f408ac5e",
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"from tdom import html"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"id": "a3f47214",
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"@dataclass\n",
41+
"class Greeting:\n",
42+
" \"\"\"Give a greeting.\"\"\"\n",
43+
"\n",
44+
" name: str\n",
45+
"\n",
46+
" def __call__(self):\n",
47+
" \"\"\"Render to a string.\"\"\"\n",
48+
" return f\"Hello {self.name}\""
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": null,
54+
"id": "fbb09b3b",
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"def main():\n",
59+
" \"\"\"Render a template to a string.\"\"\"\n",
60+
" greeting = Greeting(name=\"viewdom\")\n",
61+
" # TODO Teach the constructor to make dataclass components\n",
62+
" result = html(t\"<div><{greeting} /></div>\")\n",
63+
" return result"
64+
]
65+
}
66+
],
67+
"metadata": {
68+
"jupytext": {
69+
"cell_metadata_filter": "-all",
70+
"main_language": "python",
71+
"notebook_metadata_filter": "-all",
72+
"text_representation": {
73+
"extension": ".py",
74+
"format_name": "light"
75+
}
76+
}
77+
},
78+
"nbformat": 4,
79+
"nbformat_minor": 5
80+
}

0 commit comments

Comments
 (0)