flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.07k stars 427 forks source link

Unable to refresh Image.src #1327

Closed csgoh closed 1 year ago

csgoh commented 1 year ago

My application requires to generate images on the fly and the output is display on a Image Control. However, everytime my image is generated, calling Image.src = does not refresh the image. The Image control is still showing the first image.

Code example to reproduce the issue:

import flet as ft
from flet import Theme, Icon
import logging
import os
from PIL import Image
from processpiper.text2diagram import render

logging.getLogger("flet_core").setLevel(logging.ERROR)

def main(page: ft.page):
    page.theme_mode = ft.ThemeMode.LIGHT
    page.title = "Process Piper"
    page.scroll = ft.ScrollMode.ADAPTIVE
    page.window_height = 800
    page.window_width = 1200
    page.update()

    def generate_diagram(e):
        diagram_text = user_input.value

        try:
            output_image_file = "diagram.png"
            generated_code, generated_image = render(diagram_text, output_image_file)

            output_image.src = output_image_file
            output_image.update()
            page.update()

        except Exception as e:
            log_text.value = log_text.value + "\n" + str(e)
            page.update()

    test_text = """
    title: Make pizza process
    lane: Pizza Shop
        (start) as start
        [Put the pizza in the oven] as put_pizza_in_oven
        [Check to see if pizza is done] as check_pizza_done
        <Done baking?> as done_baking
        [Take the pizza out of the oven] as take_pizza_out_of_oven
        (end) as end

    start->put_pizza_in_oven->check_pizza_done->done_baking
    done_baking-"Yes"->take_pizza_out_of_oven->end
    done_baking-"No"->put_pizza_in_oven
    """

    user_input = ft.TextField(
        label="Enter text here",
        value=test_text,
        multiline=True,
        text_size=11,
        icon=ft.icons.TEXT_FIELDS_SHARP,
    )
    generate_button = ft.ElevatedButton(
        "Generate", on_click=generate_diagram, icon=ft.icons.DIRECTIONS_RUN
    )
    output_image = ft.Image("Output Image")

    log_text = ft.TextField(
        label="Log Messages",
        value="",
        multiline=True,
        read_only=True,
        text_size=11,
        color="black",
        bgcolor="white",
    )

    page.add(
        user_input,
        generate_button,
        output_image,
        log_text,
    )

ft.app(target=main)

The render() method regenerates the image based on the diagram_text. However, the output_image.src has no effect when called subsequent times.

To reproduce the bug,

I expect the output_image get updated and showing the latest image.

Additional information you deem important (e.g. issue happens only occasionally):

Flet version (pip show flet):

Name: flet
Version: 0.5.2
Summary: Flet for Python - easily build interactive multi-platform apps in Python
Home-page:
Author: Appveyor Systems Inc.
Author-email: hello@flet.dev
License: Apache-2.0
Location: c:\lab\processpiper-gui\venv\lib\site-packages
Requires: flet-core, httpx, oauthlib, packaging, watchdog, websocket-client, websockets
Required-by:

Operating system: Windows

Additional environment details:

ndonkoHenri commented 1 year ago

That seems strange. Can you try output_img.update()?

If it still doesn't work, then build up some small working code sample with the issue, so we/I can give it a better look.

csgoh commented 1 year ago

Same result even if I use output_img.update()

ndonkoHenri commented 1 year ago

If it still doesn't work, then build up some small working code sample with the issue, so we/I can give it a better look.

csgoh commented 1 year ago

@ndonkoHenri, I have provided a complete code sample that reproduce the issue. See the original post above. Thanks!

FeodorFitsner commented 1 year ago

@csgoh by changing image file contents you are not changing Flet control properties. output_image.src and other image properties stay the same as a result page.update() does nothing.

Solution: every time you generate new image change its filename too.