python终端不支持ipywidgets

时间:2018-06-26 12:13:16

标签: python-3.x ipython jupyter-notebook ipywidgets

我正在尝试使用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>

1 个答案:

答案 0 :(得分:0)

来自 user6764549 的评论:

Ubuntu 终端不支持交互式小部件。您需要在可以运行 Javascript 的类似“浏览器”或启用 GUI 的环境中运行它。

Jupyter notebook 不是问题,Ubuntu 终端是问题。它不支持显示图像或小部件。如果您只是想避免使用浏览器,则可以查看 Jupyter Qt consoleSpyder