Javascript wont execute and gets "swallowed"

Observations. When scrip/javascript is inside a st.html it just NEVER got rendered to the page (streamlit v. 1.40).
st.markdown() at least puts the code to the page, but the alert i put there for testing NEVER gets triggered at all.

import streamlit as st
html_string = f'''        
    <script type="text/javascript">
          alert("mystr");
    </script>
'''
st.markdown(html_string,unsafe_allow_html=True)
st.html(html_string)
st.write("test")

From the docs:

Executing JavaScript is not supported at this time.

1 Like

Thanks @Goyo for clarifying about st.html … but i found a bunch of answers here using st.markdown like in the example above. And that injects it fine, but it doesnt get executed at all.

I don’t remember seeing working examples of that, but it isn’t mentioned in the docs either, so I don’t have a clear answer to that. If you con find a working example, compare it to yours and try to figure out what the relevant difference is. I will take a closer look at it when /if I have time.

As mentioned, st.html doesn’t support JS, but you can use streamlit.components.v1 (here’s a demo, wait for a few seconds and you’ll see the alert popping up)

import streamlit as st
import streamlit.components.v1 as components

st.title("JS demo")

components.html("""
<script>
alert("Hello from javascript")
</script>""")
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.