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

What is the difference between default and init_value? #26

Open
y805939188 opened this issue Apr 12, 2023 · 3 comments
Open

What is the difference between default and init_value? #26

y805939188 opened this issue Apr 12, 2023 · 3 comments

Comments

@y805939188
Copy link

y805939188 commented Apr 12, 2023

Describe the issue:
Hi, I noticed that there are two fields, default and init_value, that can be used in Field. May I ask what the difference is between these two fields? It seems that their effects are the same, but is the priority of init_value higher than default?
Thank you.

Technical details:

  • Host Machine OS (Windows/Linux/Mac):
@HIL340
Copy link
Contributor

HIL340 commented Apr 13, 2023

default is a standard attribute of pydantic's Field and is usually defined as part of the model, such as "Jane Doe" in the example below:

class User(BaseModel):
    id: int
    name = 'Jane Doe'

init_value is a streamlit-pydantic specific attribute that is used internally to store the value of a field when sp.pydantic_form()/sp.pydantic_input() is initialised from an instance of a class.

In hindsight, init_value should have a more descriptive name (instance_value would be better!) and perhaps it should start with an underscore to make it more obvious that it for internal use.

In terms of which value should get passed through to the streamlit widgets when both attributes exist, init_value is considered to be higher priority than default (in the same way that instance values would override the schema defaults when using pydantic in a regular python script)

@y805939188
Copy link
Author

default is a standard attribute of pydantic's Field and is usually defined as part of the model, such as "Jane Doe" in the example below:

class User(BaseModel):
    id: int
    name = 'Jane Doe'

init_value is a streamlit-pydantic specific attribute that is used internally to store the value of a field when sp.pydantic_form()/sp.pydantic_input() is initialised from an instance of a class.

In hindsight, init_value should have a more descriptive name (instance_value would be better!) and perhaps it should start with an underscore to make it more obvious that it for internal use.

In terms of which value should get passed through to the streamlit widgets when both attributes exist, init_value is considered to be higher priority than default (in the same way that instance values would override the schema defaults when using pydantic in a regular python script)

I noticed that if the default value is passed to Dict using default, it will not be rendered. It will work when passing the default value using init_value:

from typing import Dict

import streamlit as st
from pydantic import BaseModel, Field

import streamlit_pydantic as sp

class ShowcaseModel(BaseModel):
    string_dict: Dict[str, str] = Field(
        # init_value={"foo": "1.0"},
        default={"foo": "bar"},
    )
    float_dict: Dict[str, float] = Field(
        init_value={"foo": 1.0},
    )

session_data = sp.pydantic_input(
    key="my_input", model=ShowcaseModel, group_optional_fields="sidebar"
)

with st.expander("Current Input State", expanded=False):
    st.json(session_data)

image

Is this as expected?
Thank you.

@HIL340
Copy link
Contributor

HIL340 commented Apr 17, 2023

In the case of dicts, lists and objects I guess its a bug/missing feature. I've added a PR to address it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants