How to hide ipywidget code in Jupyter Notebook - python

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

Related

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

I can't display both widget in 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.

holoviz/param/panel: updating Str or HTML panes via python callback

I would like to know if there is a way to update a panel pane content through a python callback.
If I define a Parameterized custom class the following way:
import panel as pn
pn.extension()
import param
class Myclass(param.Parameterized):
letter = param.ObjectSelector(
objects=['a', 'b', 'c', 'd'],
default='b',
)
#param.depends('letter')
def text(self):
return(pn.pane.Str(self.letter))
instance = Myclass()
If I output this instance in my notebook, I get the following:
pn.Row(instance.param.letter, instance.text)
yields:
However, when I select another entry in the dropdown list, the text on the right is not updated:
I know that the parameter has been updated, and that the text callback is fired (through debugging). Yet, no update is done in my notebook.
I feel that this example is very similar to the Sine wave example in the documentation (https://panel.holoviz.org/user_guide/Param.html), but I don't get what I am doing wrong...
Ideally, the answer should also work with a panel.pane.HTML as well as a panel.pane.Str.
I tried your code in jupyter notebook and it works: the text gets updated when the dropdown changes.
If I try it in my jupyter lab it doesn't work, but I have that more often. I think with me it's a jupyter version / installation thing.
So I think your code is correct. You could try updating your jupyter notebook or your panel or param packages.

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)

Why is IPython.display.Image not showing in output?

I have a cell that looks like this:
from IPython.display import Image
i = Image(filename='test.png')
i
print("test")
The output is just:
test
I don't see the image in the output. I checked to see that the file exists (anyway, if it did not exist, you get an error).
Any clues?
creating the image with
i = Image(filename='test.png')
only creates the object to display. Objects are displayed by one of two actions:
a direct call to IPython.display.display(obj), e.g.
from IPython.display import display
display(i)
the displayhook, which automatically displays the result of the cell, which is to say putting i on the last line of the cell. The lone i in your example doesn't display because it is not the last thing in the cell. So while this doesn't display the image:
i = Image(filename='test.png')
i
print("test")
This would:
i = Image(filename='test.png')
print("test")
i
I had the same problem.
Matplot lib expects to show figs outside the command line, for example in the GTK or QT.
Use this: get_ipython().magic(u'matplotlib inline')
It will enable inline backend for usage with IPython notebook.
See more here and here.
After looking into the code, I can say, that under Windows, as of IPython 7.1.0, this is only supported with %matplotlib inline, which does not work under the interactive IPython shell.
There is extra code in Jupyter. The following example works with
jupyter qtconsole
jupyter notebook
jupyter console in principle, but only via external program, and then for the example the temporary file got deleted
def test_display():
import pyx
c = pyx.canvas.canvas()
circle = pyx.path.circle(0, 0, 2)
c.stroke(circle, [pyx.style.linewidth.Thick,pyx.color.rgb.red])
return c
display(test_display())

Categories

Resources