Skip to content

Commit c186714

Browse files
committed
Return the size of the block from render().
1 parent 5cacd68 commit c186714

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

docs/demo/python.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ The
6868
module allows creating SVG images using simple drawing primitives. Rendering an
6969
image creates an output block displaying the image.
7070

71-
`render(image, name='')`
71+
`render(image, name='') -> Future`
7272

7373
- `image`: The image to be rendered.
7474
- `name`: The name of the output block. If a block with the same name already
7575
exists, it is replaced. Otherwise, a new block is added, keeping blocks
7676
ordered by name.
77+
- The returned future resolves to a tuple `(width, height)` that specifies the
78+
size of the rendered image.
7779

7880
```{exec} python
7981
:name: python-graphics
@@ -117,22 +119,19 @@ import asyncio
117119
def saw(value, amplitude):
118120
return abs((value + amplitude) % (2 * amplitude) - amplitude)
119121
122+
vx, vy, va = 101, 79, 181
123+
120124
img = svg.Image(400, 100, stroke='red', style='width: 100%; height: 100%')
121125
g = img.group()
122126
paint_heart(g)
123127
124-
margin = 20
125-
vx, vy, va = 101, 79, 181
126-
127128
loop = asyncio.get_running_loop()
128129
start = loop.time()
129130
while True:
130131
t = loop.time() - start
131-
x = margin + saw(t * vx, img.width - 2 * margin)
132-
y = margin + saw(t * vy, img.height - 2 * margin)
133-
a = (t * va) % 360.0
134-
g.transform = svg.translate(x, y).rotate(a).scale(0.5)
135-
render(img)
132+
x, y, a = saw(t * vx, img.width), saw(t * vy, img.height), (t * va) % 360.0
133+
g.transform = svg.translate(x, y).rotate(a)
134+
img.width, img.height = await render(img)
136135
await asyncio.sleep(1 / 60)
137136
```
138137

tdoc/common/python/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ def write(self, data, /):
5454

5555
@public
5656
def render(html, name=''):
57+
"""Render some HTML in an output block."""
5758
if not isinstance(html, str): html = ''.join(html)
58-
js_render(run_id(), html, name)
59+
return js_render(run_id(), html, name).then(lambda res: tuple(res))
5960

6061

6162
@public

tdoc/common/python/svg.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import html
55

66
# TODO: Add <style> element
7-
# TODO: Make the output width accessible; maybe return it from render()
87

98
def esc(v, quote=True):
109
if not isinstance(v, str): v = str(v)

tdoc/common/static/tdoc-python.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ class PythonExecutor extends Executor {
198198
}
199199

200200
onRender(html, name) {
201-
this.render(name, html);
201+
const el = this.render(name, html);
202+
return [el.scrollWidth, el.scrollHeight];
202203
}
203204

204205
render(name, html) {
@@ -212,11 +213,11 @@ class PythonExecutor extends Executor {
212213
for (const el of this.output.children) {
213214
if (el.tdocName > name) {
214215
el.before(new_el);
215-
return;
216+
return new_el;
216217
}
217218
if (el.tdocName === name) {
218219
el.replaceWith(new_el);
219-
return;
220+
return new_el;
220221
}
221222
}
222223
this.output.appendChild(new_el);

0 commit comments

Comments
 (0)