Updating widget from background thread in Colab - google-colaboratory

In my Colab script, I would like to regularly update an IPython widget through a background task (ie. without blocking the main thread and prevent interaction with the notebook).
I found this section in the Jupyter Widgets documentation: https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Asynchronous.html#Updating-a-widget-in-the-background
import threading
from IPython.display import display
import ipywidgets as widgets
import time
progress = widgets.FloatProgress(value=0.0, min=0.0, max=1.0)
def work(progress):
total = 100
for i in range(total):
time.sleep(0.2)
progress.value = float(i+1)/total
thread = threading.Thread(target=work, args=(progress,))
display(progress)
thread.start()
This does not work in Google Colab, unless I follow this with a thread.join() - which obviously defeats the point...
Does anyone have a solution?

Related

PyQT5 problem with reading from port with QtSerialPort

I am currently trying to communicate with a device which sends a data stream by COM. I am having a weird problem with QtSerialPort which I currently don't know how to solve. The description of the problem is: When I restart the device and restart my application, in the application I can open the corresponding port without any error. However, I do not receive any data from the device. I am trying to read data from the COM-Port with the signal readyRead. Now, when I open the port with another program B which uses python's serial, I can successfully read from the device after a restart. Now, after reading successfully from the device with program B, I can also successfully read from the device using the QT-Program A, but only if I do not restart the device. The following code contains the isolated port with QtSerial which reproduces the above mentioned problem.
import sys
from PyQt5 import QtSerialPort
from PyQt5.QtCore import *
from PyQt5.QtSerialPort import *
from PyQt5.QtWidgets import QApplication, QMainWindow
class QtSerialTest(QMainWindow):
def __init__(self, parent=None) -> None:
super().__init__()
self.port_com_port = QtSerialPort.QSerialPort()
self.port_com_port.setPortName("COM4")
self.port_com_port.setBaudRate(QSerialPort.BaudRate.Baud115200)
self.port_com_port.setParity(QSerialPort.Parity.NoParity)
self.port_com_port.setDataBits(QSerialPort.DataBits.Data8)
self.port_com_port.setStopBits(QSerialPort.StopBits.OneStop)
self.port_com_port.open(QIODevice.ReadWrite)
self.port_com_port.readyRead.connect(self.readFromSerial)
def readFromSerial(self):
print(self.port_com_port.readAll())
if __name__ == '__main__':
app = QApplication(sys.argv)
test = QtSerialTest()
app.exec()
I can confirm that after a restart, the function "readFromSerial" is never called, although the device sends.
EDIT: I forgot to mention: I compared the port-settings from program A and B, they are equal
After some search, I could remove the workaround and it works now. I missed an important configuration-parameter. After opening the QtSerialPort, self.port_com_port.setDataTerminalReady(True) needs to be called.

Using matplotlib with Qt5 backend with an existing QApplication running, in the same process

