当前位置: 首页 > 知识库问答 >
问题:

将响应网格布局转换为绘图仪划线

郁明诚
2023-03-14

我是一个非常活跃的Dash用户,我已经开始发现我的Dash使用中有很多限制,我意识到关于如何将组件转换为dash的信息/内容绝对有限,有过时和非常简单的例子...我对Javascript或React几乎没有任何了解,我完全不知道如何转换组件。

我试图将响应式网格布局组件从react.js转换为Ploly Dash,但我不知道在这种情况下应该如何处理属性?链接到组件:https://github.com/STRML/react-grid-layout/blob/master/lib/ResponsiveReactGridLayout.jsx

由于我没有使用react.js的经验,我不知道应该做哪些修改才能将此组件转换为Plotly Dash。

对于上面的组件,我应该只html" target="_blank">声明Proptypes上的属性(如下所示),还是需要做更多的修改?

ResponsiveReactGridLayout.propTypes{
  //
  // Basic props
  //
  className: PropTypes.string,
  style: PropTypes.object,

  // This can be set explicitly. If it is not set, it will automatically
  // be set to the container width. Note that resizes will *not* cause this to adjust.
  // If you need that behavior, use WidthProvider.
  width: PropTypes.number,

  // If true, the container height swells and contracts to fit contents
  autoSize: PropTypes.bool,
  // # of cols.
  cols: PropTypes.number,

  // A selector that will not be draggable.
  draggableCancel: PropTypes.string,
  // A selector for the draggable handler
  draggableHandle: PropTypes.string,

  // Deprecated
  verticalCompact: function (props: Props) {
    if (
      props.verticalCompact === false &&
      process.env.NODE_ENV !== "production"
    ) {
      console.warn(
        // eslint-disable-line no-console
        "`verticalCompact` on <ReactGridLayout> is deprecated and will be removed soon. " +
          'Use `compactType`: "horizontal" | "vertical" | null.'
      );
    }
  },
  // Choose vertical or hotizontal compaction
  compactType: PropTypes.oneOf(["vertical", "horizontal"]),

  // layout is an array of object with the format:
  // {x: Number, y: Number, w: Number, h: Number, i: String}
  layout: function (props: Props) {
    var layout = props.layout;
    // I hope you're setting the data-grid property on the grid items
    if (layout === undefined) return;
    require("./utils").validateLayout(layout, "layout");
  },

  //
  // Grid Dimensions
  //

  // Margin between items [x, y] in px
  margin: PropTypes.arrayOf(PropTypes.number),
  // Padding inside the container [x, y] in px
  containerPadding: PropTypes.arrayOf(PropTypes.number),
  // Rows have a static height, but you can change this based on breakpoints if you like
  rowHeight: PropTypes.number,
  // Default Infinity, but you can specify a max here if you like.
  // Note that this isn't fully fleshed out and won't error if you specify a layout that
  // extends beyond the row capacity. It will, however, not allow users to drag/resize
  // an item past the barrier. They can push items beyond the barrier, though.
  // Intentionally not documented for this reason.
  maxRows: PropTypes.number,

  //
  // Flags
  //
  isBounded: PropTypes.bool,
  isDraggable: PropTypes.bool,
  isResizable: PropTypes.bool,
  // If true, grid items won't change position when being dragged over.
  preventCollision: PropTypes.bool,
  // Use CSS transforms instead of top/left
  useCSSTransforms: PropTypes.bool,
  // parent layout transform scale
  transformScale: PropTypes.number,
  // If true, an external element can trigger onDrop callback with a specific grid position as a parameter
  isDroppable: PropTypes.bool,

  // Resize handle options
  resizeHandles: resizeHandlesType,
  resizeHandle: resizeHandleType,

  //
  // Callbacks
  //

  // Callback so you can save the layout. Calls after each drag & resize stops.
  onLayoutChange: PropTypes.func,

  // Calls when drag starts. Callback is of the signature (layout, oldItem, newItem, placeholder, e, ?node).
  // All callbacks below have the same signature. 'start' and 'stop' callbacks omit the 'placeholder'.
  onDragStart: PropTypes.func,
  // Calls on each drag movement.
  onDrag: PropTypes.func,
  // Calls when drag is complete.
  onDragStop: PropTypes.func,
  //Calls when resize starts.
  onResizeStart: PropTypes.func,
  // Calls when resize movement happens.
  onResize: PropTypes.func,
  // Calls when resize is complete.
  onResizeStop: PropTypes.func,
  // Calls when some element is dropped.
  onDrop: PropTypes.func,

  //
  // Other validations
  //

  droppingItem: PropTypes.shape({
    i: PropTypes.string.isRequired,
    w: PropTypes.number.isRequired,
    h: PropTypes.number.isRequired
  }),

  // Children must not have duplicate keys.
  children: function (props: Props, propName: string) {
    var children = props[propName];

    // Check children keys for duplicates. Throw if found.
    var keys = {};
    React.Children.forEach(children, function (child) {
      if (keys[child.key]) {
        throw new Error(
          'Duplicate child key "' +
            child.key +
            '" found! This will cause problems in ReactGridLayout.'
        );
      }
      keys[child.key] = true;
    });
  },

  // Optional ref for getting a reference for the wrapping div.
  innerRef: PropTypes.any
};

