将响应式网格布局转换为 Plotly Dash

2024-05-10

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

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

由于我没有使用 React.js 的经验,我很困惑应该做什么修改才能将此组件转换为 Plotly Dash。

对于上面的组件,我应该只在 Proptypes 上声明 Properties(如下所示)还是需要做更多修改?

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
};

非常欢迎任何帮助或参考...

问候, 列奥纳多


实现自定义组件

如果您只想使用库中的组件以及可通过以下方式获得的包npm (like react-grid-layout),您不需要重新实现这些库中的组件。你可以简单地安装它们npm并在您的自定义组件中使用它们。

示例组件使用ResponsiveGridLayout (src/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 仪表板组件样板 https://dash.plotly.com/react-for-python-developers.

但要重申几个要点中的内容,您需要:

  • Install cookiecutter: pip install cookiecutter
  • 运行cookiecutterhttps://github.com/plotly/dash-component-boilerplate.git。这将生成您可以创建自定义组件的环境。
  • 填写名称后,您希望自定义组件将目录更改为根据您提供的名称生成的目录。我已选好名字grid_layout,如果您在提示后选择不同的值,您的结构将会不同cookiecutter.
  • 此时,您需要通过运行来安装所需的 python 依赖项pip install -r requirements.txt。您现在还可以安装react-grid-layout using npm i react-grid-layout.

基本用法

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

之后你可以运行python usage.py并且使用自定义组件的仪表板应用程序将运行。

usage.py是一个常规的Dash导入从生成的组件的应用程序react构建过程之后的组件可能看起来像这样:

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)

您也可以根据需要对其进行编辑。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将响应式网格布局转换为 Plotly Dash 的相关文章

随机推荐

  • 我应该害怕使用 UDP 进行客户端/服务器广播通话吗?

    我在过去的两天里阅读了每一篇StackOverflow问题和答案 以及googling当然 关于印地TCP and UDP协议 以便决定在我的用户应用程序和 Windows 服务之间的通信方法中应该使用哪一种 从我目前所看到的来看 UDP是
  • 为什么 [].push([]) 返回 1? [复制]

    这个问题在这里已经有答案了 为什么这会返回 1 push outputs 1 push 返回数组的新长度 one push two returns 2 array length is 2 one two push something ret
  • 将活动工作表作为电子邮件附件从 Google 工作表发送

    我有一个谷歌表单 可以捕获电子表格中的响应 目前 每次做出新响应时 它都会创建一个新工作表 我现在尝试将 邮寄活动工作表脚本 添加到创建新工作表的现有脚本中 但是我收到错误 请求失败https docs google com spreads
  • 什么是 C++11 扩展 [-Wc++11-extensions]

    我需要一些帮助来了解此错误发生的位置 警告 非静态数据成员的类内初始化是 C 11 扩展 Wc 11 extensions 这是它来自的代码部分 typedef struct Hand bool straight false bool fl
  • /WEB-INF 中的 JSP 返回“HTTP 状态 404 请求的资源不可用”

    我创建了一个 JSP 文件 sample jsp This is jsp program 我把它放在这里samplejsp项目 samplejsp WebContent WEB INF sample jsp 我通过以下网址打开了它 http
  • Java泛型类型

    当我有一个界面时 public interface Foo
  • RemoveEventListener 在 Firefox 版本 58 中不起作用

    但它在 Chrome 中有效 这是我的 UI EventBus 代码 原型 addEventListener方法是一样的 只不过remove换成了add UI EventBus removeEventListener function ob
  • 在javascript中我们如何识别一个对象是Hash还是Array?

    我的 JSON 调用的输出可以是数组或哈希 我如何区分这两者 现代浏览器支持Array isArray obj method See MDN https developer mozilla org en US docs Web JavaSc
  • scrapy python 请求未定义

    我在这里找到了答案 code for site in sites Link site xpath a href extract CompleteLink urlparse urljoin response url Link yield Re
  • 转换 Java -> Grails ... 如何加载这些属性?

    我正在将 Java Web 应用程序转换为 Grails 1 2 1 在我的 Java 应用程序中 我有一个从 properties 文件加载属性的单例 我已经看到我可以将其加载到 Config groovy conf 文件中 如果我的属性
  • 关闭应用程序后如何调试

    我正在尝试重现问题 这需要在特定位置关闭并重新打开我的应用程序 这是我的问题 1 如何查看我的日志 使用NSLog命令 当我的 iPhone 未连接到 XCode 时 2 是否可以将iPhone模拟器的特定位置 例如市中心 设置为默认位置
  • 是否可以在 Apple M1 计算机上安装 Weblogic 12.2.1.4?

    我一直在寻找有关这方面的信息 但没有找到任何信息 我认为 Oracle 站点上没有可用于在 Apple M1 设备上安装 Weblogic 12 2 1 X 的版本 但也许可以使用 Rosetta 2 来完成此操作 有人尝试过吗 我不能 因
  • 如何访问 gem5 线程统计信息?

    我希望在我的一些工作中使用 gem5 并且对其功能有一个非常普遍的问题 我的问题是 使用 gem5 我可以获得有关单个线程的行为 系统资源使用情况的统计信息 无论是 SE 还是 FS 模式 例如 如果我的应用程序中运行 2 个线程 我是否可
  • 为什么将未使用的返回值转换为 void?

    int fn void whatever void fn 是否有任何理由将未使用的返回值强制转换为 void 或者我认为这完全是浪费时间 David s answer https stackoverflow com questions 68
  • Tkinter 绑定 Mac OS“command+q”

    当我按 Command q 时 我试图 停止 根窗口退出 但这是不可能的 其他快捷键在我的 Mac 操作系统上有效 即使在 Windows Linux 中 Alt F4 绑定也可以 捕获 但在 Mac 操作系统中对我来说是不可能的 有任何想
  • Python“self”关键字[重复]

    这个问题在这里已经有答案了 我是 Python 新手 通常使用 C 最近几天开始使用它 在类中 是否需要在对该类的数据成员和方法的任何调用前添加前缀 因此 如果我在该类中调用方法或从该类获取值 我需要使用self method or sel
  • 监控 Java 应用程序上的锁争用

    我正在尝试创建一个小基准 在 Groovy 中 以显示几个同步方法上的高线程争用 当监控自愿上下文切换时 应该会出现高争用 在 Linux 中 这可以通过 pidstat 来实现 程序如下 class Res private int n s
  • 如何在RcppParallel中调用用户定义的函数?

    受到文章的启发http gallery rcpp org articles parallel distance matrix http gallery rcpp org articles parallel distance matrix 我
  • Melt() 函数复制数据集

    我有一个这样的表 id name doggo floofer puppo pupper 1 rowa NaN NaN NaN NaN 2 ray NaN NaN NaN NaN 3 emma NaN NaN NaN pupper 4 sop
  • 将响应式网格布局转换为 Plotly Dash

    我是一个非常活跃的 Dash 用户 我开始发现 Dash 使用中存在很多限制 并且我意识到有关如何将组件转换为 Dash 的信息 内容绝对有限 并且示例过时且非常简单 并且我对 Javascript 或 React 几乎没有任何了解 我完全