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