I have the following code, wherein I want to add custom JS:
import streamlit as st
import streamlit.components.v1 as components
st.write("Start...")
components.html("<script>alert('My Alert Msg!')</script>", height=0, width=0)
st.write("End...")
However, with each invocation of components.html, I get a blank line onscreen.
If the above code is run, a blank line will be output between the ‘Start’ & ‘End’ st.write commands.
Is there a way that this blank line can be not outputted onscreen?
I use containers to hide iframes at the end when I don’t actually want them rendered. I’m sure some CSS could do the trick too, but perhaps this shortcut would be sufficient for your case? (This usually comes up for me within functions.)
body = st.container()
footer = st.container()
with body:
st.write("Start...")
with footer:
st.components.v1.html("<script>alert('My Alert Msg!')</script>")
with body:
st.write("End...")
However, this was just an idea for something different: In the JS part, could we pass code to change the visual properties of onscreen widgets by just passing its label parameter (so that we don’t have to inspect the html page and revise the ids with each streamlit revision) each time.
Use case: In a collection of onscreen widgets, colour red the labels of all widgets which are to be mandatory inputs.
I wrote the code for that, and it works very well; however, I cant do away with the blank lines, as the changes disappear if I use st.empty. (I even tried the available jseval components).
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.