location: Python

Institute of Mathematics - PublicMathWiki:

Python

matplotlib: no window

The problem

No window is opened when a script is run from a command line. Running the script in an interactive IDE is fine.

What happens

The module matplotlib closes all windows when unloaded. This happens immediately after the script terminates if it is run from a command line. When run inside a IDE, then the module stays loaded, keeping windows opened.

How to fix

A quick solution is wait for an input from a user at the end of the script. The following code, when added at the end of the script, opens an extra window asking to press any key:

   1 ctrlWindow = matplotlib.pyplot.figure(figsize=(5, 1.5))
   2 ctrlWindow.text(0.5, 0.5, "Press any key to close\nwhile this window is active.", ha="center", va="center", size=16)
   3 ctrlWindow.show()
   4 while not mathplotlib.pyplot.waitforbuttonpress(): pass

The function waitforbuttonpress() waits for a mouse click or a key being pressed and returns respectively False or True. The loop makes it sure that the program exists only in the second situation, so that you can interact with the windows using your mouse.

Note: the program stops only when a key is pressed while the extra window is active. This allows to interact with other windows without limitation.

ngsolve.webgui.Draw() does not show a picture

The problem

The command Draw(...) prints BaseWebGuiScene instead of showing a graphics

What happens

The WebGUI extension used by NGSolve has not been integrated with Jupyter, so that the notebook does not know what to do with them.

How to solve

Install the WebGUI extension with the following commands:

pip3 install webgui_jupyter_widgets
jupyter nbextension install --user --py widgetsnbextension
jupyter nbextension enable --user --py widgetsnbextension
jupyter nbextension install --user --py webgui_jupyter_widgets
jupyter nbextension enable --user --py webgui_jupyter_widgets

ngsolve/netgen: RuntimeError: std::bad_cast

The problem

Importing netgen.gui opens a separate NGSolve window. After the window is closed, any Python code using ngsolve ends with RuntimeError: std::bad_cast.

What happens

NGSolve is not written entirely in Python, but instead it delegates computation to external libraries written in C/C++. In order for this to work, netgen.gui loads the libraries in a separate window and then it communicates with it. Once the window is closed, the libraries are no longer in the memory and ngsolve/netgen functions fail to access them.

How to solve

Keep the window opened by netgen.gui opened all the time. In case the window was closed, please reopen it with

   1 netgen.gui.StartGUI()

Note: the ngsolve libraries should be loaded in a background without opening the window. Currently we are not aware why this is not the case.

PublicMathWiki: Python (last edited 2023-03-28 12:26:03 by kputyr)