Is there a way to hide a displayed object using IPython? - python

I am using the IPython module in a Jupyter Notebook.
I am using the display module to display buttons.
from ipywidgets import widgets
import IPython.display as dsply
def click_reset(b):
print("reset domains button")
restoreDomains()
resetButton = widgets.Button(description="Reset Domains")
resetButton.on_click(click_reset)
dsply.display(resetButton)
This works fine, but I am trying to find a way to programatically hide certain buttons. Based off the execution of my other code, I want certain buttons to be removed from the UI. Is there anything like hide(resetButton) that I can use?

You can hide a widget using
resetButton.layout.visibility = 'hidden'
to let the widget still consume space, or
resetButton.layout.display = 'none'
to let the widget not consume space anymore.
The top-level attribute resetButton.visible = False is not longer supported.

When I use #SergeyGornostaev's answer, I still have a residual cross showing up on the left side of the cell output. I found the following command removes the widget all together:
resetButton.close()

You can hide every widget by setting it's property visible to False
resetButton.visible = False

Related

Jupyter Lab - Use input() while interacting with a ipywidget and for it to execute observe method and thus update the widget

In jupyterlab, I'm using a ipywidget consisting of nested Tab widgets with a HTML widget for each leaf Tab children. I was wondering is it possible to interact with this ipywidget while waiting for input using input() . I'm loading strings into the HTML widget when Tab.selected_index changes using observe() because jupyterlab complains about too many operations or something like that but because of input blocking nothing is happening.
So in pseudocode
I create the widget Tab_5 a Tab Widget which for each Tab has another Tab Widget associated with it. Eg Queries have associated tabs 1-10, 11-20 are top level tabs and Tabs 1,2,3,4,5,6,7,8,9,10 are nested under Tab 1 - 10 etc
create def on_value_change(change)
if len(Tab_5.children[Tab_5.selected_index].children[Tab_5.children[Tab_5.selected_index].selected_index].value) == 0:
index = 10 * Tab_5.selected_index + Tab_5.children[Tab_5.selected_index].selected_index
Tab_5.children[Tab_5.selected_index].children[Tab_5.children[Tab_5.selected_index].selected_index].value = "<div style ='line-height: normal;'>"+ descList[index] + "<br><br>"+ str(queryStrings[index]) + "<br><br>" + str(responses[index]) + "</div>"
assign it to the children widgets using Tab_5.children[i].observe(on_value_change, names='selected_index'), Tab_5.observe(on_value_change, names='selected_index')
So I looked at https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Asynchronous.html and didn't really understand it and tried doing stuff with the last example.
Basically I'm running some queries using an API and I want to see my past queries in the tabbed interface before choosing to continuing on with future queries with the same parameters apart from one which is incremented. But at the moment, the tabbed widget is not updating while waiting for input.
So previously I generated some Markdown as output but it become really slow. I also at one stage used
from IPython.core.getipython import get_ipython
shell = get_ipython()
payload = dict(
source='set_next_input',
text=contents,
replace=False,
)
shell.payload_manager.write_payload(payload, single = False)
create_new_cell("".join(output))
which created a new code cell which needed to be manually turned into a Markdown cell. (You can programmatically create a cell in Jupyter Notebook but not Jupyter Lab)
So do I need to do something with threads or asyncio? Is this possible or can I accomplish this in a better way by having a widget for the input?
Any suggestions?

How to set QAction icon text [duplicate]

I am learning PyQt by playing with examples. In this case, I'm playing with the webbrowser example that is located at \Python26\Lib\site-packages\PyQt4\examples\activeqt\webbrowser .
The demo does something really odd if you add one line to set the icon text property of a QAction.
Here's a code sample of the change I tried:
self.actionBack = QtGui.QAction(MainWindow)
self.actionBack.setIcon(QtGui.QIcon(":/icons/image1.xpm"))
self.actionBack.setObjectName("actionBack")
# added this line:
self.actionBack.setIconText("Back")
One time I tried it, and the entire toolbar went blank. I can't reproduce that, now I have no effect from that one line change.
What I'm trying to figure out is what to do to the QAction so that the toolbar has text on the button beside the image, or can it be done at all like this? Is there some other way to make the toolbar have some text plus an icon?
Try setting the button style on the toolbar that the actions are being added to:
self.tbNavigate.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
...
self.actionBack.setIconText("Back")

