How to update multiple input fields?

Hi all,

I have 3 input fields and a data object:

{
“method”: “retrieve”,
“title”: “China’s Factory Activity Sparks Hope Slump Is Bottoming Out”,
“date”: “2023-08-31”,
“source”: “Bloomberg”,
“status”: “Success”
}

I want to change the value of the 3 input fields with title, date and source respectively.

How do I do it?

I have done it this way and it can’t work:

response = {
 "method": "retrieve",
 "title": "China’s Factory Activity Sparks Hope Slump Is Bottoming Out",
 "date": "2023-08-31",
 "source": "Bloomberg",
 "status": "Success"
}

title = st.text_input("Title:", key="titleInput")
date = st.text_input("Date:", key="dateInput")
source = st.text_input("Source:", key="sourceInput")


titleData = response["title"]
dateData = response["date"]
sourceData = response["source"]

st.session_state['titleInput'] = 'titleData' 
st.session_state['dateInput'] = 'dateData' 
st.session_state['sourceInput'] = 'sourceData' 

Best Regards,
Configentia

Use the value attribute of the text_input for that.

import streamlit as st

response = {
    "method": "retrieve",
    "title": "China’s Factory Activity Sparks Hope Slump Is Bottoming Out",
    "date": "2023-08-31",
    "source": "Bloomberg",
    "status": "Success",
}

title = st.text_input("Title:", key="titleInput", value=response.get("title"))
date = st.text_input("Date:", key="dateInput", value=response.get("date"))
source = st.text_input("Source:", key="sourceInput", value=response.get("source"))

Thanks @CarlosSerrano, I have already tried the value attribute, it only works on page load. That is not what I want.

What I want for example, is as follows:

  1. Present a page with 3 empty input fields, URL, Title, Text.

  2. If the user provides the url and presses the “Fetch” button, my program fetches the document and I want to update the title and the text. This is what I cannot do now.

  3. Let the user summarize the title and the text if he wants.

  4. If the user, press the “Save” button, I save whatever is there in the url, title and text inputs.

title = st.text_input(“Title:”, key=“titleInput”, value=“Page title will come here!”)

page = fetchPage(url)

title = page[“title”]

How can I do instruct Streamlit to update the title field with the new of the variable title?

Best Regards,
Configentia

If you have session keys for those values, you can assign them in a function and use it on a callback. That will allow you to add the behavior you want using the value attributes and session state.

Hi @Configentia , If I understand correctly, you wanted to store the given input and update in the response variable and also update in the session.
For that you can try the below code.

response = {
 "method": "retrieve",
 "title": "China’s Factory Activity Sparks Hope Slump Is Bottoming Out",
 "date": "2023-08-31",
 "source": "Bloomberg",
 "status": "Success"
}

title = st.text_input("Title:", key="titleInput")
date = st.text_input("Date:", key="dateInput")
source = st.text_input("Source:", key="sourceInput")

response["title"] = title
response["date"] = date
response["source"] = source

st.session_state['titleInput'] = title
st.session_state['dateInput'] = date
st.session_state['sourceInput'] = source

If I’m wrong at getting you question please help to understand it in better way.:slight_smile:

Vicky Raghuwanshi

Thanks everyone for helping, I manged to fix it by placing the st.text_input below the buttons and other commands that changed the input key value.

Here is how I did it:


retrieve_button = st.button("Retrieve")

if retrieve_button:
    st.session_state.first = "Mint"
    st.session_state.second = "Strawberry"
    st.session_state.third = "Vanila"
    st.session_state.fourth = "Chocolate"

st.text_input(label='Textbox 1', key='first')
st.text_input(label='Textbox 2', key='second')
st.text_input(label='Textbox 3', key='third')
st.text_input(label='Textbox 4', key='fourth')

Configentia

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.