Update datafram from text inputs

Hi,

I want to update a Dataframe that is right after my text input and the update button. But I can’t figure out how to keep this placement since the button condition for updating must be placed where I want my button to be placed… any clues ? Thank you in advance

    colums = ["A","B","C"]
    df = pd.DataFrame(columns=colums)
 
    st.write("Add your numbers")
    c1, c2, c3 = st.beta_columns((1,1,1))

    T1 = c1.text_input("text1")
    T2 = c2.text_input("text2")
    T3 = c3.text_input("text3")

    if(st.button('Add')):
        add = []
        T11 = int(T1.title())
        T22 = int(T2.title())
        T33 = int(T3.title())
        add.append([T1,T2,T3])
        df2 = pd.DataFrame(add, columns=colums)
        mytable.add_rows(df2)

     st.success("added")


mytable = st.table(df)

Hi @WBP2020, welcome to the Streamlit community!

I’m afraid I don’t understand your question. You want to have an add button that adds rows to a dataframe, but after that I’m not sure I understand the issue.

Hi, thank you.

My problem is that the table I want to update must be below the button and inputs text !

But in the part

    if(st.button('Add')):
       add = []
       T11 = int(T1.title())
       T22 = int(T2.title())
       T33 = int(T3.title())
       add.append([T1,T2,T3])
       df2 = pd.DataFrame(add, columns=colums)
       mytable.add_rows(df2)

mytable doesn’t exist yet, so can’t be updated.

So how can I create the empty table before the button but display it after it, below the button ?

I think putting

mytable = st.table(df)

right before the if statement should fix the issue.

Hi Alican,

Yes it makes it working but in the layout I need the add button to be placed before the table ! That is the real problem :frowning:

1 Like

Hi @WBP2020,

I once wrote a script that displays elements in expanders once
the user clicks on of the fixed buttons at the top. Check it out:

Is that helpful?

btn = st.button('Add')
mytable = st.table(df)

if(btn):
    add = []
    T11 = int(T1.title())
    T22 = int(T2.title())
    T33 = int(T3.title())
    add.append([T1,T2,T3])
    df2 = pd.DataFrame(add, columns=colums)
    mytable.add_rows(df2)

    st.success("added")
1 Like

Oh yes ! this is it, how could I not think about it. thank you !

Thank you Chris also for you example, very interesting.

3 Likes