Open in Colab:

PlotMS in a Notebook

Original Author:


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
print("performing setup (tasks a few minutes)...")
os.system('apt-get install xvfb')
os.system('pip install casaplotms')
os.system('pip install casadata')
os.system('pip install pyvirtualdisplay')
performing setup (tasks a few minutes)...

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( )
<pyvirtualdisplay.display.Display at 0x7f0d0f7c2810>

Get Data

Somehow data must be made available…

print("download some data...")
os.system("tar -xvf")
download some data...

Plot Data

Start X11 application and plot data…

from casaplotms import plotms
print('making the plot...')
making the plot...

Display the Exported Raster Image

from IPython.display import Image