Streamlit-Javascript Component

0.1.5 · active · verified Thu Apr 16

Streamlit-javascript is a third-party Streamlit component (current version 0.1.5) that enables Python applications to execute arbitrary JavaScript code on the client-side browser and retrieve the results back into the Streamlit script. This facilitates advanced frontend interactions and data retrieval not natively supported by Streamlit, such as accessing browser-specific properties or making client-side API calls. The library's release cadence appears to be slow, with the latest PyPI version from May 2022.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `st_javascript` to execute both asynchronous JavaScript (e.g., an `fetch` API call) and synchronous JavaScript (e.g., retrieving `window.innerWidth`). The `st_javascript` function returns the result of the JavaScript execution, which can be `None` on initial page load or before the JavaScript completes.

import streamlit as st
from streamlit_javascript import st_javascript

st.subheader("Javascript API Call Example")

# Example 1: Making an asynchronous fetch request in JavaScript
# The result is returned to Python once the JS promise resolves.
return_value = st_javascript("""
    await fetch("https://reqres.in/api/products/3")
        .then(function(response) {
            return response.json();
        })
""")

if return_value:
    st.markdown(f"Return value was: `{return_value}`")
    st.write("Parsed JSON:", return_value) # Streamlit-javascript attempts to parse JSON strings into Python objects
else:
    st.info("Waiting for JavaScript fetch to complete...")

st.subheader("Get Browser Window Width Example")

# Example 2: Getting a simple browser property (synchronous)
window_width = st_javascript("window.innerWidth")

if window_width:
    st.write(f"Current browser window width: {window_width}px")
else:
    st.info("Waiting for JavaScript to return window width...")

view raw JSON →