"The widget with key "some_input" was created with a default value but also had its value set via the Session State API" warning in app

Hi guys

First time posting here.

Iā€™m building an application where I want to be able to save the input values in a json file, so I later can reload the values. I managed to get it to work with ā€œsession_stateā€ and given the input values ā€œkey namesā€

See code below:

import streamlit as st

import json

def set_values_from_json():

    file = st.session_state["file_uploader"]

    if file:

        if file.id != st.session_state["file_id"]:

            json_dir = json.loads(file.read())

            st.session_state["some_input"] = json_dir["some_value"]

            st.session_state["some_other_input"] = json_dir["some_other_value"]

            st.session_state["file_id"] = file.id

if "file_id" not in st.session_state:

    st.session_state["file_id"] = -1

st.number_input(

    "some_input",

    key="some_input",

)

st.number_input(

    "some_other_input",

    key="some_other_input",

)

file = st.file_uploader(

    "input json!", key="file_uploader", on_change=set_values_from_json

)

However, I keep getting a ā€œThe widget with key ā€œsome_inputā€ was created with a default value but also had its value set via the Session State API.ā€ warning the first time I load my json. Is there a way around this? Ether suppresses the warning or an alternative approach that doesnā€™t resolve in a warning?

Bonus question: The file_uploader ā€œon_changeā€ seems to trigger every time when it holds a file, not only when the file is changed. Is this a bug or intentional?

I also have this question, streamlit version 1.12.0, python 3.10.2 - on first upload it triggers a warning, but then never again unless streamlit itself is stopped and started. Any ideas - just suppressing the warning would be good enough for me :slight_smile:

I donā€™t see the warning using streamlit v1.12.2. The numbers are correctly updated when I load the .json file but no warnings.

I still have this issue in 1.13.0 , python 3.10.7 :man_shrugging:

I tried running your script with this json file

{
  "some_value": 0.5,
  "some_other_value": 0.6
}

But, I was unable to reproduce the warning appearing with streamlit 1.13.0. Are you just immediately uploading the file?