Can someone explain to me how do I update a property of an element from a function called from another element, like in the example below?
In this example, I created a flet app which shows 4 elements. The two elements at the top are images and the ones at the bottom are buttons. Now, I’d like to click the buttons and change the images. How do I do that? I already tried to change it through the controls, but I was not able to do it.
JavaScript
x
44
44
1
import numpy as np
2
import base64
3
import flet
4
from flet import Image, Page, Row, TextButton
5
from io import BytesIO
6
from PIL import Image as image
7
8
arr = np.zeros((400, 400, 3), dtype=np.uint8)
9
pil_img = image.fromarray(arr)
10
buff = BytesIO()
11
pil_img.save(buff, format="JPEG")
12
image_string = base64.b64encode(buff.getvalue()).decode("utf-8")
13
14
def main(page: Page):
15
page.title = "My Page"
16
page.theme_mode = "light"
17
page.padding = 50
18
19
page.update()
20
21
def update_load_button(value):
22
img = np.random.randint(0, 255, (400, 400, 3), np.uint8)
23
pil_img = image.fromarray(img)
24
buff = BytesIO()
25
pil_img.save(buff, format="JPEG")
26
new_image_string = base64.b64encode(buff.getvalue()).decode("utf-8")
27
## CHANGE IMAGE HERE ##
28
page.update()
29
30
def update_predict_button(v):
31
pass
32
33
page.add(
34
Row(controls=[
35
Image(src_base64=image_string),
36
Image(src_base64=image_string)
37
], alignment='center'),
38
Row(controls=[
39
TextButton("Load_image", on_click=update_load_button),
40
TextButton("Predict", on_click=update_predict_button)
41
], alignment='center')
42
)
43
page.update()
44
Advertisement
Answer
You need to update the value only of the Image.
Click on Load image to change the 2. image’s color:
JavaScript
1
50
50
1
import numpy as np
2
import base64
3
import flet
4
from flet import Image, Page, Row, TextButton
5
from io import BytesIO
6
from PIL import Image as image
7
8
arr = np.zeros((400, 400, 3), dtype=np.uint8)
9
pil_img = image.fromarray(arr)
10
buff = BytesIO()
11
pil_img.save(buff, format="JPEG")
12
13
def main(page: Page):
14
image_string = base64.b64encode(buff.getvalue()).decode("utf-8")
15
page.title = "My Page"
16
page.theme_mode = "light"
17
page.padding = 50
18
img_1 = Image(src_base64=image_string)
19
img_2 = Image(src_base64=image_string)
20
21
# page.update() # not needed!
22
23
def update_load_button(value):
24
img = np.random.randint(0, 255, (400, 400, 3), np.uint8)
25
pil_img = image.fromarray(img)
26
buff = BytesIO()
27
pil_img.save(buff, format="JPEG")
28
29
new_image_string = base64.b64encode(buff.getvalue()).decode("utf-8")
30
img_2.src_base64 = new_image_string # img_2's value updated
31
## CHANGE IMAGE HERE ##
32
img_2.update() # only img_2 widget will be updated
33
34
def update_predict_button(v):
35
pass
36
37
page.add(
38
Row(controls=[
39
img_1,
40
img_2
41
], alignment='center'),
42
Row(controls=[
43
TextButton("Load_image", on_click=update_load_button),
44
TextButton("Predict", on_click=update_predict_button)
45
], alignment='center')
46
)
47
# page.update() # not needed, every widget can be stateless (only Image not)
48
49
flet.app(target=main)
50