Is there a way to hover over a streamlit selectbox component and display the currently selected item in a tooltip?
This is useful when the currently selected item’s text is too long.
An example:
selectbox_options = ['extremely long textextremely long textextremely long textextremely long textextremely long textextremely long textextremely long textextremely long text', 'b', 'c']
with st.sidebar.header("Sidebar"):
selected_kb = st.selectbox("Choose something", selectbox_options)
My attempt at using javascript to add a title to my selectbox based on current selection:
script = """
<script>
const updateTooltip = () => {
console.log("Update tool tip triggered!")
const selectbox = document.querySelector(".stSelectbox");
selectboxTextDiv = document.querySelector("#root > div:nth-child(1) > div.withScreencast > div > div > div > section.st-emotion-cache-1cypcdb.eczjsme11 > div.st-emotion-cache-6qob1r.eczjsme3 > div.st-emotion-cache-16txtl3.eczjsme4 > div > div > div > div > div > div > div > div.st-bf.st-bg.st-bz.st-c0.st-c1.st-b4.st-c2.st-c3.st-bh.st-c4.st-c5.st-c6.st-c7 > div.st-c8.st-bf.st-c9.st-ca.st-cb.st-bh.st-cc.st-cd.st-ce")
selectbox.title = selectboxTextDiv.textContent
};
// Initial update
updateTooltip();
// Add event listener for change event
selectboxTextDiv.addEventListener("change", updateTooltip);
</script>
"""
components.html(script)
which errors out:
Update tool tip triggered!
about:srcdoc:9 Uncaught TypeError: Cannot read properties of null (reading 'textContent')
at updateTooltip (about:srcdoc:9:44)
at about:srcdoc:13:9
If I manually add the title field to the selectbox div using chrome dev tools, the hover indeed works how I want it to. But idk how to make it work with streamlit…
This is as close as I could get but the behavior is inconsistent even though my callback is invoked (I see the python print statement everytime, but not the console.log statements). I don’t understand why it doesn’t update the title of the selectbox everytime the value is changed? It seems to update it when I change the selectbox value twice or thrice. I don’t fully understand what I’m seeing. Here’s the code:
import streamlit as st
import streamlit.components.v1 as components
selectbox_options = ['extremely long textextremely long textextremely long textextremely long textextremely long textextremely long textextremely long textextremely long text', 'b', 'c']
# Custom HTML and JavaScript to add tooltips to the selectbox
script = \
"""
<script>
console.log("I'm in the script!!!");
selectbox = window.parent.document.querySelector(".stSelectbox");
console.log("selectbox", selectbox);
selectboxTextDiv = window.parent.document.querySelector("#root > div:nth-child(1) > div.withScreencast > div > div > div > section.st-emotion-cache-1cypcdb.eczjsme11 > div.st-emotion-cache-6qob1r.eczjsme3 > div.st-emotion-cache-16txtl3.eczjsme4 > div > div > div > div > div > div > div > div > div");
console.log("selectboxTextDiv", selectboxTextDiv);
selectbox.title = selectboxTextDiv.textContent;
</script>
"""
# Display the script
def add_title_to_selectbox():
print("callback invoked")
components.html(script)
# Streamlit components
with st.sidebar.header("Sidebar"):
selected_kb = st.selectbox("Choose something", selectbox_options, on_change=add_title_to_selectbox)
Thank you for responding! I really appreciate it, but a toast really doesn’t really feel right for this.
It’s important for the full text to appear on a hover, and only if the user wants to see it (maybe they don’t care to read the very long string, or they already read it when selecting). But after they’ve selected it, I want to give them a way to quickly see what they have selected
I haven’t used st.toast yet btw, so I’m also not fully sure if we can control where the toast appears. In your gif, it appears in the middle of the screen, but my work will all be in the sidebar.
Anyway, I really feel like there must be a way to do this, if we can just find the right javascript function to use as a callback maybe?