Skip to main content

Integrate React components into NiceGUI applications

Project description

NiceGUI React Integration

Integrate React components into your NiceGUI applications.

Introduction

I created this package to seamlessly embed React components within NiceGUI projects. It handles bundling your React code using Vite, serves the compiled assets, and provides a way to pass props from Python to React and handle events from React in Python.

Features

  • Easy Integration: Embed React components directly into NiceGUI applications.
  • Props and Events: Pass props and handle events between Python and React.
  • Automatic Bundling: Automatically bundles your React code using Vite.
  • Caching: Efficiently caches builds to improve performance during development.

Installation

Install the package via pip:

pip install nicegui-react

Requirements

  • Python 3.6 or higher
  • NiceGUI installed
  • Node.js and npm installed (for building the React project)

Usage

Here's how to use the React class in your NiceGUI application:

from nicegui import ui
from nicegui_react import React

@ui.page("/")
async def index():
    with ui.card():
        ui.label('Here is the React component:')
        ui.button('Click me', on_click=lambda: react.props(title="Updated Title"))

        with ui.card_section():
            react = React(
                react_project_path="./path_to_your_react_project",
                main_component="App"  # Replace with your main component's name
            ).style('width: 100%; height: 100%;').props(
                title="Hello from Python!"
            ).on('onClick', lambda event: ui.notify(f'Clicked on React component: {event}'))

Parameters

  • react_project_path (str): Path to your React project directory.
  • main_component (str): Name of the main React component to render.
  • component_id (str, optional): Unique identifier for the component instance.
  • env (dict, optional): Environment variables to pass to the React app.
  • use_legacy_peer_deps (bool, optional): Whether to use legacy peer dependencies during npm install.
  • dev (bool, optional): If set to True, enables development mode.

Methods

  • props(**kwargs): Update the props passed to the React component.
  • on(event_name, handler): Register an event handler for events emitted from React.

Setting Up Your React Project

  • Place your React project in a directory relative to your Python script.
  • Ensure your package.json includes the necessary dependencies and scripts. The React class will help set this up if it's missing.
  • Your main component should be properly exported.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are welcome.

Contact

Feel free to reach out if you have any questions or suggestions.

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

nicegui_react-0.1.5.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

nicegui_react-0.1.5-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file nicegui_react-0.1.5.tar.gz.

File metadata

  • Download URL: nicegui_react-0.1.5.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for nicegui_react-0.1.5.tar.gz
Algorithm Hash digest
SHA256 d6445342e13161a4953690fa9d6fb85dfbdd5daa2e4695ab1403d7a0ef72c17d
MD5 b9afd55a81c9057475be93baa922aec8
BLAKE2b-256 e38f0f3039a73b1e29496c7fef3484e869e5519cac904f5078111cf7ffd8b36f

See more details on using hashes here.

File details

Details for the file nicegui_react-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: nicegui_react-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for nicegui_react-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9aa9259aa4342e8a03baab625e8c96dc8338ee8d056c6533232df781341bb30b
MD5 cb20cc128edcd82cb2bf924f60909603
BLAKE2b-256 44d88b5d40b52a98626b9f2f917be9e742722442792035f2c1691313552ebfd4

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