Trying to show a shap plot with solara
This is the code for the image:
df_encoded is a global variable that has been updated in another function
#imports...
shap_image = solara.reactive(None)
def show_shap():
# Use the global reactive df_encoded_reactive variable
df_encoded = df_encoded_reactive.value
if df_encoded is None:
return # Prevent error if no prediction has been made yet
# SHAP values for user inputs
shap_values = explainer.shap_values(df_encoded)
shap.force_plot(explainer.expected_value, shap_values[0], df_encoded.iloc[0], matplotlib=True)
# Save SHAP plot to an image
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
shap_image.value = Image.open(buf)
then I wanna use a button that when clicked it displays it:
def Page():
solara.Button(label="More info", on_click=show_shap)
if shap_image.value:
solara.Markdown(shap_image.value)
My Solara interface displays the More Infor button, but when pressed, nothing happens although the function is working. I think the issue is with how I am trying to show the image.
Thank you
2 Replies
Hi solara.Markdown does not take an image as argument, but solara.Image does, could you try that?
Also, it would be great if you could share snippets using py.cafe 🙂
thank you it works now 🙂