Debug Macros in Terminal - Amourspirit/live-libreoffice-python GitHub Wiki

Debug Macros

Debugging using PDB

pdb is built into python. This makes it handy to access.

In you macro code simply set a break point using the breakpoint() method.

Getting Started

Open Macro

Open the your macro up in VS Code Editor and set a break point.

A break point is set by calling breakpoint() in the code editor at the line you want to break at.

Example macro with breakpoint() set.

def write_hello(*args) -> None:
    try:
        doc = WriteDoc.from_current_doc()
    except Exception as e:
        _ = MsgBox.msgbox(f"This method requires a Writer document.\n{e}")
        return
    breakpoint()
    cursor = doc.get_cursor()
    cursor.goto_end()
    al = Alignment().align_center
    ft = Font(size=36, u=True, b=True, color=StandardColor.GREEN_DARK2)
    cursor.append_para(text="Hello World!", styles=[ft, al])
 # ... other code

Load Office Program

First start VS Browser. This can be done by click on VS Browser in the lower right corner of Vs Code.

Start VS Browser

The opened window will look something the following.

VS Browser Loaded for Localhost with LibreOffice Start Toolbar

Next, click a button to open a LibreOffice Program, Writer in this case.

LibreOffice Toolbar with mouse over Writer button

This will start the app in the browser window.

LibreOffice running in Web Browser Window

Note:

When running in Codespace (remote Development Container) the URL to connect to LibreOffice will be different.

To get Remote address:

Find the Forward address in the PORTS of VS Code. Copy the address that for port 3002 and paste it into the browser window.

Getting Port url from VS Code Ports

Remote URL Connection.

LibreOffice running in Remote Connection Web Browser

Load Console

Load the console built into Live LibreOffice Python . In the terminal:

console

This will start a specially configure python console.

Console started in VS Code Terminal

See Also: Console

Import

Import ooodev_ex from macro folder whereas ooodev_ex is the name of the macro file.

>>> from macro import ooodev_ex

Run the macro with the breakpoint set. The console will stop at the breakpoint.

>>> ooodev_ex.write_hello()
[6] > /workspace/libreoffice_python_app/macro/ooodev_ex.py(40)write_hello()
-> cursor = doc.get_cursor()
(Pdb++)

Optionally sticky can be turned on to see where the code is at in the editor.

sticky is a feature of pdbpp which is installed in the container.

(Pdb++) sticky

This will give a display similar to the following.

PDB Plus Plus Stick turned on

Navigation

At the prompt you can type n to go to the next line.

(Pdb++) n

image

At the prompt you can type c to continue the code execution.

(Pdb++) c

For more options you can type help at the prompt.

image

There are many commands that can be used with pdb. See the pdb documentation for more information.

Summary

This is a quick overview of how to use the built in debugger in python. There are many more features that can be used. See the pdb documentation for more information.

A better option is to Debug Macros in Vs Code in most cases.

See Also

⚠️ **GitHub.com Fallback** ⚠️