I am updating a jupyter notebooks with interactive widgets to a more recent version (ipywidgets 7.2.1).
It used to have interactive() functions that were executed manually by clicking on a button (feature __manual=True). However now I cannot reproduce the same behavior.
Here is a minimal example:
from ipywidgets import interactive, interact_manual
import ipywidgets as widgets
def do_sth(x):
#do sth with the argument passed
print("done "+str(x))
nb = widgets.BoundedIntText(description='Number:')
#Interaction in accordion nested in a tab
tab = widgets.Tab()
tab.set_title(0, 'Page 1')
#old method
#problem: it is not manual anymore
w = interactive(do_sth, x=nb, __manual=True)
#new solution 1
#problem: the widget appears also outside the tab/accordion
w1 = interact_manual(do_sth, x=nb)
w1.widget.children[1].description = 'do sth' #seems a bit of a hack
#new solution 2
w2 = interactive(do_sth, x=nb, manual=True) #does no pass the manual option
#if I set it manually with:
#w2.manual = True
#It generates an error (AttributeError: 'interactive' object has no attribute 'manual_button')
accordion = widgets.Accordion(children=[w, w1.widget, w2])
accordion.set_title(0, 'old interaction 0')
accordion.set_title(1, 'new interaction 1')
accordion.set_title(2, 'new interaction 2')
tab.children = [accordion]
tab
Is it possible to use solution 1 and prevent the widget from appearing twice? Otherwise, is there another way to do this?
Seems they may have moved it into a dict when adding the ability to relabel the button.
Try
w2 = interactive(do_sth, {'manual' : True, 'manual_name' : 'Do Something'}, x=nb)
Related
I am trying to do a simple GUI using Gtk with the Julia programming language, however, when I try to get the button to remove the active selection in the combobox programmatically, I get an "AssertionError".
"ERROR: AssertionError: xor(prev, current_task() !== g_stack)"
I am not sure how to get this simple example to work ?
Can anyone point me in the right direction ?
Here is my non-functional code:
using Gtk
# Create widgets------------------------------------
cb = GtkComboBoxText()
button = GtkButton("Remove Active")
# Create and Add choices to ComboBox ---------------
choices = ["zero", "one", "two", "three", "four"]
for choice in choices
push!(cb,choice)
end
# Function to get the selected choice (text) from the ComboBox
function getChoice()
i = get_gtk_property(cb, "active", Int)
return choices[i+1]
end
# Function that handles the ComboBox selection change---
function selection_changed(widget)
sel = getChoice()
println("We selected: $sel")
end
# Function to handle the button press------------------
function removeChoice(widget)
set_gtk_property!(cb,:active,-1)
end
# Connect the signals to the widgets -------------------
signal_connect(selection_changed, cb, "changed")
signal_connect(removeChoice, button, "clicked")
# Create window, and add widgets to it using Box Layout
win = GtkWindow("ComboBoxText Example",200,50)
vbox = GtkBox(:v)
push!(win, vbox)
push!(vbox, cb)
push!(vbox, button)
showall(win)
Note the warning at the end of this Gtk.jl manual page:
Warning: it is essential to avoid task switching inside Gtk callbacks, as this corrupts the Gtk C-stack. For example, use #async print or queue a message for yourself. ...
if you are still seeing segfaults in some random method due to there existing a callback that recursively calls the glib main loop (such as making a dialog box) or otherwise causes g_yield to be called, wrap the faulting code in GLib.#sigatom. This will postpone execution of that code block until it can be run on the proper stack (but will otherwise acts like normal control flow).
This is what happens here, when you try to change the selection status of the combo box from with a signal-handler callback - the "callback that recursively calls the glib main loop" as the manual page calls it.
Using either #async or Gtk.GLib.#sigatom in front of the set_gtk_property! call avoids this problem and allows the code to run.
In this case, that leads to a different error message because removeChoice itself leads to selection_change being called, and the getChoice call made there does not take into account that get_gtk_property(cb, "active", Int) could return -1. So we get a BoundsError. How you fix that depends on your use case, for demo purposes I just return nothing here in that case:
# Function to get the selected choice (text) from the ComboBox
function getChoice()
i = get_gtk_property(cb, "active", Int)
return i >= 0 ? choices[i+1] : nothing
end
# Function that handles the ComboBox selection change---
function selection_changed(widget)
sel = getChoice()
println("We selected: $sel")
end
# Function to handle the button press------------------
function removeChoice(widget)
#async set_gtk_property!(cb,:active,-1)
end
Running this, the output as I select two, then "Remove Active", then four, then "Remove Active" again in the GUI is:
julia> We selected: two
We selected: nothing
We selected: four
We selected: nothing
I have several buttons which generate images. All work perfectly in Jupyter Notebook, but when I click Voila, and click the buttons, nothing happens. The first button works, but the code is very similar to run other models, yet the other buttons do not work. Is there a work around for this issue?
Edit:
My code which does not show the output when the button is clicked:
compare = {}
button9 = widgets.Button(description = "Report All")
output = widgets.Output()
display(button9, output)
dt = DecisionTreeClassifier(random_state=42)
dt = dt.fit(X_train, y_train)
y_pred = dt.predict(X_test)
compare['Decision Trees'] = [accuracy_score(y_test, y_pred), precision_score(y_test, y_pred), recall_score(y_test, y_pred), f1_score(y_test, y_pred)]
def report_button(b):
compare = pd.DataFrame.from_dict(compare).T
compare.columns = ['Accuracy', 'Precision', 'Recall', 'F1 Score']
compare = com.sort_values('Accuracy', ascending=False)
sns.heatmap(compare, center = True, fmt='.4f', cmap='Blues', annot=True)
button9.on_click(report_button)
However this code displays the output when clicked:
button3 = widgets.Button(description="Decision Trees")
output3 = widgets.Output()
display(button3, output3)
def dt_button(b):
# Decision Trees Classifier
dt = DecisionTreeClassifier(random_state=42)
dt = dt.fit(X_train, y_train)
y_pred = dt.predict(X_test)
compare['Decision Trees'] = [accuracy_score(y_test, y_pred), precision_score(y_test, y_pred_dt), recall_score(y_test, y_pred), f1_score(y_test, y_pred)]
CM = confusion_matrix(y_test, y_pred_dt)
sns.heatmap(CM, center = True, fmt='', cmap='Blues', annot=True)
plt.title('Decision Trees Confusion Matrix')
plt.show()
button3.on_click(dt_button)
Additionally, I am having the issue of MyBinder rendering my file with Voila, but after a few minutes, the MyBinder link shows Error 404.
I'm answering your last question here (about the download button) because I need to add code blocks and comments don't allow that.
You are using Panel in your notebook to do the download and so it works. For Voila you need to stick with ipywidgets-compatible solutions as I discussed earlier. You cannot just add another dashboard extension, Panel, and expect it to work in Voila
Here makes it seem this isn't as easy as it seems.
Suggested Option:
Based on https://stackoverflow.com/a/60013735/8508004 , using SVM_Confusion_Matrix.jpg as the example.
%%html
Click to Download SVM image
Options along that line can even be coded to show up dynamically after an event in VOila, see example code use.
Not suggested but could be useful for similar:
Based on https://github.com/jupyter-widgets/ipywidgets/issues/2471#issuecomment-580965788 (this just opens the image as separate and you have to right-click and save image as)
from IPython.display import display, FileLink
local_file = FileLink('./SVM_Confusion_Matrix.jpg', result_html_prefix="Click here to download: ")
display(local_file)
I am developing an orbital analysis tool using PyQT5 and pyqtgraph!
See: https://github.com/3fon3fonov/trifon
My tool has a plotting area with ~15 plots shown in different tab windows, which show different aspects of the data analysis.
The tool it self is assembled with the Designer-qt5, while the QGraphicView widgets are promoted to pyqtgraphs's PlotWidgets
For example in the gui.py I initialize the plots like this:
def initialize_plots(self):
global p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,pe
p1 = self.graphicsView_timeseries_RV
p2 = self.graphicsView_timeseries_RV_o_c
p3 = self.graphicsView_timeseries_phot
p4 = self.graphicsView_timeseries_phot_o_c
p5 = self.graphicsView_timeseries_activity
p6 = self.graphicsView_timeseries_correlations
# ...
pe = self.graphicsView_extra_plot
# ...
so p1-p6 in this case are different PlotWidget objects on which I add Items/Plot data, i.e. p1.plot(x,y), p1.addItem(), etc.
What I want is to link pe to any of p1-p6!. pe is an extra plot so the user can choose from those already available/created.
Thus the user can select which plot he/she wants to see next to the main plot.
Lets imagine that the ComboBox dropdown menu selects between p1-p6 objects, so
pe = p1, or later: pe = p4
for example.
Is there any way this to be done with PyQtgraph?
I really tried all kind things in the last two weeks and nothing seems to work.
I am aware of the
pe.setXLink(p1)
pe.setYLink(p2)
but this only links the axes not the plot object itself. It doesn't work for me.
I implemented something like that using Docks and a DockArea. I simply added several docks stacked below each other.
They can be shown using either by clicking on the labels or by using the raiseDock() method of each dock.
You can simply add the PlotWidget (or any other Widget) to the dock using the addWidget() method of each dock.
The labels can be hidden or locked if you don't want the user to be able to move the docks at runtime.
import sys
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui
from pyqtgraph.dockarea import DockArea, Dock
class Accel_GUI():
def __init__(self, window, dock_area):
self.testing = 0
self.pen = pg.mkPen(color='y')
"""Setup the UI"""
self.window = window
self.dock_area = dock_area
self.window.setCentralWidget(self.dock_area)
self.spec_dock = []
self.spec_dock.append(Dock("Spectro 1",
size=(1200, 600),
autoOrientation=False))
self.spec_dock.append(Dock("Spectro 2",
size=(1200, 600),
autoOrientation=False))
self.dock_area.addDock(self.spec_dock[0], "top")
self.dock_area.addDock(self.spec_dock[1], "below", self.spec_dock[0])
if __name__ == "__main__":
app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication(argv)
win = QtGui.QMainWindow()
area = DockArea()
pyqtplot = Accel_GUI(win, area)
win.show()
app.exec_()
There is also an example in the pyqtgraph library that shows how to work with docks.
I can register a handler to button.on_click in ipython notebook widgets, but I don't know how to do the same for a dropdown widget
import ipywidgets as widgets
from IPython.display import display
def on_button_clicked(b):
print("Button clicked.")
button = widgets.Button(description="Click Me!")
display(button)
button.on_click(on_button_clicked)
But for
choose_task = widgets.Dropdown(
options=['Addition', 'Multiplication', 'Subtraction'],
value='Addition',
description='Task:',
)
there seems to be only
on_trait_change(...)
if I register a handler with this, can I use it to access the value of the widget?
I have seen examples with the handler and the widget belong to a subclass, and the handler can use self to introspect. But if I don't want to use a subclass, how does the handler know what widget was the target of the event.?
Between this link and the traitlet docs on github and just playing around, I finally figured this out:
w = widgets.Dropdown(
options=['Addition', 'Multiplication', 'Subtraction', 'Division'],
value='Addition',
description='Task:',
)
def on_change(change):
if change['type'] == 'change' and change['name'] == 'value':
print("changed to %s" % change['new'])
w.observe(on_change)
display(w)
Overall this looks a lot richer than the deprecated interface, but it could definitely use more examples.
You can specify the change name in observe. This makes for cleaner code, and the handler is not called for changes you don't need:
from IPython.display import display
from ipywidgets import Dropdown
def dropdown_eventhandler(change):
print(change.new)
option_list = (1, 2, 3)
dropdown = Dropdown(description="Choose one:", options=option_list)
dropdown.observe(dropdown_eventhandler, names='value')
display(dropdown)
Put it all together
Inspired on previous answers and lambda expressions I use this:
def function(option):
print(option)
w = widgets.Dropdown(
options=['None', 'Option 1', 'Option 2', 'Option 3'],
description='Option:',
disabled=False
)
w.observe(
lambda c: plot_content(c['new']) if (c['type'] == 'change' and c['name'] == 'value') else None
)
display(w)
I agree that event handling is not as thorough as would be desired: I have been filtering the events as you receive multiple events for a typical dropdown change as the index changes, the value changes, i.e., change['name'].
I am doing the following:
def on_dropdown_change(change):
if change['name'] == 'value' and (change['new'] != change['old']):
print('do something with the change')
dropdown = ipywidgets.Dropdown({options=['one','two','three'],
value='one'})
dropdown.observe(on_dropdown_change)
I believe the idea is to use trait name, e.g. value. For example:
from ipywidgets import Dropdown
def handle_change():
print type_sel.value
type_sel = Dropdown(description="Keypoint type", options=['surf', 'orb'])
type_sel.on_trait_change(handle_change, name="value")
display(type_sel)
SciPy 2015 Advanced Jupyter Video Tutorial
I had the same issue. This also begs the next question, how to interface button actions based on dropdown menu selections.
# Common Imports for Widgets
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
'''
Precusor:
<class 'traitlets.utils.bunch.Bunch'> It is a dictionary-like object containing:
{'name': 'value', 'old': 'what_ever_the_old_value_was', 'new': 'what_ever_the_new_value_is',
'owner': Dropdown(description='the_user_defined_label:', index=1, # I'm not sure what this is
options=()#list of options passed,
value='value_kwarg_value'), 'type': 'change'} # type: action_or_event type
For more information see:
https://traitlets.readthedocs.io/en/stable/using_traitlets.html#default-values-and-checking-type-and-value
or
https://github.com/jupyter-widgets/tutorial/blob/master/notebooks/08.00-Widget_Events.ipynb
or a long but well done SciPy talk on the use of widgets #
https://www.youtube.com/watch?v=HaSpqsKaRbo
'''
foo = ['a','b','c'] # List to use
# Function to apply to drop box object
def bar(x):
'''
I am intentionally passing what it is made of so you can see the output.
'''
print(x,'\n') # Whole object
print(x.new,'\n') # New value
# Function for the button to select user input and do work
def get_user_selection(a): # A default arg is needed here, I am guessing to pass self
# Displays the current value of dropbox1 and dropbox two
display(dropbox1.value,dropbox2.value)
# creation of a widget dropdown object called dropbox1
dropbox1 = widgets.Dropdown(
options=foo, # Object to iterate over
description='Letter:', # User defined
value=foo[1], # Default value selection
rows=len(foo), # The number of rows to display when showing the box
interactive=True, # This makes the box interactive, I believe this is true by default
);
# Drop box of k,v like pairs
dropbox2 = widgets.Dropdown(
options=[('One', 1), ('Two', 2), ('Three', 3)],
value=2,
description='Number:',
)
# Button to click
select_button = widgets.Button(
description='Click', # User defined
disabled=False
)
# Event Handlers
dropbox1.observe(bar,names='value')
dropbox2.observe(bar,names='value')
select_button.on_click(get_user_selection)
# I you need more help with commands try things like:
# interact_manual?
# display(arg.keys,arg.traits)
# print(widgets.widget_type_here.widget_function_or_attr.__doc__)
# Create a UI object to display things. There are other ways of organizing them.
ui = widgets.HBox([dropbox1,dropbox2,select_button]) # pass an array of widgets to the ui
# display the UI
display(ui)
This will display the following after a couple of clicks.
I pretty new in this whole Python thing and my question is how to make, that a button runs the command, when clicking it and not before.
I searched much in the Internet but i didnt find anything.
I dont understand the classes at all. Is there no other way to do this?
Here is my work, i did on the programm.
Thanks for your help
from tkinter import *
import os
t = ""
def ordner(x):
print ("def")
if os.path.exists(os.path.join("/Kunden/",x)) == True:
pass
else:
os.mkdir(os.path.join("/Kunden/",x))
def E1holen():
x = E1.get()
ordner(x)
#Hauptfenster
main=Tk(className='Kundendatenbank')
main.iconbitmap('icon.ico')
#Inhalt Hauptfenster
L1 = Label(main, text="Kundenname:")
L1.pack(side = LEFT)
E1 = Entry(main, bd =5, textvariable=t)
E1.pack(side = RIGHT)
a = Button (main, text=("erstellen/bearbeiten"), command=E1holen()).pack()
main.mainloop()
It runs immediately ecause you tell it to.
What is the syntax for calling a function in Python? It's foo(), right? So, when you do command=E1holen(), what should python do? It should call E1holen(), and then pass the result to the command attribute.
Put another way, the command attribute takes a reference to a function, but because of the () you were calling the function and giving the command attribute whatever that function returned. The solution? Remove the ():
a = Button(..., command=E1holen)