But what gets rendered is just plain HTML, not the nice looking streamlit rendered dataframe. So perhaps not what you want. Until the streamlit team allows execution of HTML within the streamlit version of dataframe, not sure what else can be done. There is a separate post concerning allowing HTML code execution.
Thanks, all. I got this to work now. Here’s what I am doing:
def make_clickable(link):
# target _blank to open new window
# extract clickable text to display for your link
text = link.split('=')[1]
return f'<a target="_blank" href="{link}">{text}</a>'
# link is the column with hyperlinks
df['link'] = df['link'].apply(make_clickable)
df = df.to_html(escape=False)
st.write(df, unsafe_allow_html=True)
How many characters is your link/markup? I have the problem that streamlit or pandas is cutting off the text at a certain character (followed by an ellipsis ...) which then breaks the HTML.
Edit
Ah got it working, if anyone has the same issue then you need to change pandas` display options:
Hmm I was not able to get this work for me within streamlit. Works outside of using streamlit.
EDIT: Per this discussion your suggestion does not work. However, the current workaround is to use st.table() which perhaps is what you did, but forgot to include that info, which also has some drawbacks too.
If by “outside of streamlit” you mean just a table rather than the scrollable tables then yes, you are right. Ideally I would like to use the scrollable table and HTML (all I really need is the link), but for now I’m happy to settle with having just a static table w/ the links.
As for the implementation, I used a combination of the suggestions here:
# pandas display options
pd.set_option('display.max_colwidth', -1)
def add_stream_url(track_ids):
return [f'https://open.spotify.com/track/{t}' for t in track_ids]
def make_clickable(url, text):
return f'<a target="_blank" href="{url}">{text}</a>'
# show data
if st.checkbox('Include Preview URLs'):
df['preview'] = add_stream_url(df.track_id)
df['preview'] = df['preview'].apply(make_clickable, args = ('Listen',))
st.write(df.to_html(escape = False), unsafe_allow_html = True)
else:
st.write(df)
But if I click on the hyperlink, it is not working. If I tell Mozilla to copy the hyperlink, it copies the following:
file://///server/file.txt
It changes the backslash into slash and the hyperlink does not work. I have to use windows for this application, that’s why the mess of the backslash and slash.