Open in Colab: https://colab.research.google.com/github/casangi/examples/blob/master/community/jupyter_and_x11.ipynb


PlotMS with Jupyter Notebooks and X11

Original Author: drs@nrao.edu

Description

X11 is a client/server windowing system. The client communicates with the server using the X Window System Core Protocol. This architecture allows the X11 server to accept drawing commands from a remote client and render the GUI drawing commands on the hardware which the server controls/uses, e.g. monitor, keyboard and mouse. The X11 server renders the drawing commands onto a frame buffer. This buffer allows for caching drawing results. At the appropriate time, the buffer contents are flushed video RAM.

This architecture allowed for the implementation of a virtual Frame Buffer and an accompanying virtual server. This allows X11 applications to run without actual display hardware. This is the only way to use X11 (including Qt) applications in the context of a Jupyter notebook. There is no other way.

If someone implemented an X11 server designed for use with Jupyter notebooks, the display of X11 applications within the notebook may be possible. This potential server would behave like XQuartz (the X11 server implementation for MacOS which renders X11 widgets using the native GUI environment on MacOS).

This notebook shows how virtual frame buffers can be used to run X11 applications within a Jupyter notebook. The application used is casaplotms, a Qt application.

The first step is to perform som configuration steps:

[ ]:
# Installation
import os
import sys
print("performing setup (tasks a few minutes)...")
!{sys.executable} -m pip install --index-url https://casa-pip.nrao.edu/repository/pypi-casa-release/simple casaplotms
!{sys.executable} -m pip install pyvirtualdisplay
performing setup (tasks a few minutes)...
Requirement already satisfied: casaplotms in /export/hypnos/local/lib64/python3.6/site-packages
Requirement already satisfied: grpcio in /export/hypnos/local/lib64/python3.6/site-packages (from casaplotms)
Requirement already satisfied: casatasks in /export/hypnos/local/lib/python3.6/site-packages (from casaplotms)
Requirement already satisfied: protobuf in /export/hypnos/local/lib64/python3.6/site-packages (from casaplotms)
Requirement already satisfied: six>=1.5.2 in /export/hypnos/local/lib/python3.6/site-packages (from grpcio->casaplotms)
Requirement already satisfied: scipy in /export/hypnos/local/lib64/python3.6/site-packages (from casatasks->casaplotms)
Requirement already satisfied: matplotlib in /export/hypnos/local/lib64/python3.6/site-packages (from casatasks->casaplotms)
Requirement already satisfied: casatools==6.0.0.27 in /export/hypnos/local/lib64/python3.6/site-packages (from casatasks->casaplotms)
Requirement already satisfied: setuptools in /usr/lib/python3.6/site-packages (from protobuf->casaplotms)
Requirement already satisfied: numpy>=1.13.3 in /export/hypnos/local/lib64/python3.6/site-packages (from scipy->casatasks->casaplotms)
Requirement already satisfied: python-dateutil>=2.1 in /export/hypnos/local/lib/python3.6/site-packages (from matplotlib->casatasks->casaplotms)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /export/hypnos/local/lib/python3.6/site-packages (from matplotlib->casatasks->casaplotms)
Requirement already satisfied: kiwisolver>=1.0.1 in /export/hypnos/local/lib64/python3.6/site-packages (from matplotlib->casatasks->casaplotms)
Requirement already satisfied: cycler>=0.10 in /export/hypnos/local/lib/python3.6/site-packages (from matplotlib->casatasks->casaplotms)
Requirement already satisfied: pyvirtualdisplay in /export/hypnos/local/lib64/python3.6/site-packages
Requirement already satisfied: EasyProcess in /export/hypnos/local/lib/python3.6/site-packages (from pyvirtualdisplay)

Setup Virtual Frame Buffer

The pyvirtualdisplay package can be used to configure and launch a virtual frame buffer server. However, the way new processes are directed to the new frame buffer server is via a unix shell environment variable, DISPLAY. This means that this frame buffer server will be used by all X11 processes. This makes it difficult to encapsulate this pattern.

[ ]:
from pyvirtualdisplay import Display
display = Display(visible=0,size=(2048,2048))
display.start( )
<Display cmd_param=['Xvfb', '-br', '-nolisten', 'tcp', '-screen', '0', '2048x2048x24', ':1017'] cmd=['Xvfb', '-br', '-nolisten', 'tcp', '-screen', '0', '2048x2048x24', ':1017'] oserror=None return_code=None stdout="None" stderr="None" timeout_happened=False>

Get Data

Somehow data must be made available…

[ ]:
print("download some data...")
os.system("wget https://bulk.cv.nrao.edu/almadata/public/working/sis14_twhya_calibrated_flagged.ms.tar")
os.system("tar -xvf sis14_twhya_calibrated_flagged.ms.tar")
print('complete')
download some data...
complete

Plot Data

Start X11 application and plot data…

[ ]:
from casaplotms import plotms
print('making the plot...')
plotms(vis='sis14_twhya_calibrated_flagged.ms',plotfile='sis14_twhya_calibrated_flagged.jpg',showgui=False,highres=True,width=600,height=350,overwrite=True)
print('complete')
making the plot...
Plot file /home/hypnos/dschieb/casa/casa-ensemble/test-virtualdisplay/sis14_twhya_calibrated_flagged.jpg
exporting the plot
complete

Display the Exported Raster Image

[ ]:
from IPython.display import Image
Image(filename="sis14_twhya_calibrated_flagged.jpg")
../../_images/examples_community_jupyter_and_x11_10_0.jpg