widgetti / widgetti/ipyreact

Best practice: How to phrase traitlets into js classes?

Open
#24 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 asked chatGPT to convert this code into a widget

%%react

import React, { PureComponent } from 'react';
import ReactDiffViewer from 'react-diff-viewer';

const oldCode = `Hello World`;
const newCode = `Hello New World`;

export default class App extends PureComponent {
  render = () => {
    return (
       <div
        style={{
            position: "relative",
            width: "600px",
            height: "100px",
        }}
        >
      <ReactDiffViewer oldValue={oldCode} newValue={newCode} splitView={true} />
      </div>
    );
  };
}

and after some prompting and telling the GPT how an widget is structured, it came up with this js string literal oldCode = `${this.props.my_old_code}`; syntax, which is working.
My question: Is this best practice?

import ipyreact
from traitlets import Unicode, Bool

class DiffViewerWidget(ipyreact.ReactWidget):
    my_old_code = Unicode('Hello World').tag(sync=True)
    my_new_code = Unicode('Hello New World').tag(sync=True)
    split_view = Bool(True).tag(sync=True)
    
    _esm = """
    import React, { PureComponent } from 'react';
    import ReactDiffViewer from 'react-diff-viewer';

    export default class App extends PureComponent {
        render = () => {
            const oldCode = `${this.props.my_old_code}`;
            const newCode = `${this.props.my_new_code}`;
            return (
                <div
                    style={{
                        position: "relative",
                        width: "600px",
                        height: "100px",
                    }}
                >
                    <ReactDiffViewer oldValue={oldCode} newValue={newCode} splitView={this.props.split_view} />
                </div>
            );
        };
    }
    """
    
d = DiffViewerWidget(my_old_code='Hello World', my_new_code='Hello New World')
d

with the GPT explaination:

In this code, my_old_code and my_new_code are traits defined in the Jupyter widget DiffViewerWidget. When the widget is rendered, their values will be available as properties of the props object in the React component.
The template literals use ${} to embed the values of my_old_code and my_new_code inside a string.

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 with the issue's Python DiffViewerWidget and embedded React _esm example, then examine how ipyreact.ReactWidget exposes traitlet values through props. Done would require a maintainer-confirmed best-practice recommendation or documented pattern for phrasing traitlet values in the React component.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, python, react
Domain
documentation, frontend
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.