How can I use info from text_input

I want to perform an operation by finding that value in the dataset according to the value entered by the user.
I don’t know how to do it.

st.text_input("id:")
def func(arg1):
       list = df.loc[df.col_name == arg1, "column"].values
       list = list[0][:]
       return list

or

x = st.text_input("id:")
list = df.loc[df.col_name== x , "column"].values
list = list[0][:]

I wanna the value from “text_input” to be written instead of x.

Welcome to the forum, @KANZ!

I’m not totally sure what exactly you’re expecting to get out of this, but is this the behavior you’re looking for?

import pandas as pd
import streamlit as st

df = pd.DataFrame({"id": ["a", "b", "c"], "column": [4, 5, 6]}).set_index("id")

st.write(df)

x = st.text_input("id:")
if x:
    st.write(df.loc[x, "column"])

If you put in one of the id values (a, b or c) you get the associated value of that row in the "column" column

1 Like
import pandas as pd
import streamlit as st

data = pd.DataFrame({"title": ["A","B","C"], "category":[["n","m","p"],["m","d"],["p","a","k","l"]]})


def func(arg):
       list = data.loc[data.title == arg, "category"].values
       list = list[0][:]  #result -> ["n","m","p"] for func("A")   
       ....
       ....
       ....
func("A")  **#not like this, create with text_input value**
#I won't write this list (st.write()), I'll just use in the loop.

then

Example:

st.title("Generator")
st.text_input("write a arg")

if st.button("Button"):
         for i in list:
              print(list[i])

When a user entered a value (when clicking on button and user sent a value),
I want it to generate a list with (data.title == user’s value).

Something like this should work for you:

from typing import List

import pandas as pd
import streamlit as st

data = pd.DataFrame(
    {
        "title": ["A", "B", "C"],
        "category": [["n", "m", "p"], ["m", "d"], ["p", "a", "k", "l"]],
    }
)


def func(title: str) -> List[str]:
    categories = data.loc[data.title == title, "category"].values[0]
    return categories


st.title("Generator")

title = st.text_input("Choose a title")

if st.button("Button"):
    for category in func(title):
        st.write(category)

not working.

Why is it so difficult to process data from the user?

@KANZ Can you explain what is not working about this solution? If I type “A” in the textbox, I see “n”, “m”, and “p” written, which is what I would expect. What are you expecting it to return, exactly?

1 Like
import pandas as pd
import streamlit as st

data= pd.DataFrame({"name":["A","B","C","D"], "skills":[["a","n","k"],["n","a"],["k","c","d","a"],["s","p"]]})

checking =[]   #[["a","n","k"]] for title 'A'
target =[] 
def func(title:str):
      checking.append(data.loc[data.name == title, "skills"]).values[0]
      for n, skill in data[["name","skills"]].values: 
            if len(list(set(checking[0]).intersection(skill)))) > 1:
                   target.append(n)
            ....
            ....
       return target

title = st.text_input("C a title")

#if work it, target -> ["B"]   

if st.button("Submit"):
     for i in range(len(target)):
            st.markdown(f"<p style=' '....>{target[i]}</p>")

—Streamlit----
Please enter a name

User: wrote A and clicked button
Output:B

@KANZ It’s still not entirely clear to me what exactly you want this app to do.

If the goal is to find the other Names that have overlapping skills with the one the user typed, then typing in A should yield A, B and C. I’m not sure why you would only get [“B”].

With some slight tweaking, this yields ["A, “B”, and “C”] when you type “A”. The only thing you’re doing with Streamlit here is getting a string from the user, so it seems that you may be having issues with your use of pandas and perhaps have some logic issues in your code. If your goal is to have the user type in one name, and then get the resulting other names that have at least 1 overlapping skill, here is your code slightly rewritten to accomplish this.

import pandas as pd
import streamlit as st

data = pd.DataFrame(
    {
        "name": ["A", "B", "C", "D"],
        "skills": [["a", "n", "k"], ["n", "a"], ["k", "c", "d", "a"], ["s", "p"]],
    }
)


def func(title: str):
    target = []
    checking = data.loc[data.name == title, "skills"].values[0]
    for name, skills in data[["name", "skills"]].values:
        if set(checking).intersection(skills):
            target.append(name)
    return target


title = st.text_input("C a title")
# Would perhaps be better to make this a selectbox like so:
# title = st.selectbox("C a title", data.name)

if title:
    matching_names = func(title)

    if st.button("Submit"):
        for name in matching_names:
            st.write(name)

Firstly, I’m sorry.
I have not an equipment, I was trying to write with my hands.

Your solutions:

Expected result:

Also not adding the name of the value entered by the user to the list. I forgot that condition.

Example:
User entered; AR for “len(set().intersection()) >= 1:”
Output:
BR 2 ->a,n
CR 3 ->k

Are you saying that my solution is correct, except it shouldn’t include the option that the user typed? In that case, you can update the if statement in my code to something like this:

        if set(checking).intersection(skills) and name != title:

I understand that this may not solve your issue exactly, but at this point it seems clear that the streamlit part of your code is working fine, and you simply need to spend some time polishing up the python code to do exactly what you want it to do. Can I suggest some simple debugging, starting with simply putting some print() or st.write() statements at important parts in your code to see if the lines of code you think are running are actually running, what the values of different variables are at different points in the app, etc.

For example, you could debug the func function like this:

def func(title: str):
    st.write("Inside func, title = ", title)
    target = []
    checking = data.loc[data.name == title, "skills"].values[0]
    st.write("Checking = ", checking)
    for name, skills in data[["name", "skills"]].values:
        st.write(name, skills)
        st.write(set(checking).intersection(skills))
        if set(checking).intersection(skills) and name != title:
            target.append(name)
    st.write("Return value = ", target)
    return target

Sorry,
ur solution;
Output:
AR 1
BR 2
CR 3
AR 1
BR 2
CR 3
DR 4

not correct.

Please, can u check again ? :pray:

in expected result:

i.startswith(I'm writing here)
or
data.loc[data.name == I'm writing here, "skills]

Anyway;

user = input("title:")
list = []
check = data.loc[data.name == user, "skills"]
for i,j in data[["name", "skills"]].values:
      if user not in list:
           if len(list(set(check).intersection(j))) > 1:
                  list.append(i)
                  if len(list) == 5: break

            
print(list)

Actually, my problem is very simple but I don’t know how it works in streamlit

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