亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Jupyter 中使用 @interact 裝飾器時實現“重置”按鈕

在 Jupyter 中使用 @interact 裝飾器時實現“重置”按鈕

喵喔喔 2023-08-15 17:06:12
我正在嘗試做一個簡單的按鈕來將小部件“重置”為某些默認值。我@interact在 Jupyter Lab 環境中使用裝飾器。問題是小部件標識符的值被復制到函數內用作浮點變量的相同標識符,因此我無法在這個新范圍內再訪問它們。這是一個簡短的示例(不起作用):import numpy as npimport matplotlib.pyplot as pltfrom ipywidgets import interact, Button@interact(starts_at=(0, np.pi*0.9, np.pi*0.1), ends_at=(np.pi, 2*np.pi, np.pi*0.1))def plot_graph(starts_at=0, ends_at=2*np.pi):        def on_button_clicked(_):        # instructions when clicking the button (this cannot work)        starts_at = 0        ends_at = 2*np.pi            button = Button(description="Reset")    button.on_click(on_button_clicked)    display(button)        f = lambda x : sum(1/a*np.sin(a*x + np.pi/a) for a in range(1,6))    x = np.linspace(0, 2*np.pi, 1000)    plt.plot(x, f(x))    plt.xlim([starts_at, ends_at])有誰知道如何將對原始小部件對象的引用發送到裝飾函數的范圍?我還將接受實現重置這些滑塊的按鈕的簡單方法。:-D編輯:更正文本流
查看完整描述

3 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

要完成此操作,您必須使用更多手動interactive_output功能。該函數允許您預先創建小部件,然后將它們傳入:


import ipywidgets as widgets

import numpy as np

import matplotlib.pyplot as plt


start_slider = widgets.FloatSlider(

? ? ? ? ? ? ? ? ? ? ? ? ? ? val = 0,

? ? ? ? ? ? ? ? ? ? ? ? ? ? min = 0,

? ? ? ? ? ? ? ? ? ? ? ? ? ? max = np.pi*0.9,

? ? ? ? ? ? ? ? ? ? ? ? ? ? step = np.pi*0.1,

? ? ? ? ? ? ? ? ? ? ? ? ? ? description = 'Starts at'

? ? ? ? ? ? ? ? ? ? ? ? ? ? )

end_slider = widgets.FloatSlider(

? ? ? ? ? ? ? ? ? ? ? ? ? ? val = np.pi,

? ? ? ? ? ? ? ? ? ? ? ? ? ? min = np.pi,

? ? ? ? ? ? ? ? ? ? ? ? ? ? max = 2*np.pi,

? ? ? ? ? ? ? ? ? ? ? ? ? ? step = np.pi*0.1,

? ? ? ? ? ? ? ? ? ? ? ? ? ? description = 'Ends at'

? ? ? ? ? ? ? ? ? ? ? ? ? ? )

def on_button_clicked(_):

? ? start_slider.value = 0

? ? end_slider.value = 2*np.pi


button = Button(description="Reset")

button.on_click(on_button_clicked)

def plot_graph(starts_at=0, ends_at=2*np.pi):

? ? f = lambda x : sum(1/a*np.sin(a*x + np.pi/a) for a in range(1,6))

? ? x = np.linspace(0, 2*np.pi, 1000)

? ? plt.plot(x, f(x))

? ? plt.xlim([starts_at, ends_at])


display(widgets.VBox([start_slider, end_slider, button]))

widgets.interactive_output(plot_graph, {'starts_at': start_slider, 'ends_at':end_slider})

然而,這將在您每次更新時完全重新生成情節,這可能會導致不穩定的體驗。因此,您還可以重寫它以使用 matplotlib 方法,就像.set_data在筆記本中使用交互式 matplotlib 后端一樣。因此,如果您要使用ipympl,您可以按照此示例筆記本中的示例進行操作。

通過另一個圖書館

我編寫了一個mpl-interactions庫,以便更輕松地使用 ipywidgets 滑塊控制 matplotlib 繪圖。ipywidgets.interact它提供了一個類似于為您創建小部件的功能,但它的優點是專注于 matplotlib,因此您需要提供的只是數據。%matplotlib ipympl

import mpl_interactions.ipyplot as iplt

import matplotlib.pyplot as plt

import numpy as np

import ipywidgets as widgets


def plot_graph(starts_at=0, ends_at=2*np.pi):

? ? x = np.linspace(starts_at, ends_at, 1000)

