Streamlit JavaScript Evaluator Component

1.0.0 · active · verified Thu Apr 16

streamlit-js-eval is a custom Streamlit component that enables developers to execute arbitrary JavaScript expressions directly within a Streamlit application. It facilitates interactions with the browser DOM, local storage, window properties, and more, returning results back to Python. The current version is 1.0.0, and it follows an infrequent release cadence, with stability being a primary focus.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `streamlit_js_eval` to interact with browser-side JavaScript, including retrieving the current URL, setting and getting local storage values, getting screen dimensions, and performing a page redirect. Each `streamlit_js_eval` call requires a unique `key`.

import streamlit as st
from streamlit_js_eval import streamlit_js_eval

st.set_page_config(layout="wide")
st.title("Streamlit JS Eval Demo")

st.header("1. Get Browser URL")
current_url = streamlit_js_eval(js_expressions="window.location.href", key="get_url")
st.write(f"**Current URL:** `{current_url}`")

st.header("2. Set Local Storage Value")
if st.button("Set 'myKey' to 'myValue' in Local Storage"):
    streamlit_js_eval(js_expressions="localStorage.setItem('myKey', 'myValue');", key="set_ls_value", want_result=False)
    st.success("Value set! Refresh the page to see it in browser dev tools.")

st.header("3. Get Local Storage Value")
local_storage_val = streamlit_js_eval(js_expressions="localStorage.getItem('myKey');", key="get_ls_value")
st.write(f"**Local Storage 'myKey':** `{local_storage_val}`")

st.header("4. Get Screen Width")
screen_width = streamlit_js_eval(js_expressions="screen.width", key="get_screen_width")
st.write(f"**Screen Width:** `{screen_width}px`")

st.header("5. Redirect Page (example)")
if st.button("Go to Streamlit.io"):
    streamlit_js_eval(js_expressions="window.location.href='https://streamlit.io';", key="redirect_streamlit", want_result=False)

view raw JSON →