【问题标题】:How to show output of toogle button in HMTL using ipywidgets?如何使用 ipywidgets 在 HTML 中显示切换按钮的输出?
【发布时间】:2020-03-23 15:04:09
【问题描述】:

使用 jupyter notebook,切换按钮的输出工作顺利。但是,当我将其保存为 html 时。 HTML 无法显示切换按钮的输出

下图为html格式,切换按钮无法显示输出。但在 jupyter 中它的工作正常。

我正在使用此代码。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import ipywidgets as widgets
from IPython.display import display, Markdown, clear_output, HTML
from IPython.display import display
from ipywidgets.embed import embed_minimal_html

data=[[47,15,3,5,7],[33,13,4,7,2],[25,17,9,3,5]]
df = pd.DataFrame(data, columns=['VAR_A','VAR_B','VAR_C','VAR_D','VAR_E'])

toggle = widgets.ToggleButtons(options = df.columns, description = 'Variables:', button_style = '')
out = widgets.Output()

def toggle_plot(button):
    with out:
        if button['new']:
            out.clear_output()
            plt.ioff()
            ax=plt.gca()
            sns.barplot(x=toggle.value, data=df)
            display(ax.figure)
            plt.clf() 
        else:
            out.clear_output()

toggle.observe(toggle_plot, 'value')
display(toggle)
display(out)

embed_minimal_html('try.html', views=[toggle], title='try')

我也尝试在 jupyter notebook 中使用这两个函数保存,但它保存的是空的 html。

!jupyter nbconvert --to html --no-input try.ipynb

!jupyter nbconvert --to html --no-input --template basic --output try.html try.ipynb

我仍在探索使用这些小部件,但我被困在这里。

【问题讨论】:

    标签: python html jupyter-notebook ipywidgets


    【解决方案1】:

    正如 jasongrout 解释的 here 所解释的那样,您正在尝试做的事情是不可能的,因为小部件需要一个内核才能执行。

    但是您可以查看https://www.nbinteract.com/,这可能就是您想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 2011-10-29
      • 2021-05-29
      相关资源
      最近更新 更多