? ? f = lambda x : sum(1/a*np.sin(a*x + np.pi/a) for a in range(1,6))

? ? return np.array([x, f(x)]).T



fig, ax = plt.subplots()

button = widgets.Button(description = 'reset')

display(button)

controls = iplt.plot(plot_graph, starts_at = (0, np.pi), ends_at = (np.pi, 2*np.pi), xlim='auto', parametric=True)

def on_click(event):

? ? for hbox in controls.controls.values():

? ? ? ? slider = hbox.children[0]

? ? ? ? slider.value = slider.min

button.on_click(on_click)


查看完整回答
反對 回復 2023-08-15
?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

我可以在不使用裝飾器的情況下弄清楚它@interact(現在它正在工作),但我對最終結果不滿意。所以,我仍然愿意為那些可以做出清晰/更簡單的Python版本的人提供正確的答案狀態。


無論如何,這是工作代碼:


import numpy as np

import matplotlib.pyplot as plt

from ipywidgets import interact, Button, FloatSlider


def plot_graph(starts_at, ends_at):

    f = lambda x : sum(1/a*np.sin(a*x + np.pi/a) for a in range(1,6))

    x = np.linspace(0, 2*np.pi, 1000)

    plt.plot(x, f(x))

    plt.xlim([starts_at, ends_at])

    

starts_at = FloatSlider(min=0, max=np.pi*0.9, value=0, step=np.pi*0.1)

ends_at = FloatSlider(min=np.pi, max=2*np.pi, value=2*np.pi, step=np.pi*0.1)


def on_button_clicked(_):

    starts_at.value = 0

    ends_at.value = 2*np.pi


button = Button(description="Reset")

button.on_click(on_button_clicked)

display(button)

_ = interact(plot_graph, starts_at=starts_at, ends_at=ends_at)

編輯:從這一點開始的新方法================================


我選擇 @Ianhi 答案作為正確的答案,因為他指出了在我的問題背景下要考慮的問題。謝謝!


不管怎樣,我在這里發布了我正在使用的最終方案,該方案對于我的需求來說足夠簡單,并且我可以在所有交互中重復使用重置按鈕:


# Preamble ----

import numpy as np

import matplotlib.pyplot as plt

from ipywidgets import interact, Button, FloatSlider


def reset_button(defaults={}):

    def on_button_clicked(_):

        for k, v in defaults.items(): 

            k.value = v

    button = Button(description='Reset')

    button.on_click(on_button_clicked)

    display(button)


# Code ----

slider1 = FloatSlider(min=0, max=np.pi*0.9, value=0, step=np.pi*0.1)

slider2 = FloatSlider(min=np.pi, max=2*np.pi, value=2*np.pi, step=np.pi*0.1)

reset_button(defaults={slider1: 0, slider2: 2*np.pi})


@interact(starts_at=slider1, ends_at=slider2)

def plot_graph(starts_at, ends_at):

    f = lambda x : sum(1/a*np.sin(a*x + np.pi/a) for a in range(1,6))

    x = np.linspace(0, 2*np.pi, 1000)

    plt.plot(x, f(x))

    plt.xlim([starts_at, ends_at])


查看完整回答
反對 回復 2023-08-15
?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

您可以創建一個簡單的裝飾器,為每次使用添加一個重置按鈕:


按照上面的答案重置按鈕:


def reset_button(defaults={}):

? ? def on_button_clicked(_):

? ? ? ? for k, v in defaults.items():?

? ? ? ? ? ? k.value = v

? ? button = widgets.Button(description='Reset')

? ? button.on_click(on_button_clicked)

? ? display(button)

裝飾者:


def interact_plus_reset(**_widgets):

? default_vals = {wid:wid._trait_values['value'] for k, wid in _widgets.items()}

? reset_button(defaults=default_vals)

? def wrap(func):

? ? @wraps(func)

? ? @widgets.interact(**_widgets)

? ? def inner(*args, **kwargs):

? ? ? return func(*args, **kwargs)

? ? return inner

? return wrap

然后按如下方式使用它:


@interact_plus_reset(

? a = widgets.FloatSlider(min=1, max=2000, value=35, step=1),?

? b = widgets.FloatSlider(min=1, max=2000, value=1000, step=1),?

)

def run(a, b):

? print(a + b)


查看完整回答
反對 回復 2023-08-15
  • 3 回答
  • 0 關注
  • 345 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號