Skip to content

Commit 134648a

Browse files
authored
Plugin docs (#8)
* Start working on plugin docs. * Add plugin docs to menu/reference. * Move app_template_demo.py -> demos/ * Add plugin demos. * Make plugin demos available to use in code examples. * Plugins documentation. * Move plugins docs to Guide * Add file extension section. * Make boxy_counter a subclass of counter. * Plugin doc improvements. * Make plugin examples available as local variables to `exec()` * Remove unnecessary IIFE. * Plugin docs fixes. * Docs fixes. * Remove boxy plugins section.
1 parent dfb5a33 commit 134648a

File tree

14 files changed

+565
-2
lines changed

14 files changed

+565
-2
lines changed

hyperdiv_docs/code_examples.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
22
from textwrap import dedent as dedent_text
33
import hyperdiv as hd
4+
from .demos.counter_plugin import counter, boxy_counter
5+
from .demos.leaflet_plugin import leaflet
46

57

68
def parse_doc(doc):
@@ -63,7 +65,15 @@ def code_example(code, code_to_execute=None):
6365
state.error = None
6466
else:
6567
try:
66-
exec(dedent_text(code_to_execute), globals(), globals())
68+
exec(
69+
dedent_text(code_to_execute),
70+
globals(),
71+
dict(
72+
counter=counter,
73+
boxy_counter=boxy_counter,
74+
leaflet=leaflet,
75+
),
76+
)
6777
except Exception as e:
6878
state.error = str(e)
6979

hyperdiv_docs/demos/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .counter import counter, boxy_counter
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
button {
2+
padding: 0.5rem;
3+
background-color: var(--sl-color-blue-100);
4+
border: none;
5+
border-radius: var(--sl-border-radius-large);
6+
cursor: pointer;
7+
font-size: 1rem;
8+
width: fit-content;
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
window.hyperdiv.registerPlugin("counter", (ctx) => {
2+
let count = 0;
3+
const button = document.createElement("button");
4+
button.innerText = "Increment";
5+
const countDiv = document.createElement("div");
6+
7+
const updateCount = (newCount) => {
8+
count = newCount;
9+
countDiv.innerText = count;
10+
};
11+
12+
// On click, increment the count and send the updated
13+
// prop value to Python:
14+
button.addEventListener("click", () => {
15+
updateCount(count + 1);
16+
ctx.updateProp("count", count);
17+
});
18+
19+
// Handle incoming prop updates from Python. We ignore
20+
// `propValue` because there is only one prop, "count".
21+
ctx.onPropUpdate((propName, propValue) => {
22+
updateCount(propValue);
23+
});
24+
25+
// Add the dom elements to the shadow root.
26+
ctx.domElement.appendChild(button);
27+
ctx.domElement.appendChild(countDiv);
28+
29+
// Initialize the plugin with the initial count.
30+
updateCount(ctx.initialProps.count);
31+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
import hyperdiv as hd
3+
4+
5+
class counter(hd.Plugin):
6+
_name = "counter"
7+
_assets_root = os.path.join(os.path.dirname(__file__), "assets")
8+
_assets = ["*"]
9+
10+
count = hd.Prop(hd.Int, 0)
11+
12+
13+
class boxy_counter(counter, hd.Boxy):
14+
_classes = ["box"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .leaflet import leaflet
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
window.hyperdiv.registerPlugin("leaflet", ctx => {
2+
const L = window.L;
3+
4+
const props = {...ctx.initialProps};
5+
6+
const mapContainer = document.createElement("div");
7+
mapContainer.style.width = "100%";
8+
mapContainer.style.height = "100%";
9+
ctx.domElement.appendChild(mapContainer);
10+
11+
const map = L.map(mapContainer);
12+
map.setView([props.lat, props.lng], props.zoom);
13+
14+
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
15+
maxZoom: 19,
16+
attribution:
17+
'&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
18+
}).addTo(map);
19+
20+
map.on("zoomend", () => {
21+
props.zoom = map.getZoom();
22+
ctx.updateProp("zoom", props.zoom);
23+
});
24+
25+
map.on("moveend", () => {
26+
const center = map.getCenter();
27+
props.lat = center.lat;
28+
props.lng = center.lng;
29+
ctx.updateProp("lat", props.lat);
30+
ctx.updateProp("lng", props.lng);
31+
});
32+
33+
ctx.onPropUpdate((propName, propValue) => {
34+
props[propName] = propValue;
35+
map.setView([props.lat, props.lng], props.zoom);
36+
});
37+
38+
const resizeObserver = new ResizeObserver(() => map.invalidateSize());
39+
resizeObserver.observe(mapContainer);
40+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
import hyperdiv as hd
3+
4+
5+
class leaflet(hd.Plugin):
6+
_assets_root = os.path.join(os.path.dirname(__file__), "assets")
7+
_assets = [
8+
"https://unpkg.com/leaflet@1.9.4/dist/leaflet.css",
9+
"https://unpkg.com/leaflet@1.9.4/dist/leaflet.js",
10+
"leaflet-plugin.js",
11+
]
12+
13+
zoom = hd.Prop(hd.Int, 13)
14+
lat = hd.Prop(hd.Float, 51.505)
15+
lng = hd.Prop(hd.Float, -0.09)
16+
17+
def __init__(self, height=20, **kwargs):
18+
super().__init__(height=height, **kwargs)

hyperdiv_docs/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import hyperdiv as hd
33
from .router import router
44
from .menu import menu
5-
from .app_template_demo import main as demo_main
5+
from .demos.app_template_demo import main as demo_main
66

77

88
def render_title(slot=None):

hyperdiv_docs/menu.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .pages.guide.static_assets import static_assets
1717
from .pages.guide.deploying import deploying
1818
from .pages.guide.matplotlib_charts import matplotlib_charts
19+
from .pages.guide.plugins import plugins
1920

2021
from .pages.reference.components import components
2122
from .pages.reference.env_variables import env_variables
@@ -44,6 +45,7 @@
4445
"Using The App Template": {"href": using_the_app_template.path},
4546
"Matplotlib Charts": {"href": matplotlib_charts.path},
4647
"Static Assets": {"href": static_assets.path},
48+
"Building Custom Components": {"href": plugins.path},
4749
"Deploying Hyperdiv": {"href": deploying.path},
4850
},
4951
"Reference": {

0 commit comments

Comments
 (0)