簡體   English   中英

如何清除Jupyter單元格中的Bokeh輸出?

[英]How to clear Bokeh output in Jupyter cell?

無論我是否用新月更新下拉列表,Bokeh都不會使用第一次創建的相同數字。 它會附加到現有的,這是我顯然不想要的行為。 我怎樣才能更新相同的數字?

def group_plot(df, col_name, xlabel=None, plot_width=None, plot_height=None, n_columns=5, truncate_label=20):

    columns_labels, columns_counts = get_agg_data(df, col_name, n_columns=n_columns, truncate_label=truncate_label)

    bdf = pd.DataFrame(list(zip(columns_labels, columns_counts)), columns=[col_name, 'Amount'])

    bar = Bar(bdf,
              values='Amount',
              label=CatAttr(columns=[col_name], sort=False), agg='sum',
              xlabel=xlabel, ylabel="Amount",
              color=color(columns=col_name, palette=get_pwc_palette()),
              plot_width=plot_width, plot_height=plot_height,
              legend=False
             )

    return bar

def show_month_report(df):
    return show(
        column(
            row(
                group_plot(df, 'Col1', xlabel='Col1', plot_width=450, plot_height=300),
                group_plot(df, 'Col2', xlabel='Col2', plot_width=450, plot_height=300),
        ),
            row(group_plot(df, 'Col3', xlabel='Col3', plot_width=900, plot_height=350)),
        ),
        notebook_handle=True
    )

months_idxs = list(set(df['Created'].dt.month.tolist()))
options = dict(zip(get_months(months_idxs), months_idxs))
default = months_idxs[0]

drop = widgets.Dropdown(
    options=options,
    value=default,
    description='Month:',
)
display(drop)

# default: show plots for the first month
handle = show_month_report(full_df.loc[(full_df['Created'].dt.month == default),])

def _on_month_change(change):
    global full_df
    show_month_report(full_df.loc[(full_df['Created'].dt.month == change['new']),])
    push_notebook(handle=handle)

drop.observe(_on_month_change, names='value')

創建一個html小部件,它將保存散景圖的html部分,並使用bokeh.embed.components將散景圖轉換為html和javascipt。

您需要注意兩點:

  1. 在調用display_javascript()你需要調用clear_output() ,否則所有的javascript代碼都將保留在單元格的輸出中。

  2. 給圖形一個修復id以防止散景在瀏覽器中保留多個圖形實例。

這是一個演示:

def call_javascript(code, delay=None):
    from IPython.display import display_javascript, clear_output    
    if delay is not None:
        code = 'window.setTimeout(function(){{{code}}}, {delay:d});'.format(code=code, delay=delay)
    display_javascript(code, raw=True)
    clear_output()

import re
from bokeh.io import show
from bokeh.charts import Line, Bar, Area, BoxPlot
from bokeh.embed import components
import pandas as pd
import numpy as np
import ipywidgets as iw

df = pd.DataFrame(np.random.randn(100, 3), columns=["A", "B", "C"])

fig_settings = dict(
    id="myfig",
    plot_height=300
)

def plot_Line():
    return Line(df.cumsum(axis=0), x="index", **fig_settings)

def plot_BoxPlot():
    return BoxPlot(pd.melt(df), label="variable", values="value", **fig_settings)

def plot_Bar():
    return Bar(pd.melt(df), label="variable", values="value", legend=None, **fig_settings)

def on_click(button):
    name = button.description
    func = globals()["plot_{}".format(name)]
    fig = func()
    fig._id = "myfig"
    script, html = components(fig)
    script = re.search(r'<script type="text/javascript">(.+?)</script>', script, flags=re.DOTALL | re.MULTILINE).group(1)
    w_html.value = html
    call_javascript(script)

buttons = [iw.Button(description=label) for label in ["Line", "BoxPlot", "Bar"]]
for button in buttons:
    button.on_click(on_click)
w_html = iw.HTML(layout=iw.Layout(height="300px"))
iw.VBox([iw.HBox(buttons), w_html])

結果如下:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM