Skip to content

Commit 9dfbc7e

Browse files
committed
Add iframe rendering functionality and update checkbox height
1 parent dfb62be commit 9dfbc7e

File tree

3 files changed

+71
-25
lines changed

3 files changed

+71
-25
lines changed

sdk/python/packages/flet/docs/controls/checkbox.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ example_images: ../test-images/examples/material/golden/macos/checkbox
1616

1717
{{ image(example_images + "/basic.png", alt="basic", width="50%", caption="After clicking Submit") }}
1818

19-
{{ iframe(route="checkbox/basic", height="520") }}
19+
{{ iframe(route="checkbox/basic", height="400") }}
2020

2121
### Handling events
2222

sdk/python/packages/flet/docs/extras/macros/__init__.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from .cli_to_md import render_flet_cli_as_markdown
55
from .controls_overview import render_controls_overview
6+
from .iframe import render_iframe
67

78

89
def define_env(env):
@@ -139,30 +140,16 @@ def iframe(
139140
allow=None,
140141
loading="lazy",
141142
):
142-
"""
143-
Renders an iframe block.
144-
145-
Args:
146-
src: Full iframe source URL. If omitted, `route` and `base` are combined.
147-
route: Path on the PoC site (e.g., `"checkbox/basic"`).
148-
base: Base URL for the PoC site.
149-
width, height, title, allow, loading: Standard iframe attributes.
150-
"""
151-
if route:
152-
src = base.rstrip("/") + "/" + route.lstrip("/")
153-
if not src:
154-
raise ValueError("Either src or route must be provided")
155-
attrs = [
156-
f'src="{src}"',
157-
f'width="{width}"',
158-
f'height="{height}"',
159-
'style="border:0;"',
160-
f'title="{title or "iframe"}"',
161-
f'loading="{loading}"',
162-
]
163-
if allow:
164-
attrs.append(f'allow="{allow}"')
165-
return f"<iframe {' '.join(attrs)}></iframe>"
143+
return render_iframe(
144+
src=src,
145+
route=route,
146+
base=base,
147+
width=width,
148+
height=height,
149+
title=title,
150+
allow=allow,
151+
loading=loading,
152+
)
166153

167154
@env.macro
168155
def controls_overview():
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from typing import Optional
2+
from urllib.parse import urlparse
3+
4+
5+
def _prettify_token(token: str) -> str:
6+
return " ".join(
7+
word.title() for word in token.replace("-", " ").replace("_", " ").split()
8+
)
9+
10+
11+
def _pretty_from_route(route: str) -> str:
12+
parts = [p for p in route.split("/") if p]
13+
if not parts:
14+
return "iframe"
15+
if len(parts) == 1:
16+
return _prettify_token(parts[0])
17+
# Use the last two segments for nested routes, e.g. charts/line_chart/example_1 -> Line Chart / Example 1
18+
tail = parts[-2:]
19+
return " / ".join(_prettify_token(p) for p in tail)
20+
21+
22+
def render_iframe(
23+
src=None,
24+
*,
25+
route=None,
26+
base: str = "http://127.0.0.1:60222/",
27+
width: str = "100%",
28+
height: str = "480",
29+
title: Optional[str] = None,
30+
allow: Optional[str] = None,
31+
loading: str = "lazy",
32+
) -> str:
33+
"""
34+
Build an iframe HTML snippet.
35+
"""
36+
if route:
37+
src = base.rstrip("/") + "/" + route.lstrip("/")
38+
if not src:
39+
raise ValueError("Either src or route must be provided")
40+
41+
if title is None:
42+
parsed = urlparse(src)
43+
candidate = parsed.path or src
44+
if "." not in candidate:
45+
title = _pretty_from_route(route or candidate)
46+
else:
47+
title = "iframe"
48+
49+
attrs = [
50+
f'src="{src}"',
51+
f'width="{width}"',
52+
f'height="{height}"',
53+
'style="border:0;"',
54+
f'title="{title}"',
55+
f'loading="{loading}"',
56+
]
57+
if allow:
58+
attrs.append(f'allow="{allow}"')
59+
return f"<iframe {' '.join(attrs)}></iframe>"

0 commit comments

Comments
 (0)