I would like to enter the input data within the streamlit web app

Summary

Share a clear and concise description of the issue. Aim for 2-3 sentences.

Steps to reproduce

Code snippet:

import numpy as np
import streamlit as st
st.header('Input Data')
p = np.array([[4,3,6,2],[1,4,3,5],[2,5,2,3],[5,2,4,1],[3,6,1,4]])
st.dataframe(p)
t = [ ]
tb = len(p)
for i in range(0, tb):
    ele = [int(input(f'Enter the value into {i} row for multiplication:'))]
    t.append(ele)
n = len(p)
l = list(map(int,input(f"\nEnter the {n} numbers row wise to repeat the matrix: ").strip().split()))[:n]
pt = np.multiply(p,t)
m = max(l)
idx = np.tile(np.arange(len(pt)), m)
idx = idx[np.repeat(np.arange(m), len(l)) < np.tile(l, m)]
p = pt[idx]
st.header('Output Data')
st.write(p)

I want to implement the above code. Suppose if I run the above program, the input would ask in the terminal shown in the below figure

I am getting the output like this but I want to enter the the input values within the streamlit webapp

Expected behavior:
I tried to use the streamlit slider for the number input but it shows the error
Code snippet:

import numpy as np
import streamlit as st
st.header('Input Data')
p = np.array([[4,3,6,2],[1,4,3,5],[2,5,2,3],[5,2,4,1],[3,6,1,4]])
st.dataframe(p)
t = [ ]
tb = len(p)
t = [st.sidebar.number_input(f'Enter the value into {i} row for multiplication:', min_value = 0, max_value = 10, step= 1) for i in range(0, tb)]
st.write(t)
n = len(p)
l = [st.sidebar.number_input(f"\nEnter the {n} numbers row wise to repeat the matrix: ") for i in range(0, tb)]
pt = np.multiply(p,t)
m = max(l)
idx = np.tile(np.arange(len(pt)), m)
idx = idx[np.repeat(np.arange(m), len(l)) < np.tile(l, m)]
p = pt[idx]
st.header('Output Data')
st.write(p)

Explain what you expect to happen when you run the code above.
If I run the above program it shows the error and also I would like to get separate list for each value in the t input and vice-versa for l input I would like to get multiple input in same list

Actual behavior:


Debug info

  • Streamlit version: 1.20.0
  • Python version: 3.9
  • Using Conda
  • OS version:windows 10
  • Browser version:

my desired output would look like this

The error you see is because you are trying to create multiple number_input widgets with the same key. You create the list for slider bars for t without giving the widget a key, so streamlit tries to give it a default one. You then try to create more input widgets for l, and streamlit already sees that a widget(s) exists with the same key. You can think of the key as a unique name identifier for each widget (which can also be accessed later using streamlit’s session_state).

You just need to give the widget a unique name when you create it. It’s not generally a good coding approach to dynamically name things, but you could try something like:

t = [st.sidebar.number_input(f"Enter the value into {i} row for multiplication:", 
                             min_value = 0, 
                             max_value = 10, 
                             step= 1,
                             key = "number_input_for_t_"+str(i)) 
                             for i in range(0, tb)]

l = [st.sidebar.number_input(f"\nEnter the {i} numbers row wise to repeat the matrix: ",
                              key = 'number_input_for_l_'+str(i) ) 
                              for i in range(0, tb) ]

I would have to get a separate list for each value (i.e.,) [[2],[2],[2],[2],[2]]. otherwise, it shows the error

Try something like this instead:

# Create widgets - give them all a unique key (name)
# Create 't' widgets
for i in range(0,tb):
   st.sidebar.number_input(f"Enter the value into {i} row for multiplication:", 
                           min_value = 0, 
                           max_value = 10, 
                           step= 1,
                           key = "number_input_for_t_"+str(i)) 
                           for i in range(0, tb)

# Create 'l' widgets
# DO THE SAME AS ABOVE FOR WIDGETS BUT FOR 'l' 
# (i.e. key="number_input_for_l_"+str(i))

# Create t and l lists from session_state variables
t = [st.session_state[f"number_input_for_t_{i}"] for i in range(0,tb) ]
l = [st.session_state[f"number_input_for_l_{i}"] for i in range(0,tb) ]

Thank you very much for your response

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