partial.update_content() not updating as expected
Description I expected partial.update_content() to immediately change a partial, which would allow for a dynamic element. In a callback there is immediate reaction to changes in the bound variables, but partial.update_content() seems to execute after the callback has finished. This leads to a raised exception in the example.
How to reproduce
import taipy as tp
def make_partial(state):
state.my_partial.update_content(state, """<|empty|>""")
if state.select == "with Y":
state.data_for_display = state.data
else:
state.data_for_display = state.data2
content = """<|
"""
for key in state.data_for_display.keys():
content = (
content + key + """ = <|{data_for_display['""" + key + """']}|><br />"""
)
content = (
content
+ """
|>"""
)
state.my_partial.update_content(state, content)
if __name__ == "__main__":
data = {"x": "X", "y": "Y"}
data2 = {"x": "X"}
data_for_display = data
select = 1
gui = tp.Gui()
gui.add_page(
"page",
page="""
<|selector|value={select}|lov={['with Y','Exception without Y']}|adapter={lambda i: str(i)}|on_change=make_partial|>
<br />
<|part|partial={my_partial}|>
""",
)
my_partial = gui.add_partial(page="""""")
tp.run(gui)
partial update is asynchronous by design
you could add a bit of test in your expression to avoid the exception
from taipy.gui import Gui
def make_partial(state):
state.my_partial.update_content(state, "<|empty|>")
if state.select == "with Y":
state.data_for_display = state.data
else:
state.data_for_display = state.data2
content = """<|
"""
for key in state.data_for_display.keys():
content = f"{content}{key} = <|{{data_for_display['{key}'] if '{key}' in data_for_display else ''}}|><br />"
content += """
|>"""
state.my_partial.update_content(state, content)
if __name__ == "__main__":
data = {"x": "X", "y": "Y"}
data2 = {"x": "X"}
data_for_display = data
select = None
gui = Gui()
gui.add_page(
"page",
page="""
<|selector|value={select}|lov=with Y;Exception without Y|on_change=make_partial|>
<br />
<|part|partial={my_partial}|>
""",
)
my_partial = gui.add_partial(page="")
gui.run()
Does that answer your question @chrisOnWheels ?
Hi Fred,
this is exactly what I'm doing. Change the underlying data first, then rebuild/adapt the content for the partial, finally call update_partial. But I keep getting these exceptions. My only problem is that these exceptions spam the console.
Best regards, Chris
Fred Lefévère-Laoide @.***> hat am 14.09.2023 17:27 GMT geschrieben:
you could add a bit of test in your expression to avoid the exception
from taipy.gui import Gui
def make_partial(state): state.my_partial.update_content(state, "<|empty|>")
if state.select == "with Y": state.data_for_display = state.data else: state.data_for_display = state.data2
content = """<| """ for key in state.data_for_display.keys(): content = f"{content}{key} = <|{{data_for_display['{key}'] if '{key}' in data_for_display else ''}}|>
" content += """ |>"""state.my_partial.update_content(state, content)
if name == "main": data = {"x": "X", "y": "Y"} data2 = {"x": "X"}
data_for_display = data select = None
gui = Gui() gui.add_page( "page", page=""" <|selector|value={select}|lov=with Y;Exception without Y|on_change=make_partial|>
<|part|partial={my_partial}|> """, )my_partial = gui.add_partial(page="")
gui.run()
— Reply to this email directly, view it on GitHub https://github.com/Avaiga/taipy/issues/562, or unsubscribe https://github.com/notifications/unsubscribe-auth/BB46L2Z3OCFIIQBBCI43BJDX2M46NANCNFSM6AAAAAA3SBI3GA. You are receiving this because you authored the thread.Message ID: @.***>
I don't get any exception with the code I provided. Can you give me some example ?
I will try to isolate it, it's gonna take a while
Fred Lefévère-Laoide @.***> hat am 19.09.2023 07:47 GMT geschrieben:
I don't get any exception with the code provided. Can you give me some example ?
— Reply to this email directly, view it on GitHub https://github.com/Avaiga/taipy/issues/562, or unsubscribe https://github.com/notifications/unsubscribe-auth/BB46L23GNSGFTBABZAXGR43X3FE2HANCNFSM6AAAAAA3SBI3GA. You are receiving this because you were mentioned.Message ID: @.***>
👍
Hi @chrisOnWheels Is this issue still relevant ?
I'll not be working on the project in the near future, so it's up to you. Thanks for the support :-)
Here is a modified version of the code that works properly.
All I did was postpone the update of state.data_for_display until just before the partial gets updated. Please let us know if this works for you, so we can Close the issue with a smile on our faces.
import taipy as tp
def make_partial(state):
state.my_partial.update_content(state, "<|empty|>")
new_data = data if state.select == "with Y" else data2
content = "<|\n"
for key in new_data.keys():
content = content + key + f" = <|{{data_for_display['{key}']}}|><br />\n"
content = content + "|>"
state.data_for_display = new_data
state.my_partial.update_content(state, content)
data = {"x": "X", "y": "Y"}
data2 = {"x": "X"}
if __name__ == "__main__":
data_for_display = data
select = 1
gui = tp.Gui()
gui.add_page(
"page",
page="""
<|selector|value={select}|lov={['with Y','Exception without Y']}|adapter={lambda i: str(i)}|on_change=make_partial|>
<br />
<|part|partial={my_partial}|>
""",
)
my_partial = gui.add_partial(page="<|empty|>")
tp.run(gui)
Closed as answered.