Skip to main content

Streamlit component for doing message passing between parent window and server via JS

Project description

streamlit-message-passing

Streamlit component for doing message passing between parent window and server via JS

Installation instructions

pip install streamlit-message-passing

Usage instructions

import streamlit as st

from msgpassing import listen_for_parameters

# If parent window is running on http://localhost:8080 ...
data = listen_for_parameters(web_url="http://localhost:8080")

# We can wait until we have these parameters:
if not data:
  st.stop()

st.markdown("Parameters is: ```%s```" % json.dumps(data, indent=2))

On the parent window, you can do something like this:

<iframe src="http://localhost:8501/" frameborder="0"></iframe>
<script>
  document.addEventListener('DOMContentLoaded', function () {
    const messageHandler = (event) => {
      console.debug("Received message from iframe:", event.data, event.origin)

      // Optional: Check if we're receiving messages from our dashboard only
      // We assume that the streamlit app is running on http://localhost:8501
      if (event.origin !== "http://localhost:8501") return;

      if (event.data.type === "ready") {
        // We are ready to send these parameters to streamlit
        const msg = { 
          type: "parameters", 
          foo: "1", 
          bar: 2
        };
        
        console.debug("Sending parameters to iframe:", msg);
        event.source.postMessage(msg, "http://localhost:8501");
      }
    };

    window.addEventListener("message", messageHandler, false);
  });
</script>

See iframe.html for a full example.

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_message_passing-0.1.1.tar.gz (439.5 kB view details)

Uploaded Source

Built Distribution

streamlit_message_passing-0.1.1-py3-none-any.whl (443.7 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_message_passing-0.1.1.tar.gz.

File metadata

  • Download URL: streamlit_message_passing-0.1.1.tar.gz
  • Upload date:
  • Size: 439.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.9.6 Darwin/22.6.0

File hashes

Hashes for streamlit_message_passing-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0a2ab5b7d75ec1ad5a986a9eb6799235dfbcbe131d5bf20a8325e2bcd3b3c721
MD5 7993cd4fba5a28160af73373c4fcbfdd
BLAKE2b-256 44e02c6e82399153c7a288892444106671b18a1b254e2cc296480da85d9c3a4e

See more details on using hashes here.

File details

Details for the file streamlit_message_passing-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_message_passing-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f582a28884dbd3863196f5a1020b78042438e9a77d347f875d477f80734664f4
MD5 fd2efa548c6d021679e12529038abe63
BLAKE2b-256 0164d0452abb4d53e79cd801d81068795c31f11495be0d9faf75b76cf3d3205b

See more details on using hashes here.

Supported by

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