ipywidgets не поддерживается в терминале Python - PullRequest
0 голосов
/ 26 июня 2018

Я пытаюсь запустить приведенный ниже код с помощью терминала Python, он работает совершенно нормально, когда я запускаю в ноутбуке Jupyter, но в терминале Ubuntu он выдает ошибку. даже попытался запустить

jupyter nbextension enable --py --sys-prefix widgetsnbextension
на терминале до сих пор я получаю ту же ошибку
import matplotlib as mp
from pylab import *
from sklearn import datasets
from ipywidgets import interact, widgets
from IPython.display import display, clear_output

faces = datasets.fetch_olivetti_faces()

class Trainer:
    def __init__(self):
        self.results = {}
        self.imgs = faces.images
        self.index = 0

    def increment_face(self):
        if self.index + 1 >= len(self.imgs):
            return self.index
        else:
            while str(self.index) in self.results:
                print(self.index)
                self.index += 1
            return self.index

    def record_result(self, smile=True):
        self.results[str(self.index)] = smile

trainer = Trainer()

button_smile = widgets.Button(description='smile')
button_no_smile = widgets.Button(description='sad face')

def display_face(face):
    clear_output()
    imshow(face, cmap='gray')
    axis('off')
    show()

def update_smile(b):
    trainer.record_result(smile=True)
    trainer.increment_face()
    display_face(trainer.imgs[trainer.index])

def update_no_smile(b):
    trainer.record_result(smile=False)
    trainer.increment_face()
    display_face(trainer.imgs[trainer.index])

button_no_smile.on_click(update_no_smile)
button_smile.on_click(update_smile)

display(button_smile)
display(button_no_smile)
display_face(trainer.imgs[trainer.index])

Ошибка трассировки при запуске из терминала Python

Widget Javascript not detected.  It may not be installed properly. Did you enable the widgetsnbextension? If not, then run "jupyter nbextension enable --py --sys-prefix widgetsnbextension"
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/IPython/core/formatters.py in __call__(self, obj)
    880             method = get_real_method(obj, self.print_method)
    881             if method is not None:
--> 882                 method()
    883                 return True
    884 

/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipywidgets/widgets/widget.py in _ipython_display_(self, **kwargs)
    480                 loud_error('The installed widget Javascript is the wrong version.')
    481 
--> 482             self._send({"method": "display"})
    483             self._handle_displayed(**kwargs)
    484 

/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipywidgets/widgets/widget.py in _send(self, msg, buffers)
    485     def _send(self, msg, buffers=None):
    486         """Sends a message to the model in the front-end."""
--> 487         self.comm.send(data=msg, buffers=buffers)
    488 
    489 

/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipykernel/comm/comm.py in send(self, data, metadata, buffers)
    119         """Send a message to the frontend-side version of this comm"""
    120         self._publish_msg('comm_msg',
--> 121             data=data, metadata=metadata, buffers=buffers,
    122         )
    123 

/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipykernel/comm/comm.py in _publish_msg(self, msg_type, data, metadata, buffers, **keys)
     64         metadata = {} if metadata is None else metadata
     65         content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
---> 66         self.kernel.session.send(self.kernel.iopub_socket, msg_type,
     67             content,
     68             metadata=json_clean(metadata),

AttributeError: 'NoneType' object has no attribute 'session'
<ipywidgets.widgets.widget_button.Button at 0x7f0de5ebfb38>
Widget Javascript not detected.  It may not be installed properly. Did you enable the widgetsnbextension? If not, then run "jupyter nbextension enable --py --sys-prefix widgetsnbextension"
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/IPython/core/formatters.py in __call__(self, obj)
    880             method = get_real_method(obj, self.print_method)
    881             if method is not None:
--> 882                 method()
    883                 return True
    884 

/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipywidgets/widgets/widget.py in _ipython_display_(self, **kwargs)
    480                 loud_error('The installed widget Javascript is the wrong version.')
    481 
--> 482             self._send({"method": "display"})
    483             self._handle_displayed(**kwargs)
    484 

/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipywidgets/widgets/widget.py in _send(self, msg, buffers)
    485     def _send(self, msg, buffers=None):
    486         """Sends a message to the model in the front-end."""
--> 487         self.comm.send(data=msg, buffers=buffers)
    488 
    489 

/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipykernel/comm/comm.py in send(self, data, metadata, buffers)
    119         """Send a message to the frontend-side version of this comm"""
    120         self._publish_msg('comm_msg',
--> 121             data=data, metadata=metadata, buffers=buffers,
    122         )
    123 

/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipykernel/comm/comm.py in _publish_msg(self, msg_type, data, metadata, buffers, **keys)
     64         metadata = {} if metadata is None else metadata
     65         content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
---> 66         self.kernel.session.send(self.kernel.iopub_socket, msg_type,
     67             content,
     68             metadata=json_clean(metadata),

AttributeError: 'NoneType' object has no attribute 'session'
<ipywidgets.widgets.widget_button.Button at 0x7f0ddadc2cc0>
...