Reputation: 2423
I have a container running with Python/JupyterLab and all of my dependencies. I start it with:
docker run --rm -it -p 8888:8888 \
--mount type=bind,source=/project,target=/work \
python-3.9.1-jupyterlab
It launches jupyterlab and I can connect through the browser. Everything is good.
Now I am experimenting with using VSCode as a Python IDE. It is helpful to attach a shell from VSCode to my container so I can run iPython and edit my code all in one place. I run "attach shell" from the VSCode Docker extension:
docker exec -it {containerID} bash <
Then I open an iPython shell:
jo@:~/work $ ipython --pylab
Python 3.9.1
IPython 7.20.0 -- An enhanced Interactive Python.
Using matplotlib backend: agg
In [1]: matplotlib.get_backend()
Out[1]: 'agg'
In [2]: import matplotlib.pyplot as plt
In [3]: plt.plot([1.6, 2.7])
Out[3]: [<matplotlib.lines.Line2D at 0x7f5ed0ed8d30>]
In [4]: plt.show()
In [5]: %matplotlib inline
In [6]: plt.plot([1.6, 2.7])
Out[6]: [<matplotlib.lines.Line2D at 0x7f5ed0df5d60>]
<Figure size 432x288 with 1 Axes>
In [7]: plt.show()
I cannot see any plots. I have tried rendering them with different backends (default was 'agg'). I am thinking this is because the kernel -- executing on the container -- cannot make use of the host graphics (i.e., kernel can render plots but cannot display them). Perhaps I do not have host/container ports mapped properly.
Could someone provide some guidance on things to try? Here is the image for the container I am using.
Upvotes: 6
Views: 1008
Reputation: 5557
You are running jupyterlab in a shell, which is not a graphical environment. In addition, if you are running the Docker container from non X11 desktop, the DISPLAY variable (used by X11 to display graphics) will not be set. If you are running it from a linux desktop, you can set the environment to allow the X11 as in this example, but it's not same for all environments and you might need to research further how to set your specific environment to allow X11 servers from docker container to communicate with X11 client on the host (note that in X11, the application is the server
and the desktop is the client
)
On Linux, if the environment variable DISPLAY is unset, the "event loop" is identified as "headless", which causes a fallback to a noninteractive backend (agg). You can read more about the backends and how they are selected in the matplotlib documents. Once the "even loop" is set to be "headless", all of the rendering will be done in files. You can check this answer on how to use the mahtplotlib with this backend.
A good approach to use Jupyther and the benefits of VSCode would be to use an extension for VSCode. One of the most popular extensions is jupyter developed by Microsoft. The extension will allow you to have the same experiance as using the browser.
Upvotes: 3