Repository navigation
Expand file tree
/
Copy pathhello.py
More file actions
49 lines (41 loc) · 2.07 KB
/
Copy pathhello.py
File metadata and controls
49 lines (41 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""renderfi from Python, through the binding generated from the spec.
cargo build --release -p rf-capi --features cpu-renderer
python3 examples/python/hello.py
"""
import os
import sys
root = os.path.join(os.path.dirname(__file__), "..", "..")
sys.path.insert(0, os.path.join(root, "bindings", "python"))
import renderfi as rf # noqa: E402
# region: readme
lib_name = {"darwin": "librenderfi_c.dylib", "win32": "renderfi_c.dll"}.get(sys.platform, "librenderfi_c.so")
lib = rf.load(os.path.join(root, "target", "release", lib_name))
# a Context owns the context; its parts are objects (ui.styles, ui.data, ui.engine.plugins)
ui = rf.Context(lib)
markup = """
<box style="flex-direction: column; padding: 24; gap: 10; background-color: #1d1f23; color: #d8dbe0">
<style> .title { font-size: 26 } .py { color: #ffd43b } </style>
<text class="title">Hello from <b class="py">Python</b></text>
<text>Every type and function here was generated from spec/renderfi.rfspec.</text>
<repeat each={langs} style="flex-direction: row; gap: 8">
<text text={item.name} style="padding: 4 10; border-radius: 4; background-color: #2f3238"/>
</repeat>
</box>
"""
ui.set_root(ui.load_markup(markup)) # a markup error raises rf.RenderFiError
# a list signal: rows with a key and fields (the markup shows them in a repeat)
names = ["C", "Rust", "Python", "Swift", "C#"]
ui.data.set_list("langs", [(str(i), {"name": name}) for i, name in enumerate(names)])
out = os.path.join(root, "out", "hello_python.png")
os.makedirs(os.path.join(root, "out"), exist_ok=True)
ui.render_to_image(rf.RfRenderToImageInfo(path=out, width=640, height=200)) # PNG: from the extension
print("wrote", out)
# endregion
# no path: the bytes come back (valid until the next render_to_image)
try:
data = ui.render_to_image(rf.RfRenderToImageInfo(format=rf.RF_IMAGE_FORMAT_BMP, width=64, height=32))
assert data[:2] == b"BM", "a BMP file starts with BM"
print("a BMP of", len(data), "bytes in memory")
except rf.RenderFiError as e:
assert e.code == rf.RF_ERROR_FEATURE_NOT_PRESENT, e.code # a build without BMP
ui.close()