Interactive list with action buttons

I couldn’t find a nice implementation for a grid where for each row there are buttons for actions (like edit or delete).
I decided to use markup and render a grid like rows by myself. The main issue with deleting is that indexes change when deleting a list item.
Attaching here a snippet, in hope it will have someone or a better solution will be suggested:

import streamlit as st

if 'my_list' not in st.session_state:
    st.session_state.my_list = [{"name": "a", "val": "b"}, {
        "name": "c", "val": "d"}, {"name": "e", "val": "f"}, {"name": "g", "val": "h"}, {"name": "i", "val": "j"}]

for index, item in enumerate(st.session_state.my_list):
    emp = st.empty()
    col1, col2, col3 = emp.columns([9, 1, 1])
    if col2.button("Del", key=f"but{index}"):
        del st.session_state.my_list[index]
    if col3.button("Edit", key=f"ed{index}"):
        # not implemented - just for example
    if len(st.session_state.my_list) > index:
        col1.markdown(f'My row item **{item}**. Item index {index}', unsafe_allow_html=True)

How it looks:

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