st.column_config.LinkColumn display_test issue with styling

When I use styling on pandas dataframe columns, st.column_config.LinkColumns does not work.

Example:

> st.dataframe(patent_maxscore_df_new[col_order].style.apply(get_cell_color,subset= st.session_state.idea_list),
>                                          column_config={
>                                              "Patent_Link": st.column_config.LinkColumn(
>                                                  "Patent",
>                                                  help="Link to Patent PDF",
>                                                  max_chars=100,
>                                                  display_text="https://ppubs\.uspto\.gov/dirsearch-public/print/downloadPdf/(.*$)"
>                                                  #display_text="Link to PDF"
>                                                  ),
>                                         }
>                                    )

In this case, the display_text does not work as expected. It does not extract the group and displays the entire URL link.
display_text="Link to PDF"

also does not work.
When I remove the style construct, it works as described.
I want to color code my dataframe cells based on the values, and hence I had to use the styling. Would also request to build a column_config feature that can be used to color the cells by applying a function.

Haven’t received any response in 5 days. Did anybody else encounter this issue? Is this a bug?

Hi,

This code displays a dataframe with links and applies color styles to cells based on the values in a specific column.


# Sample dataframe
patent_maxscore_df_new = pd.DataFrame({
    'Patent_Link': ['https://ppubs.uspto.gov/dirsearch-public/print/downloadPdf/12345', 
                    'https://ppubs.uspto.gov/dirsearch-public/print/downloadPdf/67890', 
                    'https://ppubs.uspto.gov/dirsearch-public/print/downloadPdf/11111'],
    'Value': [10, 20, 30]
})

# Function to extract the link text
def extract_link_text(url):
    import re
    match = re.search(r'\/(.*$)', url)
    return match.group(1) if match else url

# Apply the function to the Patent_Link column
patent_maxscore_df_new['Patent_Link'] = patent_maxscore_df_new['Patent_Link'].apply(extract_link_text)

# Function to color code cells based on values
def get_cell_color(val):
    if val > 20:
        return 'background-color: green'
    elif val < 20:
        return 'background-color: red'
    else:
        return ''

# Apply the styling function
styled_df = patent_maxscore_df_new.style.applymap(get_cell_color, subset=['Value'])

# Display the dataframe with links and styling
st.dataframe(styled_df, column_config={
    "Patent_Link": st.column_config.LinkColumn("Patent", help="Link to Patent PDF", max_chars=100)
})

This solution does not display the links properly. It does not extract the number from the url and link it.
e.g It should extract 12345 from “https://ppubs.uspto.gov/dirsearch-public/print/downloadPdf/12345” and link it to the URL.
Here is a better compromise solution:

st.dataframe(
    pd.DataFrame(
        {
            "col_0": [
                "https://google.com",
                "https://docs.streamlit.io",
                "https://streamlit.io/gallery",
                None,
            ],
           "col_1": [
                "Google",
                "Streamlit docs",
                "Streamlit gallery",
                "None"
            ]
        }
    ).style.format(lambda url: "📝" if url else "",subset=["col_0"]),
    column_config={"col_0": st.column_config.LinkColumn()},
)

This code is not what I actually need, but a good compromise.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.