Skip to main content

A custom Streamlit component to evaluate arbitrary Javascript expressions.

Project description

Streamlit-JS-Eval

PyPI version Downloads Streamlit App

SJE is a custom Streamlit component, built to evaluate arbitrary Javascript expressions and return the result. It can become useful in doing certain functionalities which are simple things in JavaScript, but unavailable or difficult to do in Streamlit. Examples include cookie management, writing to clipboard, getting device width (e.g. to check if we are on a mobile device), getting browser language, sharing something through Android's share feature, knowing user agent, etc. See MDN docs for more information about Web APIs.

  • Version 0.1.7 - March 2024: Proposed a workaround for issue #2.

Install

pip3 install streamlit_js_eval

Example

st.write(f"Screen width is {streamlit_js_eval(js_expressions='screen.width', key = 'SCR')}")

key is an arbitrary but unique string, required by Streamlit components API for each call to streamlit_js_eval.

Common JavaScript functionalities

Some more common functionalities are already implemented as Python functions. Examples include:

# Returns user's location after asking for permission
location = get_geolocation()

# Check if location permission was denied
if location and 'error' in location:
    if location['error']['code'] == 1:
        st.error("Location permission denied")
    else:
        st.warning(f"Geolocation error: {location['error']['message']}")
elif location:
    st.write(f"Latitude: {location['coords']['latitude']}")
    st.write(f"Longitude: {location['coords']['longitude']}")

# The URL parts of the page
location_json = get_page_location()

See streamlit_js_eval/__init__.py for more functions. Check a demo in example.py or see it live.

Handling Geolocation Errors

The get_geolocation() function now returns error information when the user denies permission or when geolocation fails. The returned object will have an error key with code and message fields:

location = get_geolocation()

if location and 'error' in location:
    error_code = location['error']['code']
    error_msg = location['error']['message']
    
    # Error codes:
    # 0: Browser doesn't support geolocation
    # 1: Permission denied by user
    # 2: Position unavailable
    # 3: Timeout
    
    if error_code == 1:
        st.error("User denied location permission")
        # Disable location-related buttons, etc.
    else:
        st.warning(f"Geolocation error: {error_msg}")
elif location:
    # Success - location contains coords and timestamp
    st.write(f"Lat: {location['coords']['latitude']}, Lon: {location['coords']['longitude']}")

Known Limitations

  • It seems SJE has issues with st.button when getting called from inside a branch in Streamlit (e.g. in a loop, if-else block, ...). In version 0.1.7, you may use the custom bootstrapButton as a workaround in such situations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_js_eval-1.0.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_js_eval-1.0.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_js_eval-1.0.0.tar.gz.

File metadata

  • Download URL: streamlit_js_eval-1.0.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for streamlit_js_eval-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9e2b2005e3e8a4971d222488a02c880b7b93c8951393ff7f97419ded914498e4
MD5 f993f12cd0b0bf0c0669bd5640b952df
BLAKE2b-256 10431403cdd4cafd4df8509e60d04b67bbd2e5fa454c03528d53e337c8022b1f

See more details on using hashes here.

File details

Details for the file streamlit_js_eval-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_js_eval-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4876f56dadb6a578615661b43bbec772c93dd4cd12efb2a2310534da06c34dbb
MD5 af7be4ce3fbc36d7a4906393db004d7b
BLAKE2b-256 7c40ae13b0290e89056ea16fbce331a94fa8bfb50cfeb7c1947e61e842636f94

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page