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).
@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?
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>")
@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)
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 ?
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