Note: From the wording of the question the focus seems to be on having a canvas grid with clickable cells that can be toggled on and of in Dash, so I will address this in my answer. I won't go into Conway's Game of Life here, but this answer could serve as a starting point if you wanted to implement it or a variation of it.
^{} 模块允许我们访问^{} 组件
我们可以做的是创建一个运行一次的clientside callback,通过它我们可以使用Javascript访问DOM,这样我们就可以基于由
Canvas
Dash组件呈现的canvas
元素创建网格,并将click事件侦听器附加到它Dash应用程序可能看起来像这样:
然后,您可以在
assets
目录中添加一个.js
文件,其中包含以下内容:有关在Dash应用程序中包含Javascript的更多信息,请参阅文档here
因此,上面代码的思想是使用嵌套循环来创建网格。我们根据当前行数、列数以及为所有单元格设置的宽度和高度绘制矩形
除了绘制网格外,还保存了所有网格坐标,以便我们可以确定用户单击哪个单元格时,该单元格是否应填充或清除。为了知道是否应该清除或填充单元格,为所有xy坐标对设置了
filledIn
属性结果:
相关问题 更多 >
编程相关推荐