Timeless
Timeless

Reputation: 37737

Making a hyperlink clickable cell of a DataFrame in Streamlit

I'm trying to display the dataframe df (in the code below) in a Streamlit app.

In Jupyter Notebook, the code run successfully and without any problems.

import pandas as pd

df = pd.DataFrame({
    'name': ['stackoverflow', 'gis stackexchange', 'meta stackexchange'],
    'url': ['https://stackoverflow.com', 'https://gis.stackexchange.com/', 'https://meta.stackexchange.com']
})

def make_clickable(url, name):
    return '<a href="{}" rel="noopener noreferrer" target="_blank">{}</a>'.format(url, name)


df['link'] = df.apply(lambda x: make_clickable(x['url'], x['name']), axis=1)
df.style

Result in Jupyter Notebook

enter image description here

But when I try to implement it in a Streamlit app (by replacing the two last lines by the one below) I get an error.

st.dataframe(df.style.apply(
    lambda x: make_clickable(x['url'], x['name']), axis=1))

Result/Error with Streamlit

ValueError: Function <function at 0x0000028F008DC9D0> resulted in the apply method collapsing to a Series. Usually, this is the result of the function returning a single value, instead of list-like.

Do you have any idea how to fix that ?

Upvotes: 0

Views: 1908

Answers (1)

Caroline Frasca
Caroline Frasca

Reputation: 734

To render HTML in Streamlit, you can use st.markdown, e.g.:

return st.markdown('<a href="{}" rel="noopener noreferrer" target="_blank">{}</a>'.format(url, name),unsafe_allow_html=True)

Upvotes: 2

Related Questions