St.button inside html generated by markdown

Hi,
is there possible to call a python function inside a html generated markdown?

I have something like this:

def img_to_markdown(img_path, inherit=""):
    def img_to_bytes(path):
        return base64.b64encode(Path(path).read_bytes()).decode()

    class_inherit = f"class='{inherit}'" if inherit else ""
    img_html = (
        f"""<img src='data:image/png;base64,{img_to_bytes(img_path)}' {class_inherit}>""")
    return img_html

class Table():
    def btn_download_button(self, raw: str) -> str:
        if raw:
            return f"""<a href='{raw}' download='{raw}' class='centered-image-button'>
                       {img_to_markdown("./images/download.png", inherit='enabled')}
                    </a>"""
        else:
            return f"""<div class='centered-image-button'>
                        {img_to_markdown("./images/download.png", inherit='disabled')}
                    </div>"""

    def btn_delete(self, key):
        return f"""<div key={key} class='centered-image-button'>
                    {img_to_markdown("./images/delete.png", inherit='enabled')}
                </div>"""

    def render(self, documents):
        table_data = [[document.field1,
                       document.field2,
                       self.btn_download_button(document.field3),
                       self.btn_download_button(document.field4),
                       self.btn_delete(document.delete)]
                      for document in documents]

        table_rows = [f"<tr>{''.join([f'<td>{d}</td>' for d in document])}</tr>" for document in table_data]
        if not table_rows:
            table_rows = ["<tr></tr>"]
            st.warning("No files found.")
        table = f"""<table> 
                        <tr>
                            <th>Field1</th>
                            <th>FIeld2</th>
                            <th>Field3</th>
                            <th>Field4</th>
                            <th>Delete</th>
                        </tr>
                        {"".join(table_rows)}
                    </table>"""
        st.markdown(table, unsafe_allow_html=True)

And i want to update the btn_delete in a way to call a python function, something like that:

def delete(key):
    ...

def btn_delete(self, key):
        return st.button(label=f"Delete {key}", on_click=delete, args=key)

In this way the buttons are rendered but not inside the table. Is there a way to add the function to the html tag or to render the st.button inside the table ?

Hope to be clear

Thanks a lot!

Did anyone find a solution to this? I have created cards to showcase data and I want to include a button in my card to be able to show the data frame when the users click.

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