jupyterlab button event not working - events

This code work in jupyter notebook, but doesn't work in jupyterlab:
import ipywidgets as widgets
from IPython.display import display
button = widgets.Button(description="Click Me!")
display(button)
def on_button_clicked(b):
print("Button clicked.")
button.on_click(on_button_clicked)
Does anyone have a solution ?
Env:
MacOsX 10 10.12.2
Python 2.7.14 :: Anaconda, Inc.
Jupyter Notebook 5.4.0
Jupyter Lab 0.31.5

Current, still know as issue ... but here I found a solution.
import ipywidgets as widgets
button = widgets.Button(description='Display Chart')
out = widgets.Output()
def on_button_clicked(b):
button.description = 'clicked'
with out:
print('Ay')
button.on_click(on_button_clicked)
widgets.VBox([button, out])

Related

opencv issues with M1 MAC - OpenCV imshow doesnot work

I purchased a M1 Mac. Is anyone having issues with imshow with opencv. I did pip install opencv-python and brew install opencv and brew install opencv as well.
import cv2
import urllib
import numpy as np
import requests
url = 'https://www.visitcalifornia.com/sites/visitcalifornia.com/files/styles/welcome_image/public/vc_crtr_borntobewild_module_mendocino_st_rf_623667652_1280x640.jpg'
from skimage import io
img = io.imread(url)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imshow('URL Image', img)
cv2.waitKey()
and also
import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.destroyWindow("preview")
vc.release()
does not work for me
It was solved by
pip install opencv-python opencv-python-headless
I was able to solve this by building from the source code of OpenCV 4.5, use this link to get the source code
Try to install Miniconda from https://docs.conda.io/en/latest/miniconda.html
then activate it and try to install cv2 pip install opencv-python
getting 80-90 FPS for just reading image & showing
from my side, all are working in M1 using Miniconda
Opening the terminal in rosetta and doing a pip install(inside virtual environment) helped me build the opencv. Before this , while I was doing it with the M1 terminal, it was giving errors stating some architecture issue. Guess this did the trick to me.
Create Virtual Environment -> Activate it -> (Rosetta Terminal) -> pip3 install opencv-python
Install OpenCV using pip. This normally gives a ffmpeg error, so install that first
First do:
pip3 install ffmpeg
and then
pip3 install opencv-python
I thought I had the same issue. The window was not popping up when using cv2.imshow() or cv2.namedWindow(). I realized hours later that the window was there in the Mac menu bar, I just had to click on it.

Can't install and use properly pythreejs

I installed pythreejs following the instructions on the official website
pip3 install pythreejs
jupyter nbextension install --user --py pythreejs
jupyter nbextension enable --user --py pythreejs
Looking at the output of jupyter nbextension list, everything looks fine:
Known nbextensions:
config dir: /home/gael/.jupyter/nbconfig
notebook section
jupyter-threejs/extension enabled
- Validating: OK
But when I start the server (jupyter notebook), create a new (Python3) notebook, and try to run the example code
from pythreejs import *
import numpy as np
from IPython.display import display
from ipywidgets import HTML, Text, Output, VBox
from traitlets import link, dlink
ball = Mesh(geometry=SphereGeometry(radius=1, widthSegments=32, heightSegments=24),
material=MeshLambertMaterial(color='red'),
position=[2, 1, 0])
c = PerspectiveCamera(position=[0, 5, 5], up=[0, 1, 0],
children=[DirectionalLight(color='white', position=[3, 5, 1], intensity=0.5)])
scene = Scene(children=[ball, c, AmbientLight(color='#777777')])
renderer = Renderer(camera=c,
scene=scene,
controls=[OrbitControls(controlling=c)])
display(renderer)
from pythreejs' official repository, it does not show me a visualization window embedded in the page. Instead, below the cell it displays a hash:
UmVuZGVyZXIoY2FtZXJhPVBlcnNwZWN0aXZlQ2FtZXJhKGNoaWxkcmVuPShEaXJlY3Rpb25hbExpZ2h0KGNvbG9yPSd3aGl0ZScsIGludGVuc2l0eT0wLjUsIHBvc2l0aW9uPSgzLjAsIDUuMCzigKY=
I also tried with jupyter lab, and with Python2, and it had exactly the same behavior.
What am I missing?
System: Ubuntu 18.04
Jupyter Notebook version: 5.7.8
Looking more closely at jupyter's output in the console, I read:
[W 18:55:45.544 NotebookApp] 404 GET /nbextensions/widgets/notebook/js/extension.js?v=20200107185531 (127.0.0.1) 14.69ms referer=http://localhost:8889/notebooks/Untitled.ipynb?kernel_name=python3
Following advice for this error I do:
jupyter nbextension enable --py widgtesextension
then start the server: and pythreejs works!!

ImportError: cannot import name QtGui or any other PyQt4 module

I´m using PyQt4 with Mac Yosemite 10.10.5. and Python 2.7. When I try to run a simple script using sublime text I get
´ImportError: cannot import name QtGui.
However when I go into python through the terminal and enter the following line, it seems to accept it.
>>> from PyQt4 import QtGui
>>>
The script I´m trying to run in Sublime Text is:
import sys
from PyQt4 import QtGui
def main():
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
Main()
My Pyqt4 path is at /Library/Python/2.7/site-packages.
My sys-path output is:
['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Users/cameronmacintyre/Library/Python/2.7/lib/python/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']
I installed using sip and PyQt4 was installed with the following:
python configure.py -q /usr/bin/qmake-4.8 -d /Library/
Python/2.7/site-packages/ --use-arch x86_64
•
make
•
sudo make instal
I´ve searched other similar problems for days and can´t find a solution. I´m a bit of a newbie to this, so any help would be appreciated.
ps I did have Python 3 installed for a while but I removed it.

Can't make a tk image Button with IPython Notebook

I'm using an IPython notebook to create a Tk application in Python. I can make a button with a text label:
%matplotlib tk
import tkinter as tk
root = tk.Tk()
button = tk.Button(root, text = 'IPython')
button.pack()
But if I try to make a button with an image I get an error message. This code works fine when run from the command line, but crashes when executed in an IPython Notebook:
%matplotlib tk
import tkinter as tk
root = tk.Tk()
img = tk.PhotoImage(file='IPy_header.gif')
print(img)
button = tk.Button(root, image = img)
button.pack()
The print statement verifies the PhotoImage object was created (i.e. no 'file not found' errors). The call to tk.Button leads to this error:
TclError: image "pyimage2" doesn't exist
I'm using ipython/jupyter 3.0.0 on Mac OS X 10.10.3
Anybody have a suggestion?
You may have to enable the tk event loop with %gui tk
magic-gui documentation.

How to import packages/modules into spyder?

I just installed python in my MacOS using the Anaconda distribution. My problem is although the packages (eg. matplotlib, numpy, scipy) came included with the installation, I have to import them to spyder every time which is tedious and it's also tiring that that I have to remind spyder of their functionalities.
For eg, in Windows, I only needed to type in the console:
x=array([...,...,...])
but in my mac it would have to be:
import numpy as py
and then type into the console:
x=py.array([...,...,...])
I do notice that in that the spyder-python console (Windows version), there is a text saying,
Imported NumPy 1.8.1, SciPy 0.13.3, Matplotlib 1.3.1 + guidata 1.6.1, guiqwt 2.3.2
Type "scientific" for more details.
That is probably the reason why I don't have to import anything in Windows because Spyder already did it.
How do I do the same for Mac?
Thank you

Resources