In this application note, we will look at the new Python automation features introduced in ADS 2024.
A few years ago, a Python add-on for data display was introduced that enabled advanced data processing and visualization. Now in ADS 2024, Python can also be used to automate and remote control the ADS design environment, modify designs etc. For more detailed information, see ADS 2024 release notes.
The code snippets shown in ADS 2024 documentation are not complete, so hopefully this appnote can help to get you started. The example workspace includes Python code that is executed when the workspace is loaded, and demonstrates how to include standard modules and custom modules. It also shows how to add a custom menu item that triggers Python code.
Enable Python menu
To enable the Python menu in ADS main window, start ADS with -python option in the command line.
In that Python menu, “Developer Python Console” will bring up a console window where you can see output from print() statements in Python code. In the examples shown below, we will use that output for testing some simple code.
Execute code when loading a project
Executing Python code when loading a project requires a trick: we first load AEL code, and start the Python code from there.
Loading AEL uses a boot mechanism that we know from ADS design kits: an eesof.lib file in the library directory tells ADS to load an AEL boot script.
In our example workspace, the AEL boot code is placed in /scripting/boot.ael in the workspace directory.
This code is executed whenever ADS loads the workspace with this library. Nothing Python-specific so far, this is the standard boot mechanism for ADS programming language AEL. The next step is to call our Python code from within the AEL boot file.
Invoking Python from boot.ael
The first lines in boot.ael query the ADS workspace path, and set a variable with full path to our AEL and Python scripts. The scripting path is then handed to Python environment, and appended to the python modules path. This allows us to place our custom Python modules in the scripting directory of our ADS workspace.
Next, we use command “python_run” in three individual calls, one after another, to demonstrate that the environment remembers variables defined in earlier python calls. This code has no other function than learning how the python_run() function behaves!
If we have started Developer Python Console from Python menu, we can see the output of the print(a+b) command: 42.
This confirms that subsequent individual calls from AEL work as expected, and we could run Python code line by line. However, there is a better way to execute a Python script: we now run the entire Python script “boot.py” from one single call. This is done using the method described in Keysight documentation, with Python module runpy.
The output that you see in python console above (except for 42) is created by the boot.py code discussed below.
Example boot.py
Python script boot.py is used here to demonstrate a few basic concepts:
The initial print commands show how text can be printed to the Python Developer Console for debugging purposes. One strength of Python is the use of modules, and we load one standard module (os) and one custom module (mymodule). The module path was extended earlier by Python commands in boot.ael, to include the “scripting” directory of our workspace.
Function write_testfile() in mymodule is very simple, it just creates a file in the data directory.
Next, it is shown how to add a menu entry (“Test”) to the ADS main window. The code checks if previous menus with the same name exist, and removes these old entries before adding the new menu item. This way, we avoid duplicate menu entries, and always have the latest code associated with the menu call when re-loading the workspace.
Response to the menu item is implemented by a callback function. When creating a new menu item, a callback function name must be specified. That callback function is executed whenever the new menu item is pressed. In our example, we have defined “callback_menuitem”, which just prints some information to the Python Developer Console.
Example workspace
The example code shown here is included in workspace Pythontest_wrk. The archived workspace can be downloaded here.
To enable the Python menu in ADS main window, including Python Developer Console, start ADS with -python option in the command line.