plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.
https://plotly.com/dash
MIT License
21.42k stars 2.06k forks source link

Allow extracting meta data from typescript components #719

Closed andrenanninga closed 9 months ago

andrenanninga commented 5 years ago

We would like to use dash-generate-components on some Typescript components to allow our Python developers to use them in their Dash code. We have a custom pipeline to transpile the Typescript components to a bundle but we're currently blocked by this line of code:

https://github.com/plotly/dash/blob/ec1576c3652cbb90ffb064abe2753a17e13f3c51/dash/extract-meta.js#L63

Extracting metadata from components is currently only allowed for .js and .jsx files. However react-docgen has no issues extracting metadata from Flow or Typescript.

Would you support the addition of .ts and .tsx files to this filter. This would allow us to continue using Typescript.

Example typescript code

(Currently I am saving this as Dev.jsx to allow the extracting of metadata.)

import React from 'react';

type Props = {
    /**
     * my description of what a label is
     */
    label?: string,
};

export default class Test extends React.Component<Props> {
    render() {
        const {label} = this.props;
        return <h1>{label}</h1>;
    }
}
# AUTO GENERATED FILE - DO NOT EDIT

from dash.development.base_component import Component, _explicitize_args

class Test(Component):
    """A Test component.

Keyword arguments:
- label (string; optional): my description of what a label is"""
    @_explicitize_args
    def __init__(self, label=Component.UNDEFINED, **kwargs):
        self._prop_names = ['label']
        self._type = 'Test'
        self._namespace = 'something'
        self._valid_wildcard_attributes =            []
        self.available_properties = ['label']
        self.available_wildcard_properties =            []

        _explicit_args = kwargs.pop('_explicit_args')
        _locals = locals()
        _locals.update(kwargs)  # For wildcard attrs
        args = {k: _locals[k] for k in _explicit_args if k != 'children'}

        for k in []:
            if k not in args:
                raise TypeError(
                    'Required argument `' + k + '` was not specified.')
        super(Test, self).__init__(**args)
alexcjohnson commented 5 years ago

Good idea! @Marc-Andre-Rivet do you see any blockers to allowing this? We already support flow, I can imagine us running into problems with complex types but presumably as long as people stay within reason we should be able to handle it.

Marc-Andre-Rivet commented 5 years ago

This has been on my radar for a while and been looking at the available options periodically. Last time I've tried this the types were not pulled correctly past a certain complexity and was having trouble with imported types. Being based on the Babel AST, some information was getting lost along the way. @andrenanninga is pointing to a different implementation/solution than what I tried. Would need testing but if it works I'm 100% for it. If this works fine with the table, I'd argue it will probably be fine with anything we can throw at it.

Marc-Andre-Rivet commented 5 years ago

I'll put this on the tech roadmap for tracking.

Marc-Andre-Rivet commented 5 years ago

Looking at this:

A partial solution that works to solve both the TS typing needs and the runtime prop types needs is using PropTypes.InferProps example which will take a propTypes structure and spew out a TS interface. I haven't tested this in depth but it did seem to behave as expected / provide correct typing enforcement when I played around with it in the table.

All in all, there are definite upsides to improve direct TS support but want to make it clear that a component developer will lose access to some Dash features (DevTools) in doing so. The duck-typing nature of TS does not make this something we can fix later.

chabb commented 4 years ago

Is there any progress here ?. We have some typescript React components that we'll like to use in dash. I'm currently publishing my typescript components, and then wrapping them in plain JS components.

Marc-Andre-Rivet commented 4 years ago

@chabb I last had a look at this in ~November, support was still not quite there but there is progress being made. I haven't had the time to look since. The DataTable suffers from the same situation you describe above, having to duplicate the PropTypes into TypeScript types. I'll try and have another look at this in the next couple of weeks. Better TS support would allow significant 🔪🌴🐫 in our codebase too.

anders-kiaer commented 3 years ago

Since type annotations in Python 3+ have taken inspiration from TypeScript, there is some isomorphism between the two.

Due to this, another feature this issue could potentially enable (in Dash 2+ when Python 2.7 support is dropped) is to generate type annotated Dash (Python) components based on the TypeScript type definitions.

Advantages:

frnhr commented 2 years ago

Just wanted to link this here, seem like serious progress is being made on this front! 🥳 https://github.com/plotly/dash/pull/1956

alexcjohnson commented 9 months ago

Looks like we forgot to close this with #1956 😄