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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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