S
Solara9mo ago
kd

Unable to trigger re-render after updating reactive state

Hi. I'm trying to render a dataframe using the following code:
mnemonic = solara.reactive("PGDP")
sector = solara.reactive("ALBERTA")
error = solara.reactive(False)
error_message = solara.reactive("")
equation = solara.reactive("")
table1 = solara.reactive(pd.DataFrame())
table2 = solara.reactive(pd.DataFrame())
equations_df_reacton = solara.reactive(pd.DataFrame())
combined_data_df_reacton = solara.reactive(pd.DataFrame())

def on_change(_ = {}):
try:
equation.set(mypackage.get_equation(equations_df_reacton.value, mnemonic.value, sector.value))
df1, df2 = mypackage.create_two_tables(combined_data_df_reacton.value, equations_df_reacton.value, equation.value)
table1.set(df1)
table2.set(df2)
error.set(False)
error_message.set("")
except Exception as e:
error.set(True)
error_message.set(f"Error: {e}")

@solara.component
def Page(combined_data_df, equations_df):
combined_data_df_reacton.set(combined_data_df)
equations_df_reacton.set(equations_df)

on_change()

solara.Title(title = "MDL Data Explorer")
solara.AppBarTitle("MDL Data Explorer")

solara.Row(children = [
solara.InputText(label="Mnemonic", value = mnemonic, on_value=on_change, continuous_update=False),
solara.InputText(label="Sector", value = sector, on_value=on_change, continuous_update=False)
])

if error.value:
solara.Error(f"Unable to get equations for {mnemonic.value},{sector.value}. {error_message.value}")
else:
solara.Text(equation.value)
solara.DataFrame(table1.value, items_per_page=5)
solara.DataFrame(table2.value)
mnemonic = solara.reactive("PGDP")
sector = solara.reactive("ALBERTA")
error = solara.reactive(False)
error_message = solara.reactive("")
equation = solara.reactive("")
table1 = solara.reactive(pd.DataFrame())
table2 = solara.reactive(pd.DataFrame())
equations_df_reacton = solara.reactive(pd.DataFrame())
combined_data_df_reacton = solara.reactive(pd.DataFrame())

def on_change(_ = {}):
try:
equation.set(mypackage.get_equation(equations_df_reacton.value, mnemonic.value, sector.value))
df1, df2 = mypackage.create_two_tables(combined_data_df_reacton.value, equations_df_reacton.value, equation.value)
table1.set(df1)
table2.set(df2)
error.set(False)
error_message.set("")
except Exception as e:
error.set(True)
error_message.set(f"Error: {e}")

@solara.component
def Page(combined_data_df, equations_df):
combined_data_df_reacton.set(combined_data_df)
equations_df_reacton.set(equations_df)

on_change()

solara.Title(title = "MDL Data Explorer")
solara.AppBarTitle("MDL Data Explorer")

solara.Row(children = [
solara.InputText(label="Mnemonic", value = mnemonic, on_value=on_change, continuous_update=False),
solara.InputText(label="Sector", value = sector, on_value=on_change, continuous_update=False)
])

if error.value:
solara.Error(f"Unable to get equations for {mnemonic.value},{sector.value}. {error_message.value}")
else:
solara.Text(equation.value)
solara.DataFrame(table1.value, items_per_page=5)
solara.DataFrame(table2.value)
But only the text field updates, and the dataframe tables don't. I believe it is because the page function isn't being re-rendered.
No description
2 Replies
kd
kdOP9mo ago
InputText accepts a value keyword argument where value: reactive. But DataFrame doesn't. Is that the reason it doesn't rerender? cc @MaartenBreddels Okay I got it to work by splitting the page into a input and results component
MaartenBreddels
MaartenBreddels9mo ago
it should re-render, if you use .value or get() you automatically rerender on a change of a reactive variable. Could you explore a bit more what is going on ? you can always add a print statement to see if the function gets re-executed (a rerender)
Want results from more Discord servers?
Add your server