Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default float fields not updated when model instance argument changed #70

Open
3 of 4 tasks
benoitsdm opened this issue Jan 28, 2025 · 2 comments
Open
3 of 4 tasks
Labels
status:needs-triage Has not been triaged yet type:bug Something isn't working

Comments

@benoitsdm
Copy link

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

Thanks for the great library, really powerful !

I have noticed a bug that you can reproduce with the minimalist example.

Reproducible Code Example

import streamlit as st
import streamlit_pydantic as sp
from pydantic import BaseModel

class Model(BaseModel):
    i: int
    s: str

a= Model(i=1, s="1")
b= Model(i=2, s="2")

init = st.selectbox(label="instance", options=[a, b])

data = sp.pydantic_form(key="f", model=init)

if data:
    st.json(data)

Steps To Reproduce

When you change the init model from the select box, the string default is successfully updated but not the int.

Expected Behavior

No response

Current Behavior

No response

Is this a regression?

  • Yes, this used to work in a previous version.

Debug info

Additional Information

Not sure how the session state this handled under the hood, do you have any quick work around ? Or if you have any idea where the bug can come from, please points me the function I can have a look.

@benoitsdm benoitsdm added status:needs-triage Has not been triaged yet type:bug Something isn't working labels Jan 28, 2025
@benoitsdm
Copy link
Author

benoitsdm commented Jan 28, 2025

    if key not in ["FormSubmitter:f-Submit", "run_id"]:
        del st.session_state[key]

seems to fix the bug but i am not sure if it's dangerous and can have side effect ?

I guess it's an issue with _render_single_number_input but still trying to understand how it works

@benoitsdm
Copy link
Author

After digging a bit, it is due to these lines:

if self._session_state.get(streamlit_kwargs["key"]) is None:
        if property.get("init_value") is not None:
            streamlit_kwargs["value"] = number_transform(property["init_value"])
        elif property.get("default") is not None:
            streamlit_kwargs["value"] = number_transform(property["default"])  # type: ignore
        else:
            if "min_value" in streamlit_kwargs:
                streamlit_kwargs["value"] = streamlit_kwargs["min_value"]
            elif number_transform == int:
                streamlit_kwargs["value"] = 0
            else:
                # Set default value to step
                streamlit_kwargs["value"] = number_transform(
                    streamlit_kwargs["step"]
                )
else:
        streamlit_kwargs["value"] = number_transform(
        self._session_state[streamlit_kwargs["key"]]
        )

Not sure if it's made on purpose or not, if there is already a value in session_state, we use it instead of using property["init_value"]. Nothing similar in _render_single_string_input. If you confirm it's a bug i can submit a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:needs-triage Has not been triaged yet type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant