Call Javascript Functions out of Streamlit - HTML5 Vibrate API

Basically I want to make the device vibrate on certain occasions, and haven’t been able to find a reliable way to execute the script consistently.

This is my sample function:

st.markdown(
    """
    <script>
        function vibrate() {
            navigator.vibrate(1000)
            }
    </script>
    """,
    unsafe_allow_html=True
)

This is how I’m trying to call it using the streamlit-javascript package just as a test:

if st.button("Vibrate"):
        js_code = """vibrate()"""
        res = st_javascript(js_code)

This does nothing.
If I try and skip the middleman like this:

if st.button("Vibrate"):
        st_javascript("""navigator.vibrate(1000)""")

It doesn’t work either but exhibits the following behaviour: If I click it a lot of times in quick succession it briefly vibrates, but not for the specified amount of time.

Basically my question is:

  1. How can I vibrate the device using Streamlit?
  2. If there is no easy solution, how can I run any JS script reliably out of Streamlit (so I can vibrate)

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