是否可以在 Flask 中使用 iPython 小部件?

Is it possible to use iPython widgets in Flask?

我一直在 Jupyter notebook 中使用 iPython 小部件,但是我用来启用应用程序模式的扩展不起作用,所以我决定尝试将其全部重写为 Python/Flask,但我不知道如何在 Flask 视图中使用 iPython 小部件(尤其是交互式)?

import ipywidgets as widgets

items_layout = widgets.Layout(flex='1 1 auto',
                      width='400px', height='200px')

box_layout = widgets.Layout(display='flex',
                    flex_flow='row',
                    align_items='stretch',
                    width='100%')

instruction = widgets.Textarea(description="Command",
                               value = "",
                               layout = items_layout)

code = widgets.Textarea(description="Code",  
                        value = "",
                        layout = items_layout)

interactive_plot = widgets.interactive(run_code, trigger = instruction)

items = [interactive_plot, code]
box = widgets.Box(children=items, layout=box_layout)
display(box)

以下是来自 ipywidgets 存储库 issue 关于类似主题的回复。

In order for the widgets to do something, they must be hooked up to a kernel to do the computation. This requires some extra javascript code and a server somewhere that is providing the python kernel. The web3 example shows one way to do this.

Example 在网络服务中使用 ipywidgets。

但是您将无法在 Python 中编写小部件代码。小部件的整个显示是用 JavaScript 代码完成的。