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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.