I can't display both widget in Python - python

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.

Related

Modify ipywidgets button text color

I'm trying to change the text color of a button widget in a jupyter notebook. According to the ipywidgets documentation the button widget has the following style attributes:
['_model_module',
'_model_module_version',
'_model_name',
'_view_count',
'_view_module',
'_view_module_version',
'_view_name',
'button_color',
'font_family',
'font_size',
'font_style',
'font_variant',
'font_weight',
'text_color',
'text_decoration']
https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Styling.html#The-style-attribute
However when I execute the following code in a jupyter notebook get much less attributes:
That means that I cannot modify parameters like the text color or size...
Any idea of what could be happening so that I get so few attributes compared to the documentation? I have the last version of Ipywidgets installed.

JupyterLab ipywidget close one widget and display another on button click

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

How to hide ipywidget code in Jupyter Notebook

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 to link opposite values of two buttons using ipywidgets in Python 3?

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.

Get Keyboard Events on Jupyter Widgets

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)

Categories

Resources