Search code examples
pythonipywidgets

Ipywidgets button and dropdown not showing showing output in Jupyter lab


I am trying to create a small project using Button and Dropdown widgets from ipywidgets in Jupyter Lab. Sample code is

btn = widgets.Button(description='Mark')
display(btn)
def btn_eventhandler(obj):
    print('The name is {} !'.format(obj.description))
btn.on_click(btn_eventhandler)

I get the button but it does not give any output on clicking as seen in image below. There is no error message either. enter image description here

The result that i want can be seen in the log classified under warning as seen in the image below

enter image description here

The same issue also arises in using Dropdown. However, Intslider widget works fine without any issues

I have tried some of the suggestions mentioned in these threads but none worked

  1. How to get ipywidgets working in Jupyter Lab?
  2. jupyter notebook ipywidgets not showing (no error)
  3. Interactive Jupyter Widgets not working in Jupyter Lab

These are the jupyter versions that are currently installed-

  • jupyter core : 4.7.0
  • jupyter-notebook : 6.2.0
  • qtconsole : 5.0.1
  • ipython : 7.31.0
  • ipykernel : 5.4.2
  • jupyter client : 6.1.7
  • jupyter lab : 3.0.12
  • nbconvert : 6.0.7
  • ipywidgets : 7.6.5
  • nbformat : 5.0.8
  • traitlets : 4.3.3

Is there something that I am doing wrong? I tried running the same in Jupyter Notebook where it works fine Thank you


Solution

  • This issue is addressed in the ipywidgets documents:

    To capture prints (or any other kind of output) and ensure it is displayed, be sure to send it to an Output widget (or put the information you want to display into an HTML widget).

    Here is a modified version of your sample code :

    enter image description here