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.
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: