Debug Macros in Terminal - Amourspirit/live-libreoffice-python GitHub Wiki
pdb is built into python. This makes it handy to access.
In you macro code simply set a break point using the breakpoint()
method.
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
First start VS Browser. This can be done by click on VS Browser in the lower right corner of Vs Code.
The opened window will look something the following.
Next, click a button to open a LibreOffice Program, Writer in this case.
This will start the app in the 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.
Remote URL Connection.
Load the console built into Live LibreOffice Python
.
In the terminal:
console
This will start a specially configure python console.
See Also: Console
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.
At the prompt you can type n
to go to the next line.
(Pdb++) n
At the prompt you can type c
to continue the code execution.
(Pdb++) c
For more options you can type help
at the prompt.
There are many commands that can be used with pdb. See the pdb documentation for more information.
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.