Home > Blockchain >  How to create multiple number inputs and update the dataframe when new entry is there using streamli
How to create multiple number inputs and update the dataframe when new entry is there using streamli

Time:01-11

I am new to Streamlit and I am working on a problem where I am trying to create multiple number inputs based on the number of columns selected and number of values selected for number input. I am able to create number inputs as per columns selected and also the dataframe. But that dataframe is not being updated when I am entering any new number into the input field.

This is the code I am working on :

import streamlit as st
import pandas as pd

exogenous_features = ['Var1', 'Var2']
predict_size = 4
                    
cols = st.columns(len(exogenous_features))
lists = []
                
for p in range(len(exogenous_features)):
    lists.append([])
                    
for i, c in enumerate(cols):
    with c:
        for h in range(predict_size):
            a=st.number_input('',key=str(random.randint(0,100000)))
            lists[i].append(a)
                                
df= pd.DataFrame(lists)
df= bb.transpose()
df.columns = exogenous_features
st.write(df) 

CodePudding user response:

Here is a fixed version of your code.
Note: every new st.number_input must have a unique key otherwise you will always encounter DuplicateWidgetID: Error.

import streamlit as st
import pandas as pd

exogenous_features = ['Var1', 'Var2']
predict_size = 4
                    
cols = st.columns(len(exogenous_features))
lists = []
                
for p in range(len(exogenous_features)):
    lists.append([])
                    
for i, c in enumerate(cols):
    with c:
        for h in range(predict_size):
            key = f"number_input_{i}_{h}"
            a = st.number_input(exogenous_features[i], key=key)
            lists[i].append(a)
                                
df = pd.DataFrame(lists)
df = df.transpose()
df.columns = exogenous_features
st.write(df) 

Output:

enter image description here

  • Related