有没有办法控制ipywidgets的放置和对齐(在jupyter笔记本内)?
from ipywidgets import widgets
from IPython.display import Javascript, display
event_type_ui = widgets.Text(value='open', description='Test Event')
platform_ui = widgets.Text(value='Android', description='Control Event')
display(event_type_ui)
display(platform_ui)
Run Code Online (Sandbox Code Playgroud)
我想指定一个偏移量(以像素为单位?)以允许标签适合并使两个控件垂直对齐.
我正在尝试“widgetize”我的 IPython 笔记本,但遇到了事件和从函数返回值的问题。这是我认为最好的工作流程:
我首先尝试使用“interact”方法来调用函数,但这似乎很难关联事件和返回值。从阅读其他交互式示例来看,创建一个类似乎是要走的路。我不经常写课;所以希望我的错误很简单。
下面制作了两个小部件,当用户按下“Enter”键时应该调用一个函数并将其返回值存储在类中以备将来使用。
实际上,它在我输入任何文本之前两次触发该函数,并在我更改值时抛出“unicode object is not callable”。
import ipywidgets as widgets
from IPython.display import display
def any_function_returning_value(word1,word2):
new_word = 'Combining words is easy: %s %s'%(word1,word2)
print new_word
return new_word
class learn_classes_and_widgets():
def __init__(self, param1 = 'a word', param2 = 'another word'):
self.p1_text = widgets.Text(description = 'Word #1',value = param1)
self.p2_text = widgets.Text(description = 'Word #2',value = param2)
self.p1_text.on_submit(self.handle_submit())
self.p2_text.on_submit(self.handle_submit())
display(self.p1_text, self.p2_text)
def handle_submit(self):
print "Submitting"
self.w = any_function_returning_value(self.p1_text.value,self.p2_text.value)
return self.w
f = learn_classes_and_widgets(param1 = …Run Code Online (Sandbox Code Playgroud) 出于某种原因,我希望我的 HTML 小部件具有固定的高度,无论小部件中有多少行。如果行太多而无法适应高度,理想情况下可以滚动查看所有行。我尝试了类似以下的方法,但它不起作用:
import ipywidgets as widgets
widgets.HTML(
value="Hello <p>World</p><p>World</p><p>World</p><p>World</p><p>World</p><p>World</p><p>World</p><p>World</p>",
placeholder='Some HTML',
description='Some HTML',
disabled=True,
height='50px',
overflow_y='scroll'
)
Run Code Online (Sandbox Code Playgroud) 我正在使用 Jupyter 并尝试使我的绘图具有交互性。
所以我有一个阴谋。我有一个 ipywidgets 按钮。
单击按钮时,我需要更新绘图,就像与滑块进行交互一样。
但我不能。
它仅在 matplotlib 使用“笔记本”后端时才有效,但看起来很糟糕。同时与任何类型的情节进行交互。有没有办法重现这一点,而不是使用交互?
#this works fine! But toolbar near the graph is terible
#%matplotlib notebook
#this does not work
%matplotlib inline
from matplotlib.pyplot import *
button = ipywidgets.Button(description="Button")
def on_button_clicked(b):
ax.plot([1,2],[2,1])
button.on_click(on_button_clicked)
display(button)
ax = gca()
ax.plot([1,2],[1,2])
show()
Run Code Online (Sandbox Code Playgroud) 在 Jupyter notebook 中使用 Python 3.7,下面的代码显示了一个文本输入字段,它触发 handle_submit 函数以打印出一些输出。在这个例子中,输入 40 次重复。
from ipywidgets import widgets
from IPython.display import display
text = widgets.Text()
display(text)
def handle_submit(sender):
print('\n'.join([text.value] * 40 ))
text.on_submit(handle_submit)
Run Code Online (Sandbox Code Playgroud)
运行此代码会显示一个文本框。
如果您在框中输入文本并按 Enter,将运行 handle_submit 函数并打印“结果”。
这可以多次使用,但保留所有旧输出。因此,在多次使用输入字段后,您需要无休止地滚动以获得新结果。
在从 handle_submit 函数打印新输出之前,是否有清除单元格输出的命令?与此示例不同,输出长度不固定,因此解决方案应处理不同大小的输出。
Ipywidget 手风琴在第一次执行笔记本时默认展开。如何让手风琴默认折叠?
提前致谢。
我想使用 ipywidgets 水平显示单选按钮。
radio_input1 = widgets.RadioButtons(options=['选项 1', '选项 2'])
但它垂直显示单选按钮:
我尝试在 HBox 中放置只有一个选项的单选按钮,并向每个单选按钮添加观察者事件,然后从观察者方法中取消选择选定的单选按钮,但在此之前取消观察事件,然后重新注册观察事件。不知怎的,它调用了3次:
output_radio_selected = widgets.Text() # Used to take the user input and access it when needed
radio_input1 = widgets.RadioButtons(options=['Option 1', 'Option 2']) # Declare the set of radio buttons and provide options
radio_input2 = widgets.RadioButtons(options=['Option 3', 'Option 4'])
def bind_selected_to_output(sender): # Connect the input from the user to the output so we can access it
#radio_input1.unobserve(bind_selected_to_output)
radio_input1.unobserve_all()
radio_input1.index=0
#print(sender)
global selected_option # Global variable to hold the user …Run Code Online (Sandbox Code Playgroud) 我一直在努力使用 ipywidgets.FileUpload() 将图像上传到我的 Jupyter 笔记本中,它对于文本文件可以正常工作,但对于二进制文件,内容总是损坏。特别是对于图像,它们始终存储为“数据”,因此 keras.preprocessing.image.load_img() 无法使用它们。我正在使用的代码是:
import ipywidgets as widgets
uploader = widgets.FileUpload()
uploader
for name, file_info in uploader.value.items():
with open(name, 'wb') as fp:
fp.write(file_info['content'])
Run Code Online (Sandbox Code Playgroud)
我尝试了多种解决方案,但没有任何方法可以处理二进制文件,任何提示或帮助都会受到好评。我的环境是GCP AI Platform Notebooks(JupyterLabs 1.2.16,ipywidgets 7.5.1),我一直使用的参考文献是:
我有一个发展问题。我需要能够使用 ipython-widgets 从 python 的下拉菜单中同时选择多个项目。到目前为止,我已经能够在单选项小部件菜单中选择一个选项,选择后将绘制其相应的统计数据。我已将我的代码粘贴在下面,如果您能帮助我,我将不胜感激。
import numpy as np
import pandas as pd
import ipywidgets as widgets
import matplotlib.pyplot as plt
import panel as pn
pn.extension()
classes= widgets.Dropdown(
description='Products:',
options= list(output_table.class_m.unique())
)
start_year = widgets.BoundedFloatText(
value=output_table.year.min(),
min=output_table.year.min(),
max=output_table.year.max(),
step=1,
description='Start Year:',
disabled=False,
color='black'
)
end_year = widgets.BoundedFloatText(
value=output_table.year.max(),
min=output_table.year.min(),
max=output_table.year.max(),
step=1,
description='End Year:',
disabled=False,
color='black'
)
output=widgets.Output()
def response(name, start, end):
name = classes.value
output.clear_output()
df2 = output_table.copy()
# Filter between min and max years (inclusive)
df2 = df2[(df2.year >= …Run Code Online (Sandbox Code Playgroud)