0

I'm new to Python, so please be lenient.

I want to read the value of a single cell from a dataframe based on the selected row. I do this as below, but I get the value not when I click on a record (when the selection checkbox is checked), but only after I click on another one or uncheck the row. Why?

I get some record from the database and draw a table using Streamlit and pandas:

employees = get_all_employees()
event = st.dataframe(
    employees,
    hide_index=True,
    selection_mode="single-row",
    on_select=on_employee_select,
)

Then the on_select function goes like this:

def on_employee_select():
    global selected_emp_id, selected_emp_name
    row = event.selection.rows  # type: ignore # List of selected row indices
    if row:
        selected_emp_id = int(employees.iat[row[0], 0]) # type: ignore
        selected_emp_name = str(employees.iat[row[0], 1]) # type: ignore
    else:
        selected_emp_id = 0
        selected_emp_name = ""
    st.write("on_employee_select: ", selected_emp_id, selected_emp_name)

How should it be done?

3
  • 4
    Are you using streamlit? If so, you should add the streamlit tag. Also, see minimal reproducible example. Commented Nov 8 at 21:44
  • 1
    You're talking about stuff that's not part of Pandas, so I'm assuming it's part of Streamlit so I added the tag for it. Correct me if I'm wrong. Commented Nov 9 at 0:29
  • BTW, welcome to SO! Check out the tour and for tips, How to Ask. Commented Nov 9 at 0:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.