I'm new to Jupyterlab and ipywidget (and might be missing some fundamentals).
This seems to be simple, but I searched everywhere and could not find a way to do this.
I just need to close widget A and display widget B on button click event (of widget A).
This is what I have tried/need in simple terms.
import ipywidgets
from IPython.display import display
buttonA_widget = ipywidgets.Button(description='Button A')
buttonB_widget = ipywidgets.Button(description='Button B')
display(buttonA_widget)
def switchMode(x):
buttonA_widget.close()
display(buttonB_widget)
buttonA_widget.on_click(switchMode)
When I tried above, buttonA get disappear, but second display call does not get executed
and I don't get any error messages (trying this on Jetson Nano).
Thanks in advance...
You can wrap the button in an Output widget and clear it when required:
import ipywidgets
from IPython.display import display
buttonA_widget = ipywidgets.Button(description='Button A')
buttonB_widget = ipywidgets.Button(description='Button B')
out = ipywidgets.Output()
with out:
display(buttonA_widget)
def switchMode(x):
out.clear_output()
with out:
display(buttonB_widget)
buttonA_widget.on_click(switchMode)
out
Related
I'm just starting to learn python and I'm having problem with displaying two widget at the same time. I was trying to create a Text area where I will put a string then submit this string.
This string will be use in SentimentIntensityAnalyzer to check the sentiment.
My goal now is to display the two widget using ipywidgets below is my code
from ipywidgets import Textarea, Button
textarea = Textarea(
value='Sample value',
disabled=False
)
button = Button(
description='Submit Sentiment',
disabled=False,
button_style='',
tooltip='Submit',
icon='paper-plane'
)
textarea
button
When I run the code only the button is displaying, so I tried to comment out for the meantime the button variable and as expected the textarea variable shows.
I tried different approach like concatenation and using IPython.display as well but it display errors instead.
I'm using Jupyter Notebook to run this. Thank you in advance for the help.
I used IPython.display again, my wrong way is that I put the both variables inside the display()
Here's my code
from ipywidgets import Textarea, Button
from IPython.display import display
tx = Textarea(value='test',disabled=False)
btn = Button( description='Submit',disabled=False,button_style='',
tooltip='Submit',icon='paper-plane')
display(tx)
display(btn)
Finally both widget display.
In a published Jupyter notebook is there a way to insert a widget that says "running" or something similar when I am running a function.
I am aware of the tqdm function but to the best of my knowledge this is only when the function / process contains a for-loop.
I currently have a series of dropdown widgets with a submit button but some of the functions take a while for the calcs to run so i have no way of telling if the're running or not
Cheers
The way I have done this in the past is to have a function as a context manager, that displays some value to a Text widget to indicate that the function is running. You could also use an Output widget to display a indeterminate 'progress' bar like the one below:
https://www.iselect.com.au/content/themes/iselect/images/post/loader.gif
import ipywidgets as ipyw
import time
from contextlib import contextmanager
label = ipyw.Text('Ready')
button = ipyw.Button(description='Click me')
#contextmanager
def show_loading():
label.value = 'Running...'
yield
label.value = 'Ready'
def long_running_function(self):
with show_loading():
time.sleep(2)
button.on_click(long_running_function)
display(button)
display(label)
I am writting a jupyter notebook in which at the begining I am creating a complex tab ipywidget for the user to select some inptus (see picture). I am wondering if there is any way to hide to the user the code cell that contains the code to create that widget.
I saw on this forum some questiosn about hiding code when exporting the notebook but in my case the user will access the j_notebook online. Just would like to avoid complexity by hiding some code cells
in module.py
import ipywidgets as ipyw
from IPython.display import display
button = ipyw.Button('Try this:')
out = ipyw.Output()
def print_it(button):
with out:
print('You clicked it')
button.on_click(print_it)
display(ipyw.VBox(children=[button, out]))
In your notebook:
import module
How do I link the value of two buttons to be opposite of one another? The widgets.jslink() function only seems to link the value to be the same, not the opposite. I know that I could use widgets.ToggleButtons() to link two buttons, but I want the success button to be green and the fail button to be red. ToggleButtons() does not appear to allow different coloring for each button. If it does, I'm open to that as a solution, as well. Here is the code I have so far (FYI: I'm running this code in JupyterLab using ipywidgets and node.js):
button_y= widgets.Button(
description='Success',
disabled=False,
button_style='success'
tooltip='Click me',
icon='check'
)
button_n= widgets.Button(
description='Failure',
disabled=False,
button_style='danger'
tooltip='Click me',
icon='check'
)
display(widgets.HBox((button_y, button_n)))
Output:
Guessing that you want to link the disabled attributes.
You can use the observe method in the backend to add logic to links:
def toggle_button_n(value):
button_n.disabled = not value.new
def toggle_button_y(value):
button_y.disabled = not value.new
button_n.observe(toggle_button_y, names=['disabled'])
button_y.observe(toggle_button_n, names=['disabled'])
You also need to change the initialisation so that only one of the buttons is enabled.
I have a text box in Jupyter likes the following:
import ipywidgets as widgets
from IPython.display import display, clear_outpu
tagInput = widgets.Text()
And try to get keyboard events like Enter using the following code:
tagInput.observe(handle_process_text_submit,names='value')
But, it catches just the value changes. what supposed to do to solve the issue?
If You want to catch the keyboard Enter specifically, you can use on_submit event such as the following:
def on_submit_func(sender):
print "enter"
tagInput.on_submit(on_submit_func)