Accessing Jupyter notebook widgets values

While trying to use the Jupyter widgets to control a different function I am stumbling over this issue and am searching for a workaround.
The widget is displayed and I can interact with it just fine. Reading the value, as stated in the documentation using w.value also works. But now reading the value continuously in a loop does not work. Whereas I uncheck the box the value in the while loop never changes.
How can I control python functions via the widgets if they are not updated with the current state of the widgets?
Hope this helps. My reference is this link.
from IPython.display import display
import ipywidgets as widgets
def f(x):
return 'value of checkbox is: ' + str(x)
w = widgets.Checkbox(value=True, description='Updated?')
interact(f,x=w)

How to get (childless) “tabs” in a pygtk application

I am facing the problem to need tabs in a pygtk app. Pretty much just like gedit has, but without any per-child widget content.
I’ve come across gtk.Notebook, but that requires me to put a widget for each tab, which I don't want.
The reason is, that I have one widget, but would only like to updates its content based on which tab is selected.
Any hints on how to do that?
My idea so far would be to just add some invisible widget for each tab and then connect to the select-page signal. Which widget could I use as invisible widget, or is there a better/alternative way of achieving my goal?
The invisble widget idea works. But not with gtk.Invisible (this just crashes), but with gtk.HBox() or any other thing that seems empty.
self.notebook.append_page(gtk.HBox(), gtk.Label("title"))
Now if I want to display stuff inside the tab actually, I can use reparent to move the widget to the current tab like this.
class Tab(gtk.HBox):
def __init__(self, child):
self.child = child
self.notebook.append_page(Tab(myWidget), gtk.Label("title"))
def pageSelected(self, notebook, page, pagenum):
box = notebook.get_nth_page(pagenum)
box.child.reparent(box)
You can have global widgets, one per tab as you want, in order to access them easily when the tab is selected.
self.notebook.append_page(self.rightBox, gtk.Label("Orders"))
Then connect to the "switch page" signal
self.notebook.connect("switch-page", self.pageSelected)
and :
def pageSelected(self, notebook, page, pagenum):
name = notebook.get_tab_label(notebook.get_nth_page(pagenum))
Now you have "name" with the label of the currently selected page. Just test it (if name == "Orders" ...) to interact.
Hope this was of some help !

Can I make a custom pygtk tooltip stay on when the cursor is over it?

Please look at the following snippet :
import gtk
def callback(widget, x, y, keyboard_mode, tooltip):
hbox = gtk.HBox(False, 8)
button = gtk.Button('Exit Tooltip')
label = gtk.Label('Tooltip text')
hbox.pack_start(label)
hbox.pack_start(button)
hbox.show_all()
tooltip.set_custom(hbox)
return True
label = gtk.Label('Test label')
label.set_has_tooltip(True)
label.connect('query-tooltip', callback)
Here I've created a custom tooltip with a close button in it. Now I want it to stay until i click that close button. Searching google was not that helpful. besides I would also like to know the signals/events that are emitted when a tooltip is being closed.
Similar problems are handled smoothly for JQuery/JavaScript/Ajax tooltips etc. but
for gtk/pygtk there is no luck :(
Thanks in advance ...
I had this issue as well, and as far as I know, there isn't any way to determine how long a tooltip stays up.
What I did (and recommend to you) is that you make your own "tooltip" and set it's background color to yellow, or whatever color you want, via an eventbox. Make sure you don't show it yet. This is just a simplified code, as you will need to position and size this in your project yourself.
color = gtk.gdk.rgb_get_colormap().alloc_color('black')
ebTooltip = gtk.EventBox()
btnTooltip = gtk.Button("Close")
ebTooltip.add(btnTooltip)
ebTooltip.modify_bg(gtk.STATE_NORMAL, color)
Now, you just need to hide and show this eventbox via your callbacks. To show it, call...
ebTooltip.show()
And, to hide it (probably on the "clicked" event of your close button)...
ebTooltip.hide()
Hope that solves your issue!

Categories