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)
Related
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
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
In my Jupyter notebook, I want to execute some long running background jobs and display their status in a cell. I thought, before doing something difficult, I could start with a clock.
I have the following code in one cell
import ipywidgets as widgets
import time
from datetime import datetime as dt
from IPython.display import HTML
from IPython.display import display
import threading
out = widgets.Output()
#out.capture(clear_output=True, wait=True)
def display_time():
now = dt.now().strftime("%H:%M:%S")
display(HTML(f"""<h1><center>{now}</center></h1>"""))
def run_time():
while True:
time.sleep(1)
display_time()
out
When I call run_time() I get the time nicely updated every second, but my notebook is blocking on this cell. So I tried threading.Thread(target=run_time).start() but this somehow only updates the time whenever I do interactions with the notebook. How can I update the notebook even if I do not do any cell interactions?
You want to have a look at ipyrallel to run multi kernels in you notebook.
Here is a solved issue
Here is the documentation