This can be done in Streamlit by using the āst.dataframeā function rather than the āst.data_editorā or a standard table. With the use of the āst.dataframeā function, which supports rendering HTML content, you can show download links with various labels and URLs.
Here is an illustration of how to use āst.dataframeā to display download links with various labels and URLs:
import pandas as pd
import streamlit as st
data_df = pd.DataFrame(
{
"apps": [
'<a href="https://roadmap.streamlit.app">roadmap</a>',
'<a href="https://extras.streamlit.app">extras</a>',
'<a href="https://issues.streamlit.app">issues</a>',
'<a href="https://30days.streamlit.app">30days</a>',
],
}
)
st.dataframe(data_df, unsafe_allow_html=True)
The āunsafe_allow_html=Trueā option is used in this example to permit the rendering of HTML content inside the āst.dataframeā method. HTML anchor tags (āa>ā) with the proper āhrefā attributes are present in the āappsā column for each link.
If youād rather use āst-aggrid,ā you can also create a custom renderer to do the same thing. Hereās an illustration:
import pandas as pd
import streamlit as st
import streamlit.components.v1 as components
data_df = pd.DataFrame(
{
"apps": [
"roadmap",
"extras",
"issues",
"30days",
],
"urls": [
"https://roadmap.streamlit.app",
"https://extras.streamlit.app",
"https://issues.streamlit.app",
"https://30days.streamlit.app",
],
}
)
renderer_code = """
function(params) {
var link = document.createElement('a');
link.setAttribute('href', params.value);
link.setAttribute('target', '_blank');
link.innerText = params.data.urls[params.rowIndex];
return link;
}
"""
components.html(
f"""
<div id="ag-grid-container" style="height: 300px;"></div>
<script>
var gridOptions = {{
columnDefs: [
{{ field: 'apps', headerName: 'Apps', cellRenderer: 'linkRenderer' }},
],
rowData: {data_df.to_json(orient='records')},
components: {{
linkRenderer: {renderer_code},
}},
}};
new agGrid.Grid(document.querySelector('#ag-grid-container'), gridOptions);
</script>
""",
height=400,
)
In this demonstration, we use JavaScript to build a custom cell renderer that generates an anchor tag (āa>ā) with the proper link and label. The labels are located in the DataFrameās āappsā column, while the URLs are located in the āurlsā column. The labels will appear as clickable links in the produced grid.
Both strategies will produce the desired outcome, so you can pick the one that best meets your preferences and needs.