Injecting javascript to sort the table which has hyperlinks

Hey, I’m using Streamlit framework in python to display a pandas table that consists of links and prices of some items but I want the links to be hyperlinked and the table should still be dynamic and sortable. I managed to inject js but it gets injected into an iframe when I use the components.html() so the obvious workaround for this would be as follows

import pandas as pd
import streamlit as st
from streamlit.components.v1 import html

data = [["Item 1", "", "$10"],
        ["Item 3", "", "$30"],
        ["Item 2", "", "$20"],]

df = pd.DataFrame(data, columns=["Name", "Link", "Price"])

def make_clickable(val):
    return f'<a href="{val}">{val}</a>'

df["Link"] = df["Link"].apply(make_clickable)
tablehtml = df.to_html(escape=False, index=False, table_id="myTable")
## Add the hyperlinked table to webpage
st.write(tablehtml, unsafe_allow_html=True)

This outputs a table like this which cant sort


Adding the following fixes that

<script src=''></script>
    var table = window.parent.document.getElementById("myTable");
<script>new Tablesort(window.parent.document.getElementById("myTable"));</script>

Note: The html() creates an iframe and its not directly injected to the main page, this means that the iframe cant access the main page if we use document.getElementById("myTable") but we can use window.parent.document.getElementById("myTable") and allow it to access the main page content

The final result


I made this tutorial as I wasted like 3 days to find a fix for this i could only find ways to make the table into a hyperlinked one or just normally sort it.


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