你可以用
st.write(df.to_html(escape=False, index=False), unsafe_allow_html=True)
:
import pandas as pd
import streamlit as st
import openpyxl
import numpy as np
from IPython.core.display import display, HTML
df = pd.read_excel(
io='list.xlsx',
engine= 'openpyxl',
).fillna('')
def make_clickable(link):
text = link.split('=')[0]
return f'<a target="_blank" href="{link}">{text}</a>'
df['TRAILER'] = df['TRAILER'].apply(make_clickable)
st.write(df.to_html(escape=False, index=False), unsafe_allow_html=True)
例如:
import pandas as pd
import streamlit as st
link1 = "https://stackoverflow.com/questions/71641666/hyperlink-in-streamlit-dataframe"
link2 = "https://stackoverflow.com/questions/71731937/how-to-plot-comparison-in-streamlit-dynamically-with-multiselect"
df = pd.DataFrame(
{
"url": [
f'<a target="_blank" href="{link1}">Hyperlink in Streamlit dataframe</a>',
f'<a target="_blank" href="{link2}">How to plot comparison in Streamlit dynamically with multiselect?</a>'
],
"label": ["question", "question"]
}
)
st.write(df.to_html(escape=False, index=False), unsafe_allow_html=True)
它给出: