I'm new to pythonPython, 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?