Skip to main content

streamlit-js-callback

Eval js code with callbaack.

Installation instructions

pip install streamlit-js-callback

Usage instructions

import random
import streamlit as st
from streamlit_js_callback import streamlit_js_callback


print("Start render..")

st.subheader("JS Callback Component")


def use_return():
    result1 = streamlit_js_callback("""
    console.log("eval 1 + 1")
    return 1 + 1
    """)
    if result1:
        st.text("Received by use_return: " + str(result1))


def use_send_message():
    result2 = streamlit_js_callback("""
    console.log("hello")
    sendMessage("hello")
    """)
    if result2:
        st.text("Received by use_send_message: " + str(result2))


def exec_async():
    res = streamlit_js_callback("""
    return await fetch("https://reqres.in/api/products/3").then(function(response) {
        return response.json();
    })
    """)
    if res:
        st.text("Received by exec_async: " + str(res))


def exec_event_listen():
    if st.button("btn1"):
        print("btn1 click")
    result3 = streamlit_js_callback("""
        console.log("init 3..")
        let clickCount = 0
        window.parent.document.querySelectorAll('button[kind="secondary"]').forEach((item) => {
            item.addEventListener("click", function(e) {
                clickCount += 1
                console.log(clickCount)
                sendMessage(clickCount)
            });
        })
    """)
    if result3:
        st.text(f"Received event: {result3}")
        print(f"Received event: {result3}")


use_return()
use_send_message()
exec_async()
exec_event_listen()


# my_key = "my_key"
#
#
# @st.fragment
# def exec_in_context_2():
#     global my_key
#     print("my_key", my_key)
#     result4 = streamlit_js_callback("""
#         const res = String(new Date())
#         console.log(res)
#         return res
#         """, key=my_key)
#     if result4:
#         st.text("Received by exec_in_context: " + str(result4))
#         print(f"Received by exec_in_context: {result4}")
#         my_key = str(random.randint(0, 99999))
#         print("my_key", my_key)
#
#
# @st.fragment
# def exec_in_context():
#     if st.button("btn2"):
#         print("btn2 click")
#         exec_in_context_2()
#         print('exec_in_context finish.')
#
#
# exec_in_context()


print("Finish render")

Metadata

Release files for streamlit-js-callback 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for streamlit-js-callback 1.0.2
File Size Uploaded
streamlit-js-callback-1.0.2.tar.gz 403.5 kB Details

Release files / streamlit-js-callback-1.0.2.tar.gz

Download URL streamlit-js-callback-1.0.2.tar.gz
Size 403.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5097fdb157e9dba835f140393fd494c28bf1c1ed0794b8ba2191cb9aae1be20b
BLAKE2b-256 checksum
How to use checksums
79aa9cfb9178dd464775179dbbba4152ab2700bbf5d5e079da33bfc2e7cfc986
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release history Release notifications | RSS feed

This release

1.0.2 This release

1 release file

1.0.0

1 release file

0.3.3

1 release file

0.2.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page