Skip to content

Commit

Permalink
Return the size of the block from render().
Browse files Browse the repository at this point in the history
  • Loading branch information
rblank committed Oct 10, 2024
1 parent 5cacd68 commit c186714
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
17 changes: 8 additions & 9 deletions docs/demo/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ The
module allows creating SVG images using simple drawing primitives. Rendering an
image creates an output block displaying the image.

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

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

```{exec} python
:name: python-graphics
Expand Down Expand Up @@ -117,22 +119,19 @@ import asyncio
def saw(value, amplitude):
return abs((value + amplitude) % (2 * amplitude) - amplitude)
vx, vy, va = 101, 79, 181
img = svg.Image(400, 100, stroke='red', style='width: 100%; height: 100%')
g = img.group()
paint_heart(g)
margin = 20
vx, vy, va = 101, 79, 181
loop = asyncio.get_running_loop()
start = loop.time()
while True:
t = loop.time() - start
x = margin + saw(t * vx, img.width - 2 * margin)
y = margin + saw(t * vy, img.height - 2 * margin)
a = (t * va) % 360.0
g.transform = svg.translate(x, y).rotate(a).scale(0.5)
render(img)
x, y, a = saw(t * vx, img.width), saw(t * vy, img.height), (t * va) % 360.0
g.transform = svg.translate(x, y).rotate(a)
img.width, img.height = await render(img)
await asyncio.sleep(1 / 60)
```

Expand Down
3 changes: 2 additions & 1 deletion tdoc/common/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ def write(self, data, /):

@public
def render(html, name=''):
"""Render some HTML in an output block."""
if not isinstance(html, str): html = ''.join(html)
js_render(run_id(), html, name)
return js_render(run_id(), html, name).then(lambda res: tuple(res))


@public
Expand Down
1 change: 0 additions & 1 deletion tdoc/common/python/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import html

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

def esc(v, quote=True):
if not isinstance(v, str): v = str(v)
Expand Down
7 changes: 4 additions & 3 deletions tdoc/common/static/tdoc-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ class PythonExecutor extends Executor {
}

onRender(html, name) {
this.render(name, html);
const el = this.render(name, html);
return [el.scrollWidth, el.scrollHeight];
}

render(name, html) {
Expand All @@ -212,11 +213,11 @@ class PythonExecutor extends Executor {
for (const el of this.output.children) {
if (el.tdocName > name) {
el.before(new_el);
return;
return new_el;
}
if (el.tdocName === name) {
el.replaceWith(new_el);
return;
return new_el;
}
}
this.output.appendChild(new_el);
Expand Down

0 comments on commit c186714

Please sign in to comment.