Search code examples
python-3.xliststreamlit

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


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)

Suppose if I run the above program, the input would ask in the terminal shown in the below figure

enter image description here

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

enter image description here

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)

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

enter image description here enter image description here

my desired output would look like this

enter image description here


Solution

  • So you attempted to multiply the p matrix and the t matrix using np.multiply(), but the t matrix is a 1-dimensional list, whereas the p matrix is a 2-dimensional NumPy array.

    Since NumPy arrays are designed for element-wise operations, the shapes of the arrays need to match for the element-wise multiplication to work.

    To fix the issue, you the t matrix reshaped to have the same shape as the p matrix using the np.tile() and .T functions. This allows the element-wise multiplication to work correctly.

    Here's the modified code I generated as a fix:

    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(tb):
      value = st.sidebar.number_input(f'Enter the value into {i} row for 
    multiplication:', min_value=0, max_value=10, step=1)
      t.append(value)
    
    st.write(t)
    
    l = []
    n = len(p)
    for i in range(n):
        value = st.sidebar.number_input(f'Enter value {i+1} of the l 
                matrix:')
        l.append(value)
    
    st.write(l)
    
    t = np.array(t)
    # Reshape t to have the same shape as p
    t = np.tile(t, (p.shape[1], 1)).T  
    p_mod = np.multiply(p, t)
    m = max(l)
    idx = np.tile(np.arange(len(p_mod)), int(m))
    idx = idx[np.repeat(np.arange(int(m)), len(l)) < np.tile(l, int(m))]
    p_appended = p_mod[idx]
    
    st.header('Output Data')
    st.dataframe(p_appended)
    

    Input Image Output Image