1- Markdown disabling button from working
2- Adding a graphic element+text on top of a tab name
Steps to reproduce
Code snippet:
β¦
with col2:
# Send button with styling and unique session state key for each category
if st.session_state.get(f"send_clicked_{category}β):
send_button = True
st.session_state[f"send_clicked_{category}β] = False # Reset the state
else:
send_button = False
# CSS-styled markdown button with a JavaScript click handler referencing the unique session state variable for the current category
st.markdown(
f'<a href="#" class="btn-custom" onclick="window.streamlitSetComponentValue(`send_clicked_{category}`, true)">Send Email</a>',
unsafe_allow_html=True
)
with col3:
delete_button = st.button("Delete Email", key=f"delete_button_{category}")
β¦
I am using the tabs system with columns and st.session.state,
giving each button a unique key depending on the tab name (each tab has the same setup with different data input)
**Expected behavior: The markdown button (col2) doesnβt work, in comparison to regular buttons which work in the same tab (col3)
Debug info
Streamlit version: (Streamlit v1.26.0)
Python version: (Python 3.11.4)
Using Conda? yes
OS version: Windows 11
2-
Not an existing issue but looking for a tip about how to add a round square/circle with a variable inside of it, directly on top of the tab names, similar to how facebook show new notifications.
Can you explain why you are trying to use a markdown button and js click handlers, rather than just using normal streamlit buttons? In this case window.streamlitSetComponentValue isnβt defined, as this isnβt inside of a custom component where that has been imported or defined, and if is generating a javascript error on click.
I would recommend trying to accomplish the same thing with st.button, or, if really necessary create an entire component to accomplish whatever you are trying to accomplish. If you are just trying to get back which button has been clicked, then you can do this:
import streamlit as st
tab1, tab2 = st.tabs(["Tab 1", "Tab 2"])
with tab1:
tab1_button1_clicked = st.button("Tab 1 Button 1")
tab1_button2_clicked = st.button("Tab 1 Button 2")
with tab2:
tab2_button1_clicked = st.button("Tab 2 Button 1")
tab2_button2_clicked = st.button("Tab 2 Button 2")
if tab1_button1_clicked:
st.write("Tab 1 Button 1 clicked!")
...
For the second question, you can do something like this
import streamlit as st
if "new_emails" not in st.session_state:
st.session_state["new_emails"] = 0
if "new_messages" not in st.session_state:
st.session_state["new_messages"] = 0
if st.button("New email"):
st.session_state["new_emails"] += 1
if st.button("New message"):
st.session_state["new_messages"] += 1
tab1_name = (
"Emails"
if st.session_state["new_emails"] == 0
else f"Emails ({st.session_state['new_emails']})"
)
tab2_name = (
"Messages"
if st.session_state["new_messages"] == 0
else f"Messages ({st.session_state['new_messages']})"
)
tab1, tab2 = st.tabs([tab1_name, tab2_name])
with tab1:
st.write("Tab 1")
st.write(f"You have {st.session_state['new_emails']} new emails.")
with tab2:
st.write("Tab 2")
st.write(f"You have {st.session_state['new_messages']} new messages.")
Thank you so much for the mega quick response.
My reasoning for markdown is mainly the design, which is an issue I assume will repeat a lot, as I focus a lot on UI design too.
I need some buttons to look differently.
The buttons work with st.button perfectly, just adding the CSS via markdown breaks it somehow.
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.