Modify ipywidgets button text color - python

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.

Related

In an HTML embedding of a Jupyter widget that contains a Plotly FigureWidget, on_click callbacks don't work

Here's a minimal example. It works perfectly in a Jupyter notebook, but when the html file is opened in a browser, clicking the plot produces no output.
If embed_minimal_html is called after the widget has been used in the Jupyter notebook several times and output is present, the same output will appear in the opened html file (without the black border for some reason), but additional clicks will not produce additional output.
import plotly.graph_objects as go
import ipywidgets as widgets
from ipywidgets.embed import embed_minimal_html
fig = go.FigureWidget(data=[go.Scatter(x=[1, 2], y=[1, 2])])
scatter = fig.data[0]
output = widgets.Output(layout={'border': '1px solid black'})
#scatter.on_click
def output_point(_, points, __):
with output:
print("Point: ({}, {})".format(points.xs[0], points.ys[0]))
minimal_onclick_widget = widgets.VBox([fig, output])
embed_minimal_html('minimal_onclick_widget.html', views=[minimal_onclick_widget], title="Minimal on_click widget")
minimal_onclick_widget
Any ideas what's going on, or how to fix it?
I think it might have something to do with server-side versus client-side event handling. The link and dlink widgets go through the Python kernel (server-side) and don't work in static html embeddings like the one above, but they have client-side alternatives jslink and jsdlink that do: https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html#Linking-widgets-attributes-from-the-client-side. It's not stated explicitly in the docs, but maybe on_click is a server-side event with no client-side alternative.

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.

In python notebook Change the height of a ipywidget

I am using ipywidgets in jupyterlab for displaying later on with voila.
In order to display information I create several text on containers
wd_EXTRA_output = widgets.Output(layout={'border': '1px solid black'})
with wd_EXTRA_output:
print('Information will be displayed here... ')
I dont want to pass a 'height' at the moment of creating the Output widget.
How can I later on modify the height of that widget?
wd_EXTRA_output(layout={'height': '200px'})
Note: I know I could have passed the height at the moment of creating the widget, but for particular reasons I cant do that.
When you directly change the python attributes of a widget the display will update automatically. So in this case you can do:
wd_EXTRA_output.layout.height = '200px'
you can also set the height to other valid html heights, e.g. '50%'

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

Categories

Resources