<?xml version="1.0" encoding="utf-8"?><!DOCTYPE article  PUBLIC '-//OASIS//DTD DocBook XML V4.4//EN'  'http://www.docbook.org/xml/4.4/docbookx.dtd'><article><articleinfo><title>Python</title><revhistory><revision><revnumber>6</revnumber><date>2023-03-28 12:26:03</date><authorinitials>kputyr</authorinitials></revision><revision><revnumber>5</revnumber><date>2023-03-22 09:37:11</date><authorinitials>kputyr</authorinitials></revision><revision><revnumber>4</revnumber><date>2023-03-20 14:56:37</date><authorinitials>kputyr</authorinitials></revision><revision><revnumber>3</revnumber><date>2023-03-20 14:32:44</date><authorinitials>crose</authorinitials></revision><revision><revnumber>2</revnumber><date>2023-03-20 14:32:29</date><authorinitials>crose</authorinitials></revision><revision><revnumber>1</revnumber><date>2023-03-20 14:31:56</date><authorinitials>crose</authorinitials></revision></revhistory></articleinfo><section><title>Python</title><section><title>matplotlib: no window</title><section><title>The problem</title><para>No window is opened when a script is run from a command line. Running the script in an interactive IDE is fine. </para></section><section><title>What happens</title><para>The module <code>matplotlib</code> 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. </para></section><section><title>How to fix</title><para>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: </para><programlisting format="linespecific" language="highlight" linenumbering="numbered" startinglinenumber="1"><methodname><![CDATA[ctrlWindow]]></methodname><![CDATA[ = ]]><methodname><![CDATA[matplotlib]]></methodname><![CDATA[.]]><methodname><![CDATA[pyplot]]></methodname><![CDATA[.]]><methodname><![CDATA[figure]]></methodname><![CDATA[(]]><methodname><![CDATA[figsize]]></methodname><![CDATA[=(5, 1.5))]]>
<methodname><![CDATA[ctrlWindow]]></methodname><![CDATA[.]]><methodname><![CDATA[text]]></methodname><![CDATA[(0.5, 0.5, ]]><phrase><![CDATA["]]></phrase><phrase><![CDATA[Press any key to close]]></phrase><![CDATA[
]]><phrase><![CDATA[while this window is active.]]></phrase><phrase><![CDATA["]]></phrase><![CDATA[, ]]><methodname><![CDATA[ha]]></methodname><![CDATA[=]]><phrase><![CDATA["]]></phrase><phrase><![CDATA[center]]></phrase><phrase><![CDATA["]]></phrase><![CDATA[, ]]><methodname><![CDATA[va]]></methodname><![CDATA[=]]><phrase><![CDATA["]]></phrase><phrase><![CDATA[center]]></phrase><phrase><![CDATA["]]></phrase><![CDATA[, ]]><methodname><![CDATA[size]]></methodname><![CDATA[=16)]]>
<methodname><![CDATA[ctrlWindow]]></methodname><![CDATA[.]]><methodname><![CDATA[show]]></methodname><![CDATA[()]]>
<token><![CDATA[while]]></token><![CDATA[ ]]><token><![CDATA[not]]></token><![CDATA[ ]]><methodname><![CDATA[mathplotlib]]></methodname><![CDATA[.]]><methodname><![CDATA[pyplot]]></methodname><![CDATA[.]]><methodname><![CDATA[waitforbuttonpress]]></methodname><![CDATA[(): ]]><token><![CDATA[pass]]></token>
</programlisting><para>The function <code>waitforbuttonpress()</code> waits for a mouse click or a key being pressed and returns respectively <code>False</code> or <code>True</code>. 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. </para><para><emphasis role="strong">Note:</emphasis> the program stops only when a key is pressed while the extra window is active. This allows to interact with other windows without limitation. </para></section></section><section><title>ngsolve.webgui.Draw() does not show a picture</title><section><title>The problem</title><para>The command <code>Draw(...)</code> prints <code>BaseWebGuiScene</code> instead of showing a graphics </para></section><section><title>What happens</title><para>The WebGUI extension used by NGSolve has not been integrated with Jupyter, so that the notebook does not know what to do with them. </para></section><section><title>How to solve</title><para>Install the WebGUI extension with the following commands: </para><screen><![CDATA[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]]></screen></section></section><section><title>ngsolve/netgen: RuntimeError: std::bad_cast</title><section><title>The problem</title><para>Importing <code>netgen.gui</code> opens a separate NGSolve window. After the window is closed, any Python code using ngsolve ends with <code>RuntimeError: std::bad_cast</code>. </para></section><section><title>What happens</title><para>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. </para></section><section><title>How to solve</title><para>Keep the window opened by netgen.gui opened all the time. In case the window was closed, please reopen it with </para><programlisting format="linespecific" language="highlight" linenumbering="numbered" startinglinenumber="1"><methodname><![CDATA[netgen]]></methodname><![CDATA[.]]><methodname><![CDATA[gui]]></methodname><![CDATA[.]]><methodname><![CDATA[StartGUI]]></methodname><![CDATA[()]]>
</programlisting><para><emphasis role="strong">Note</emphasis>: the ngsolve libraries should be loaded in a background without opening the window. Currently we are not aware why this is not the case. </para></section></section></section></article>