๐ย ย Eval JavaScript
Submitted by Lukas Masuch
Summary
Evaluate JavaScript expressions in the browser and return results to Python.
Functions
eval_javascript
Evaluate a JavaScript expression in the browser and return the result.
This component evaluates a JavaScript expression in the user's browser using
eval() and returns the serialized result to Python. The component itself is
invisible (zero-height).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
str
|
The JavaScript expression to evaluate. Can be synchronous or return a Promise for async evaluation. |
required |
key
|
str
|
A unique key for this component instance. Required to track state across reruns. |
required |
Returns:
| Type | Description |
|---|---|
Any | None
|
The result of the JavaScript expression, serialized to a Python-compatible |
Any | None
|
value. Returns None while the expression is being evaluated or if the |
Any | None
|
expression is empty. |
Note
- The expression is evaluated with browser-side JavaScript
eval() - Supports async expressions (Promises are automatically awaited)
- Complex objects are serialized (Maps become dicts, Sets become lists, etc.)
- Errors are captured and can be accessed via session state
Example:
```python
user_agent = eval_javascript("window.navigator.userAgent", key="ua")
if user_agent:
st.write(f"Your browser: {user_agent}")
```
Source code in src/streamlit_extras/eval_javascript/__init__.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
Import:
- You should add this to the top of your .py file
Examples
example
def example() -> None:
"""Example usage of the eval_javascript component."""
examples = {
"User agent": "window.navigator.userAgent",
"Window width": "window.innerWidth",
"Location summary": "({ href: window.location.href, host: window.location.host })",
"Async example": "Promise.resolve({ language: window.navigator.language })",
"Thrown error": "(() => { throw new Error('Boom'); })()",
}
selected_example = st.selectbox("Example expression", list(examples))
selected_expression = "window.navigator.userAgent" if selected_example is None else examples[selected_example]
expression = st.text_area(
"JavaScript expression",
value=selected_expression,
height=140,
help="The expression is evaluated with browser-side JavaScript `eval(...)`.",
)
value = eval_javascript(expression, key="eval_javascript_demo")
component_state = st.session_state.get("eval_javascript_demo", {})
status = component_state.get("status", "idle")
error = component_state.get("error")
st.subheader("Python result")
if status == "running":
st.info("Evaluating in the browser...")
elif error:
error_name = error.get("name", "Error")
error_message = error.get("message", "Unknown error")
st.error(f"{error_name}: {error_message}")
if error.get("stack"):
st.code(error["stack"], language="text")
else:
st.write(value)