We have a Qt5 application that uses PySide2. Recently, we wanted to show plots using matplotlib while our PySide2 application is running in the same process (in a different thread) but then matplotlib crashes (when using PySide2) or freezes before drawing (when using PyQt5).
Here is a minimal sample, uncomment the 23rd line to make matplotlib crash or freeze:
from threading import Thread
from PySide2.QtWidgets import QApplication, QLabel
import matplotlib
matplotlib.use('QT5Agg')
import matplotlib.pyplot as plt
def start_qt_app():
t = Thread(target=qt_app_thread_func)
t.start()
def qt_app_thread_func():
app = QApplication()
label = QLabel("Hello World")
label.show()
app.exec_()
# Uncomment the line below to make matplotlib crash.
#start_qt_app()
plt.plot([1, 2, 3, 4])
plt.show()
input("Press enter to quit.")
print("Finished.")
My guess is that this is related to the limitation that we can only have 1 QApplication running in a process. So this causes something to go wrong in matplotlib.
How can I solve this problem? One solution that comes to my mind is to create a proxy object for matplotlib that runs matplotlib in another process but I am not sure if this would be the least labor intensive solution. Maybe I can somehow make matplotlib use the existing QApplication? I cannot run our PySide2 app in another process since it uses dynamically created numpy arrays to be passed from main thread to the GUI, and it would cost performance to start it in another process.
As #ImportanceOfBeingErnest points out, matplotlib can live with Qt as the official example shows: Embedding in Qt.
Example:
from PySide2.QtWidgets import QApplication, QLabel
import matplotlib
matplotlib.use('QT5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
if __name__ == '__main__':
app = QApplication()
label = QLabel("Hello World")
label.show()
canvas = FigureCanvas(Figure(figsize=(5, 3)))
ax = canvas.figure.subplots()
ax.plot([1, 2, 3, 4])
canvas.show()
app.exec_()
you should use asyncqt to manage ui and application threads.from asyncqt QEventLoop is a way you can accomplish with
await loop.run_in_executor(exec, method_name)
asyncqt is a spin off library from quamash which is written for PyQt5. example codes are same. So upon your preference, you may use asyncqt with PySide2 or quamash with PyQt5 to make your app responsive when background tasks are running.
asyncqt examples
asyncqt 0.7.0 pypi
Quamash 0.6.1

Google Colab: Why matplotlib has a behaviour different then default after importing pandas_profiling?

When using a notebook in Google Colab, my matplotlib plots have different behaviors whether I import the library pandas-profiling or not.
If I do not import pandas-profiling, the plots are displayed inline by default. But if I import the library, the plots stop being displayed inline.
Workarounds (possible solutions)
Updating the pandas-profiling library before importing it resolves the problem.
Adding %matplotlib inline after importing pandas-profiling resolves the problem.
Reproducing
Run this code in Google Colab to reproduce the problem. Test it with and without importing pandas_profiling. For each test, you will need to terminate the session (Runtime->Manage Sessions->Terminate). Just restarting the runtime is not enough.
import matplotlib.pyplot as plt
# importing the pandas_profiling makes matplotlib
# to stop showing the plot inline
# import pandas_profiling
plt.plot([1, 2], [1, 2])
The expected behavior is to show the plot inline by default, but after importing pandas-profiling, the plots stop being displayed inline.
The real problem
I stumbled upon this problem when my seaborn plotting functions started to break.
For example, consider the following code.
import matplotlib.pyplot as plt
# import pandas_profiling
import seaborn as sns
def plot():
ax = sns.pointplot([1, 2], [1, 2])
print(len(ax.collections))
Now call plot() in two different jupyter cells.
Without pandas-profiling: each function call will print 1.
With pandas-profiling: each function call will add 1 to the previous output.
The problem is in the version of pandas_profiling that is installed by default in Google Colab. The installed version (1.4.1) used to change the matplotlib's backend to agg when imported. The default matplotlib's backend in Colab is module://ipykernel.pylab.backend_inline.
We can see that by running the following before and after importing pandas-profiling:
import matplotlib
matplotlib.get_backend()
This behavior was changed in pull-request #125, which stops changing matplotlib's backend.
Until Google Colab updates the installed version, manually updating it seems to be the best solution. Just run the following in your Colab Notebook:
!pip install --upgrade pandas_profiling

By picture that is displayed in Jupyter notebook isn't shown when running the same code in IDE? [duplicate]

This question already has an answer here:
matplotlib Plot does not appear
(1 answer)
Closed 4 years ago.
I was trying to run code from an online tutorial on my local machine by copying code from Jupiter notebook to my IDE (pycharm).
This part
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
from torchvision import transforms
import matplotlib.pyplot as plt
pig_img = Image.open("pig.jpg")
preprocess = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
])
pig_tensor = preprocess(pig_img)[None,:,:,:]
plt.imshow(pig_tensor[0].numpy().transpose(1,2,0))
While Jupiter notebook shows the imagine - I cannot get it displayed when running in terminal or IDE.
Any ideas why?
Thank you
You need to call .show() explicitly to show the image in terminal i.e.
Add this to the end of the code
plt.show()
From the documentation:
Display a figure. When running in ipython with its pylab mode, display all figures and return to the ipython prompt.
In non-interactive mode, display all figures and block until the figures have been closed; in interactive mode it has no effect unless figures were created prior to a change from non-interactive to interactive mode (not recommended). In that case it displays the figures but does not block.

PyQt5 Main window doesn't render on Raspberry 3B+ (Raspian Stretch, Python 3.5)

This question applies to Raspberry Pi 3B+ running Stretch. Python 3.5. The user interface for a Qt5 application is created. If there is some time-consuming setup before app.exec_(), only an empty window frame is visible, instead of the fully rendered window.
This behavior is different from what's seen on MacOs, for example, where the window is, indeed, rendered.
import time
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
# the main GUI window
class App(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Test')
# create and run the interactive application
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = App() # create the application window
mainWindow.show() # this should show the window
app.processEvents() # everything should be updated
# delay. only an empty window frame is shown.
print ('Delay 10s...')
time.sleep(10)
# enter the GUI loop. now the window is rendered.
sys.exit(app.exec_())
Only an empty window frame is shown. What's expected is a fully rendered window (in this case, it should be just a white empty window but if there are other GUI elements, they should show as well.