Skip to main content

Streamlit component that enables anywidget support

Project description

Streamlit-AnyWidget

A Streamlit component that brings the power of AnyWidget to Streamlit applications. Create and use custom interactive widgets directly in your Streamlit apps without iframe limitations.

🌟 Features

  • Full AnyWidget Compatibility: Works with both class-based and module-based widget formats
  • Custom Styling: Apply CSS to your widgets for beautiful integration with Streamlit
  • Bidirectional Communication: State updates flow seamlessly between Python and JavaScript
  • Simple API: Familiar interface for AnyWidget users

📦 Installation

pip install streamlit-anywidget

Requires:

  • streamlit >= 0.63
  • anywidget

🚀 Quick Start

import streamlit as st
from streamlit_anywidget import anywidget
from anywidget import AnyWidget
import traitlets

# Create a simple counter widget
class CounterWidget(AnyWidget):
    value = traitlets.Int(0).tag(sync=True)
    
    _esm = """
    export default class CounterWidget {
      constructor(args) {
        this.container = args.container;
        this.value = args.value || 0;
        this.render();
        this.callbacks = [];
      }
      
      render() {
        this.container.innerHTML = `
          <div style="padding: 20px; border: 1px solid #ddd; border-radius: 8px; text-align: center;">
            <h2>Counter: ${this.value}</h2>
            <button id="increment" style="padding: 8px 16px; background: #4CAF50; color: white; border-radius: 4px;">
              Increment
            </button>
          </div>
        `;
        
        this.container.querySelector('#increment').addEventListener('click', () => {
          this.value += 1;
          this.render();
          this.callbacks.forEach(callback => callback({ value: this.value }));
        });
      }
      
      on(event, callback) {
        if (event === 'change') {
          this.callbacks.push(callback);
        }
      }
    }
    """

# Display the widget in Streamlit
st.title("Counter Widget Example")
counter = CounterWidget()
widget_state = anywidget(counter, key="counter")

# Interact with the widget state
st.write(f"Current value: {counter.value}")

🎮 Demo Widgets

Basic Counter Widget

A simple counter widget showcasing basic interactivity.

Alt Text

Text Input Widget

Sync text input between Streamlit and a custom text widget.

Alt Text

Module-based Widget

Using the module-based format for more complex widgets.

Alt Text

🔄 How It Works

Streamlit-AnyWidget creates a bridge between Streamlit's component system and AnyWidget:

  1. Widget Definition: Define your widget in Python with AnyWidget
  2. Streamlit Integration: Use the anywidget() function to render it in Streamlit
  3. State Synchronization: Changes in either Python or JavaScript automatically sync

📋 API Reference

anywidget(widget_instance, key=None)

Renders an AnyWidget instance within Streamlit.

Parameters:

  • widget_instance: An AnyWidget instance
  • key: Optional unique key for the component (string)

Returns:

  • Dictionary containing the current widget state

💡 Example Use Cases

  • Custom Controls: Create specialized UI controls tailored to your data
  • Interactive Visualizations: Build charts and graphs with interactive elements
  • Form Elements: Design custom form inputs with validation and feedback
  • Games & Demos: Create interactive demos and simple games

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📜 License

Distributed under the MIT License. See LICENSE file for more information.

🙏 Acknowledgements

  • AnyWidget - The foundation for this component
  • Streamlit - The awesome framework that makes this possible

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

Uploaded Source

Built Distribution

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

streamlit_anywidget-0.1.1-py3-none-any.whl (409.9 kB view details)

Uploaded Python 3

File details

Details for the file streamlit-anywidget-0.1.1.tar.gz.

File metadata

  • Download URL: streamlit-anywidget-0.1.1.tar.gz
  • Upload date:
  • Size: 408.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for streamlit-anywidget-0.1.1.tar.gz
Algorithm Hash digest
SHA256 98e1f3584dc2d9a0a586eb0c49a1f4a0570d5d7f646bf42861aaa8c141635abd
MD5 ee937ac74b18a29d63aae8246bf69488
BLAKE2b-256 d484c734454f463a50ba5ca30ed3953eeaabf98366eef6630eb9bcc68f621511

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for streamlit_anywidget-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fb325ccd111949740820db4f2c773cac895085973d3b17efa5fec91e8a0e714b
MD5 e3d3dc7595c51286adfd3d0260dcf166
BLAKE2b-256 84c53df38f9ce8e824a4d25e00ca80ba27623787b34d4bb73181e3b850d9512e

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