欢迎提供任何帮助或参考资料。。。

你好,莱昂纳多

共有1个答案

漆雕誉
2023-03-14

如果您只想使用库中的组件以及通过npm提供的包(如react grid layout),则无需在这些库中重新实现组件。您只需使用npm安装它们,并在自定义组件中使用它们。

使用ResponsiveGridLayoutsrc/lib/components/GridLayout.react.js)的组件示例:

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import RGL, {WidthProvider} from 'react-grid-layout';
import '../../../node_modules/react-grid-layout/css/styles.css';
import '../../../node_modules/react-resizable/css/styles.css';

const ResponsiveGridLayout = WidthProvider(RGL);

export default class GridLayout extends Component {
    render() {
        const {id, setProps} = this.props;
        const layout = [
            {x: 0, y: 0, w: 3, h: 3, i: 'a'},
            {x: 0, y: 1, w: 3, h: 3, i: 'b'},
        ];

        return (
            <div id={id}>
                <ResponsiveGridLayout rowHeight={30}>
                    {layout.map((item) => (
                        <div key={item.i} data-grid={item}>
                            <span>{item.i}</span>
                        </div>
                    ))}
                </ResponsiveGridLayout>
            </div>
        );
    }
}

GridLayout.defaultProps = {};

GridLayout.propTypes = {
    /**
     * The ID used to identify this component in Dash callbacks.
     */
    id: PropTypes.string,

    /**
     * Dash-assigned callback that should be called to report property changes
     * to Dash, to make them available for callbacks.
     */
    setProps: PropTypes.func,
};

要创建和编辑自定义组件,您需要按照说明设置cookiecutter dash组件样板。

但是,要重复几个要点中所述的内容,您需要:

  • 安装cookiecutterpip安装cookiecutter

安装完所有组件后,我们可以在src/lib/components目录中编辑自定义组件。当我们做了一些更改(用上面列出的代码替换了示例代码)并且我们感到满意时,我们可以运行npm run build来保持更改。

之后,您可以运行pythonusage.py,并且将运行使用自定义组件的dash应用程序。

usage.py是一个常规的Dash应用程序,它在构建过程之后导入从反应组件生成的组件,并且看起来像这样:

import grid_layout
import dash
from dash.dependencies import Input, Output
import dash_html_components as html

app = dash.Dash(__name__)

app.layout = html.Div([grid_layout.GridLayout(id="grid-layout")])


if __name__ == "__main__":
    app.run_server(debug=True)

你也可以随心所欲地编辑这个。

 类似资料:
  • 我想创建一个网格布局与响应方块。 我觉得我应该可以这样做与CSS网格布局,但有麻烦设置高度每个正方形等于宽度。 在每一个方块之间设置一条阴沟也有困难。 我用Flexbox会更好吗? 目前我的HTML看起来像这样,但将是动态的,所以更多的方块可能会被添加。当然,它需要具有响应性,因此理想情况下使用媒体查询将其折叠为一列。 使用css网格,这是我所得到的 我能够用flexbox做得更远一些,并且能够使

  • 问题内容: 我想创建一个带有响应正方形的网格布局。 我觉得我应该可以使用CSS Grid布局来做到这一点,但是在将每个正方形的高度设置为等于宽度时遇到麻烦。 在每个正方形之间设置水槽时也遇到麻烦。 使用flexbox会更好吗? 目前,我的HTML看起来像这样,但是它将是动态的,因此可以添加更多的正方形。当然,它需要响应,因此理想情况下将使用媒体查询将其折叠到一列。 使用CSS网格,据我所知 我可以

  • 除了图像数据,我们也可以提取图像的data URL,data URL实质上是一个很长的文本字符串,该字符串包含对画布图像进行编码后的信息。如果想把画布上的图像保存到本地存储或离线数据库,data URL就非常方便。本节,我们将绘制一个云状图形,获取其data URL,然后把它插入到HTML页面,以便我们能看到它是什么样子。 绘制步骤 按照以下步骤,把画布上的图画转换为data URL: 1. 定义

  • 我有调用我的后端endpoint进行身份验证的,我想简单地将true/false返回到另一个组件,这样我就可以显示/隐藏错误消息。最重要的是,成功后,我想将令牌存储在中。 这叫什么 问题是,不是真/假,但它是真的

  • 我正试图用Dash构建一个仪表板,它由一系列图块(文本)组成,如下图所示。 我试图构建一个组件来重用它,并构建下面的布局。每个框将包含标题、值和描述,如下所示。 有可用的组件吗?有人可以帮助我任何基本的想法/代码? 提前谢谢!

  • 我需要将其转换为以下格式: 类型的数量可以改变(例如,可以只有A和B)。有人能帮我吗?我使用这个组件在网站https://js.devexpress.com/demos/widgetsgallery/demo/datagrid/simplearray/angular/light/上显示数据