widgetti / widgetti/ipyreact

Example on how to pass a python object into ReactWidget

Open
#17 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
134
Forks
9
PR merge metrics
No merged PRs in 30d

Description

I have a python object of type fig.
This fig is used to initialize the ReactWidget.
In the init function, the figure is processed to a string and then later used in the my_message traitlet.

Here is an example on how I would do that:
Question: is this a best practice solution that we could provide in the docs like this?

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 2))
ax.plot([1,2], [10,20], c = "orange");

###

import ipyreact
from traitlets import Unicode

class TldrawWidget(ipyreact.ReactWidget):
    def __init__(self,my_figure= None, **kwargs):

        my_str = f"the size of the figure is {my_figure.get_size_inches()}"

        super().__init__(**kwargs, my_message=my_str)

    my_message = Unicode("").tag(sync=True) 
    _esm = """
    import * as React from "react";

    export default function MyButton({my_message}) {
        return < button > {my_message} < /button> 
    };"""

TldrawWidget(my_figure=fig)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed Python example and how it initializes ipyreact.ReactWidget with a traitlets Unicode value. Check the existing project documentation for related ReactWidget guidance, then determine whether this example is the recommended pattern and document the agreed approach if confirmed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, react
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.