Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

It is possible to run a Python script in several ways

1- From the lower entry, can invoke Tcl code writing In the entry that is placed at the bottom of the main GiD window Tcl code can be executed by adding the prefix -np- followed some by a space and then some the desired tcl code an press and pressing <Return> .

Note: -np- mean 'No-Process' (that is, the words are not GiD known process keywords) and instead the next command is expected to be Tcl code. It is used as a fast way to run a simple procedure or to re-define code.

Use copy/paste code after -np-

In particular there are some predefined Tcl procs that run python code, like GiD_Python_Exec which expects the python code to be evaluated.

Since GiD version 17, matplotlib modules does not come with GiD.

Install matplotlib Python module with:

-np- GiD_Python_PipInstallMissingPackages [list matplotlib]

From now on you can use the matplot module.

Example: matplotlib graph of a line

This is simple example calls a Python code from Tcl and uses the matplotlib module to draw a graph (in a new Tk window opened by tkinter):

Code Block
languagepy
GiD_Python_Exec {
    import matplotlib.pyplot as plt
    plt.plot([1, 2, 3, 4])
    plt.ylabel('some numbers')
    plt.show()
}

GiD_Python_Exec does an implicit package require tohil that load it and then calls tohil::exec with the provided script.

This Tcl code can be executed, for example, by pasting it after -np- in the command line (other possibilities are with one or more spaces separating the code pasted), and pressing the <Return> key.

...

Then a window like this will appear:

...

There are other procs similar to GiD_Python_Exec, like GiD_Python_Eval to evaluate a single instruction which will return a value, or GiD_Python_Call, … to invoke a function that has been previously defined.

Source a file with the code

The normal way to write long Python code is to write it in a .py file.

Note: Visual Studio Code is our recommended editor accompanied by the MS Python extension for this language.

GiD_Python_Source is a Tcl proc that expects the name of a python file with the code to be sourced, i.e. loaded and executed.

Example: matplotlib graph of two curves

The file <GiDdirectory>/scripts/tohil/doc/demo_matplotlib.py contains a code like the one of the image:

...

This demo file can be sourced ( loaded and executed ) writing this command ( GIDDEFAULT is a Tcl global variable with the path to the GiD directory)
-np- GiD_Python_Source [file join $GIDDEFAULT scripts/tohil/doc/demo_matplotlib.py]
and then this window will appear:

...

Force reload of a Python file

Other Tcl procs related to Python code are GiD_Python_Import_File and GiD_Python_Import.

Our Tcl command GiD_Python_Import (that does tohil::import) will import a module in Python from its tail name without .py extension. The module to import must be found based on the path environment variables.

GiD_Python_Import_File is similar to GiD_Python_Import but expects the full path.

But if we are developing and modify the .py file doing a new import won't refresh the code in the interpreter.

A trick to do this ‘reload’ is to use the Tcl command GiD_Python_Source. Using this, then the new code of the file is used, reloaded, without the need to restart GiD.

In fact it seems that this is similar, in Python, to the function reload of the importlib module:

Code Block
languagepy
import importlib
importlib.reload( module)