Hi all,

I recently added a =PYTHON() Calc function to WriterAgent that lets users
call any version of Numpy, etc. from their spreadsheet cells. The user just
sets a path to an existing venv. It avoids the headaches and crashes caused
by buggy code via cross-process marshalling. A persistent worker process
keeps overhead 0.2 ms using the basic JSON serializer.

4 Common libraries are automatically imported with their standard aliases
(numpy as np, pandas as pd, sympy as sp, and math) to keep the code
compact. For example:

=PYTHON("result = sp.prime(1000)") or =PYTHON("result =
np.polyfit(range(len(data)), data, 2)[0]", A1:A10)

The code has adapted an AST-based sandbox (originally from Hugging Face’s
smolagents) that whitelists allowed modules and blocks dangerous functions.
It currently supports scalar return variables. In the next version it will
support passing a cell reference instead of the Python text to the first
parameter, to share code between cells. LibreOffice already does the work,
there was just a little bug.

Right now the external Python environment cannot read or write to the
LibreOffice document. I am working on a proxy layer so that LLM-generated
Python scripts can call a subset of WriterAgent tools (one domain at a
time), but other ways are possible. Even without the two-way integration,
Numpy, etc. is very useful.

This =PYTHON() functionality started as a side project, but it could be
useful to include out of the box, and for file sharing. The extension also
has a “Run Numpy script” capability that inserts results into Writer or
Calc. That feature can overwrite multiple cells without the user having to
pre-select a range and do Ctrl+Shift+Enter. Currently it only supports one
Python script per app and the UI is very basic ;-)

The Python execution code could be extracted from the rest of the
AI-focused features. This implementation is different from the
LibrePythonista approach because it runs Python in a separate process, the
Calc syntax is simpler, and it provides a sandbox. While there is
marshalling overhead, it should be irrelevant for actual work, and can be
optimized more later.

I also recently added TeX import support via latex2mathml that could also
be useful out of the box. The goal was to expose it to LLMs, since they
know the syntax and can teach users, but TeX import is useful for meatbags
too. Equation editing doesn’t work yet, but there are Python libraries for
the reverse conversion process ;-)

What do you think?

Regards,

-Keith
https://github.com/KeithCu/writeragent

Reply via email to