Skip to main content

React for ipywidgets that just works

Project description

ipyreact

React for ipywidgets that just works. No webpack, no npm, no hassle. Just write jsx, tsx and python.

Build on top of AnyWidget.

Example

Inline code

import ipyreact


class ConfettiWidget(ipyreact.ReactWidget):
    _esm = """
    import confetti from "canvas-confetti";
    import * as React from "react";

    export default function({value, on_value, debug}) {
        return <button onClick={() => confetti() && on_value(value + 1)}>
            {value || 0} times confetti
        </button>
    };"""
ConfettiWidget()

initial-30-fps-compressed

Hot reloading

Create a tsx file:

// confetti.tsx
import confetti from "canvas-confetti";
import * as React from "react";

export default function({value, on_value, debug}) {
    return <button onClick={() => confetti() && on_value(value + 1)}>
        {value || 0} times confetti
    </button>
};

And use it in your python code:

import ipyreact
import pathlib


class ConfettiWidget(ipyreact.ReactWidget):
    _esm = pathlib.Path("confetti.tsx")

ConfettiWidget()

Now edit, save, and see the changes in your browser/notebook.

hot-reload-compressed

IPython magic

First load the ipyreact extension:

%load_ext ipyreact

Then use the %%react magic to directly write jsx/tsx in your notebook:

%%react
import confetti from "canvas-confetti";
import * as React from "react";

export default function({value, on_value, debug}) {
    return <button onClick={() => confetti() && on_value(value + 1)}>
        {value || 0} times confetti
    </button>
};

Access the underlying widget with the name _last_react_widget (e.g. _last_react_widget.value contains the number of clicks):

magic-optimized

Installation

You can install using pip:

pip install ipyreact

Usage

Facts

  • The ReactWidget has an value trait, which is a traitlets.Any trait. Use this to pass data to your react component, or to get data back from your react component.
  • All traits are added as props to your react component (e.g. {value, ...} in th example above.
  • For every trait we also add a on_<traitname> callback, which you can use to set the trait value from your react component (e.g. on_value in the example above).
  • Your code gets transpiled using sucrase in the frontend, no bundler needed.
  • Your code should be written in ES modules.
  • Set debug=True to get more debug information in the browser console (also accessible in the props).

Import maps

For every widget, you can provide an _import_map, which is a dictionary of module names to urls. By default we support react and react-dom which is prebundled.

Apart from react, the default we provide is:

_import_map = {
    "imports": {
        "@mui/material/": "https://esm.sh/@mui/material@5.11.10/",
        "@mui/icons-material/": "https://esm.sh/@mui/icons-material/",
        "canvas-confetti": "https://esm.sh/canvas-confetti@1.6.0",
    },
    "scopes": {
    },
}

Which means we can copy paste most of the examples from mui

%%react -n my_widget -d
import Button from '@mui/material/Button';
import confetti from "canvas-confetti";
import * as React from "react";

export default function({value, on_value, debug}) {
    if(debug) {
        console.log("value=", value, on_value);
    }
    return <Button variant="contained" onClick={() => confetti() && on_value(value + 1)}>
        {value || 0} times confetti
    </Button>
};

We add the https://github.com/guybedford/es-module-shims shim to the browser page for the import maps functionality.

Development Installation

Create a dev environment:

conda create -n ipyreact-dev -c conda-forge nodejs yarn python jupyterlab
conda activate ipyreact-dev

Install the python. This will also build the TS package.

pip install -e ".[test, examples]"

When developing your extensions, you need to manually enable your extensions with the notebook / lab frontend. For lab, this is done by the command:

jupyter labextension develop --overwrite .
yarn run build

For classic notebook, you need to run:

jupyter nbextension install --sys-prefix --symlink --overwrite --py ipyreact
jupyter nbextension enable --sys-prefix --py ipyreact

Note that the --symlink flag doesn't work on Windows, so you will here have to run the install command every time that you rebuild your extension. For certain installations you might also need another flag instead of --sys-prefix, but we won't cover the meaning of those flags here.

How to see your changes

Typescript:

If you use JupyterLab to develop then you can watch the source directory and run JupyterLab at the same time in different terminals to watch for changes in the extension's source and automatically rebuild the widget.

# Watch the source directory in one terminal, automatically rebuilding when needed
yarn run watch
# Run JupyterLab in another terminal
jupyter lab

After a change wait for the build to finish and then refresh your browser and the changes should take effect.

Python:

If you make a change to the python code then you will need to restart the notebook kernel to have it take effect.

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

ipyreact-0.1.0.tar.gz (837.5 kB view details)

Uploaded Source

Built Distribution

ipyreact-0.1.0-py3-none-any.whl (982.6 kB view details)

Uploaded Python 3

File details

Details for the file ipyreact-0.1.0.tar.gz.

File metadata

  • Download URL: ipyreact-0.1.0.tar.gz
  • Upload date:
  • Size: 837.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.0

File hashes

Hashes for ipyreact-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a676867511003af876f2789d75b1cb9701370bf3ead957caa59b74a543939d3f
MD5 4836d7aa4688918c2491f590909e7600
BLAKE2b-256 55087706ce9985ac5cb72c0092d30f6c4ee79be72e5427ee2f90ee6a23680239

See more details on using hashes here.

File details

Details for the file ipyreact-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ipyreact-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 982.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.0

File hashes

Hashes for ipyreact-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d93c27bbe75eedfaee22fa3b33ba72f8fc93901b4d61f96133cb47155b5bdd1a
MD5 65ce30097ec10f6e5b85b235936f613d
BLAKE2b-256 1a0c8eac0954ef5145493f783fb03250a5ea1f232d18039d8cd75e8c66d47e4c

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