Hi, I’m using st.metrics as a data display component, and I want to contain a hyperlink in the value, but found the value won’t be rendered as markdown or html. Any way to do this?
There are two parts: first is actually making it into a link and the second if you are trying to stay within your app, within the same session.
To deal with the first part by itself, let’s say you want to open another tab to another site when you click on a metric. Here’s a proof of concept, though it makes every metric on a page go to the same place (streamlit.io in my example):
import streamlit as st
st.metric(label='My Metric', value=10000, delta=10)
js = '''
<script>
var metrics = window.parent.document.querySelectorAll('[data-testid="metric-container"]');
metrics.forEach((metric) => {
metric.onclick = function() {
window.open('https://www.streamlit.io/');
}
})
</script>'''
st.components.v1.html(js)
To expand it to your case, you would need set the url conditionally, say based on the label you used in the metric. Here’s a related example that implements a function to change button colors based on their labels:
Now…if you are trying to navigate within your app, you’ll have to layer in even more complication because at this point the only way I know of doing from the front end is to grab the page links by javascript and emulate a click on them… Top-level links from a component iframe? - #8 by mathcatsand
PS: You’d also need to add in an edit to have some hover effect to show it’s a link which can be done as an add-on to the javascript making it a link, or separately though CSS.
Hi,
The above snippet gets all the metric-container on the document.
If there are two metric-container and when clicked both opens same page.
But I want to open different sites when clicked.
How can i achieve that I tried using xpath like below, however it doesn’t seem to work.
var metrics = window.parent.document.querySelector(‘div[text=“link”]’);
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.