Skip to content

Commit e216973

Browse files
committed
Refactor calculator examples and update integration tests
Added __init__.py to chip examples and updated main entry points to use '__name__ == "__main__"' guard. Improved calculator tutorial documentation for clarity. Enhanced integration tests for calculator examples, added golden screenshots for macOS, and expanded test coverage to include calc2 and calc3.
1 parent 27145b0 commit e216973

File tree

9 files changed

+56
-59
lines changed

9 files changed

+56
-59
lines changed

sdk/python/examples/controls/chip/__init__.py

Whitespace-only changes.

sdk/python/examples/controls/chip/assist_chips.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ async def handle_chip2_click(e: ft.Event[ft.Chip]):
3434
)
3535

3636

37-
ft.run(main)
37+
if __name__ == "__main__":
38+
ft.run(main)

sdk/python/examples/controls/chip/filter_chips.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ def handle_amenity_selection(e: ft.Event[ft.Chip]):
2929
)
3030

3131

32-
ft.run(main)
32+
if __name__ == "__main__":
33+
ft.run(main)

sdk/python/examples/tutorials/calculator/calc3.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,5 @@ class ExtraActionButton(CalcButton):
8080
)
8181

8282

83-
ft.run(main)
83+
if __name__ == "__main__":
84+
ft.run(main)

sdk/python/packages/flet/docs/tutorials/calculator.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,14 @@ Below is `on_click` event handler that will reset the Text value when "AC" butto
262262

263263
```python
264264
def button_clicked(self, e):
265-
if e.control.data == "AC":
265+
data = e.control.content
266+
print(f"Button clicked with data = {data}")
267+
if data == "AC":
266268
self.result.value = "0"
267269
```
268270

269271
With similar approach, `button_click` method will handle different calculator actions
270-
depending on `data` property for each button.
272+
depending on `content` property for each button.
271273
Copy the entire code for this step from
272274
[here](https://github.com/flet-dev/flet/blob/main/sdk/python/examples/tutorials/calc/calc.py).
273275

53.8 KB
Loading
39.4 KB
Loading
30.2 KB
Loading
Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
import pytest
22

3+
import flet as ft
34
import flet.testing as ftt
4-
5-
# from examples.controls.checkbox import basic, handling_events, styled
6-
from examples.tutorials.calculator import calc1
7-
8-
# @pytest.mark.asyncio(loop_scope="function")
9-
# async def test_image_for_docs(flet_app_function: ftt.FletTestApp, request):
10-
# flet_app_function.page.theme_mode = ft.ThemeMode.LIGHT
11-
# await flet_app_function.assert_control_screenshot(
12-
# request.node.name,
13-
# ft.Column(
14-
# intrinsic_width=True,
15-
# controls=[
16-
# ft.Checkbox(),
17-
# ft.Checkbox(label="Checked", value=True),
18-
# ft.Checkbox(label="Disabled", disabled=True),
19-
# ],
20-
# ),
21-
# )
5+
from examples.tutorials.calculator import calc1, calc2, calc3
226

237

248
@pytest.mark.parametrize(
@@ -27,47 +11,55 @@
2711
indirect=True,
2812
)
2913
@pytest.mark.asyncio(loop_scope="function")
30-
async def test_basic(flet_app_function: ftt.FletTestApp):
31-
# button = await flet_app_function.tester.find_by_text("Submit")
32-
# await flet_app_function.tester.tap(button)
14+
async def test_calc1(flet_app_function: ftt.FletTestApp):
15+
flet_app_function.page.theme_mode = ft.ThemeMode.LIGHT
16+
flet_app_function.page.enable_screenshots = True
17+
flet_app_function.resize_page(350, 850)
18+
flet_app_function.page.update()
3319
await flet_app_function.tester.pump_and_settle()
3420
flet_app_function.assert_screenshot(
3521
"calc1",
36-
await flet_app_function.take_page_controls_screenshot(),
22+
await flet_app_function.page.take_screenshot(
23+
pixel_ratio=flet_app_function.screenshots_pixel_ratio
24+
),
3725
)
3826

3927

40-
# @pytest.mark.parametrize(
41-
# "flet_app_function",
42-
# [{"flet_app_main": handling_events.main}],
43-
# indirect=True,
44-
# )
45-
# @pytest.mark.asyncio(loop_scope="function")
46-
# async def test_handling_events(flet_app_function: ftt.FletTestApp):
47-
# checkbox = await flet_app_function.tester.find_by_text(
48-
# "Checkbox with 'change' event"
49-
# )
50-
# await flet_app_function.tester.tap(checkbox)
51-
# await flet_app_function.tester.pump_and_settle()
52-
# await flet_app_function.tester.tap(checkbox)
53-
# await flet_app_function.tester.pump_and_settle()
54-
# await flet_app_function.tester.tap(checkbox)
55-
# await flet_app_function.tester.pump_and_settle()
56-
# scr = await flet_app_function.wrap_page_controls_in_screenshot()
57-
# flet_app_function.assert_screenshot(
58-
# "handling_events",
59-
# await scr.capture(pixel_ratio=flet_app_function.screenshots_pixel_ratio),
60-
# )
28+
@pytest.mark.parametrize(
29+
"flet_app_function",
30+
[{"flet_app_main": calc2.main}],
31+
indirect=True,
32+
)
33+
@pytest.mark.asyncio(loop_scope="function")
34+
async def test_calc2(flet_app_function: ftt.FletTestApp):
35+
flet_app_function.page.theme_mode = ft.ThemeMode.LIGHT
36+
flet_app_function.page.enable_screenshots = True
37+
flet_app_function.resize_page(350, 350)
38+
flet_app_function.page.update()
39+
await flet_app_function.tester.pump_and_settle()
40+
flet_app_function.assert_screenshot(
41+
"calc2",
42+
await flet_app_function.page.take_screenshot(
43+
pixel_ratio=flet_app_function.screenshots_pixel_ratio
44+
),
45+
)
6146

6247

63-
# @pytest.mark.parametrize(
64-
# "flet_app_function",
65-
# [{"flet_app_main": styled.main}],
66-
# indirect=True,
67-
# )
68-
# @pytest.mark.asyncio(loop_scope="function")
69-
# async def test_styled(flet_app_function: ftt.FletTestApp):
70-
# flet_app_function.assert_screenshot(
71-
# "styled_checkboxes",
72-
# await flet_app_function.take_page_controls_screenshot(),
73-
# )
48+
@pytest.mark.parametrize(
49+
"flet_app_function",
50+
[{"flet_app_main": calc3.main}],
51+
indirect=True,
52+
)
53+
@pytest.mark.asyncio(loop_scope="function")
54+
async def test_calc3(flet_app_function: ftt.FletTestApp):
55+
flet_app_function.page.theme_mode = ft.ThemeMode.LIGHT
56+
flet_app_function.page.enable_screenshots = True
57+
flet_app_function.resize_page(400, 350)
58+
flet_app_function.page.update()
59+
await flet_app_function.tester.pump_and_settle()
60+
flet_app_function.assert_screenshot(
61+
"calc3",
62+
await flet_app_function.page.take_screenshot(
63+
pixel_ratio=flet_app_function.screenshots_pixel_ratio
64+
),
65+
)

0 commit comments

Comments
 (0)