location: Diff for "Python"

Institute of Mathematics - PublicMathWiki:

Differences between revisions 1 and 5 (spanning 4 versions)
Revision 1 as of 2023-03-20 14:31:56
Size: 23
Editor: crose
Comment:
Revision 5 as of 2023-03-22 09:37:11
Size: 2046
Editor: kputyr
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Describe Python here. <<TableOfContents>>

= 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:
{{{#!highlight python
ctrlWindow = matplotlib.pyplot.figure(figsize=(5, 1.5))
ctrlWindow.text(0.5, 0.5, "Press any key to close\nwhile this window is active.", ha="center", va="center", size=16)
ctrlWindow.show()
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
}}}

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

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