Is there a way to install weasyprint and use it in python from Arduino AppLab?
I'd like to install it with the classic python command: python3 -m pip install weasyprint
How can I do it?
Is there a way to install weasyprint and use it in python from Arduino AppLab?
I'd like to install it with the classic python command: python3 -m pip install weasyprint
How can I do it?
Hi @jekychan. It is possible to install arbitrary Python packages inside the Docker container in which the Python script component of the Arduino App runs:
https://docs.arduino.cc/software/app-lab/apps/develop-apps/#use-python-packages
So, if I wanted to install the weasyprint package in the container of my App, I would create a new file named requirements.txt inside the python subfolder of my App:
SomeApp/
├── app.yaml
├── python/
│ ├── main.py
│ ├── requirements.txt
│ ...
...
You can add new files to an App by following the instructions here:
https://docs.arduino.cc/software/app-lab/apps/develop-apps/#add-files-or-folders
I would then open that python/requirements.txt file in the Arduino App Lab editor, and add the following content to the file:
weasyprint
requirements.txt is a standard Python requirements file:
Unfortunately some Python packages have dependencies on additional non-Python software components being available on the machine. In this case you might find that installation of the package fails. The reason is that, in order to avoid wasting computation resources, the App uses a very minimal container.
Sadly, this is the case with the weasyprint package. If you try to run the App, you will see something like this in the "Python" tab of the Arduino App Lab console panel:
======== App is starting ============================
-----
WeasyPrint could not import some external libraries. Please carefully follow the installation steps before reporting an issue:
https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#installation
https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#troubleshooting
-----
Traceback (most recent call last):
File "/app/python/main.py", line 7, in <module>
from weasyprint import HTML
File "/app/.cache/.venv/lib/python3.13/site-packages/weasyprint/__init__.py", line 372, in <module>
from .css import preprocess_stylesheet # noqa: I001, E402
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.cache/.venv/lib/python3.13/site-packages/weasyprint/css/__init__.py", line 29, in <module>
from ..text.fonts import FontConfiguration
File "/app/.cache/.venv/lib/python3.13/site-packages/weasyprint/text/fonts.py", line 17, in <module>
from .constants import ( # isort:skip
CAPS_KEYS, EAST_ASIAN_KEYS, FONTCONFIG_STRETCH, FONTCONFIG_STYLE, FONTCONFIG_WEIGHT,
LIGATURE_KEYS, NUMERIC_KEYS, PANGO_STRETCH, PANGO_STYLE, PANGO_VARIANT)
File "/app/.cache/.venv/lib/python3.13/site-packages/weasyprint/text/constants.py", line 5, in <module>
from .ffi import pango
File "/app/.cache/.venv/lib/python3.13/site-packages/weasyprint/text/ffi.py", line 479, in <module>
pango = _dlopen(
ffi, 'libpango-1.0-0', 'pango-1.0-0', 'pango-1.0', 'libpango-1.0.so.0',
'libpango-1.0.dylib', 'libpango-1.0-0.dll')
File "/app/.cache/.venv/lib/python3.13/site-packages/weasyprint/text/ffi.py", line 464, in _dlopen
return ffi.dlopen(names[0], flags) # pragma: no cover
~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "/app/.cache/.venv/lib/python3.13/site-packages/cffi/api.py", line 150, in dlopen
lib, function_cache = _make_ffi_library(self, name, flags)
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
File "/app/.cache/.venv/lib/python3.13/site-packages/cffi/api.py", line 834, in _make_ffi_library
backendlib = _load_backend_lib(backend, libname, flags)
File "/app/.cache/.venv/lib/python3.13/site-packages/cffi/api.py", line 829, in _load_backend_lib
raise OSError(msg)
OSError: cannot load library 'libpango-1.0-0': libpango-1.0-0: cannot open shared object file: No such file or directory. Additionally, ctypes.util.find_library() did not manage to locate a library called 'libpango-1.0-0'
exited with code 1
If you were installing the package on the primary machine of your PC, this would be trivial to solve. It is more challenging in this case where you are working inside a container.
You could manually install the Gnome Pango library in the container. However, the problem would come back any time a fresh container was generated for the App. So this is not a good solution.
I think the more promising approach would be to use the new "custom Bricks" feature to install and use the weasyprint Python package in a separate container which is built from an image configured to provide the Pango dependency.
There is work in progress to publish documentation of the custom Bricks feature. For now, you can learn about it by reading the draft version of those docs here:
Although it is definitely valuable to learn about installing arbitrary Python packages in general regardless. I do think it would be worth also examining what is your actual goal.
It might be that this is an "XY problem" situation, where there is a better way to accomplish your actual goal (the "Y") than the challenging arbitrary solution of using the weasyprint package on which you have focused (the "X").
Please provide a detailed description of what you are trying to accomplish.
I need a tool in python that can create pdf with a good design, preview, and so on, as like weasyprint is doing. Is there any other tool like this one, suitable to work with applab?
This doesn't provide enough context to be useful. Please provide a detailed description of how you want to use a PDF generator in the Arduino App as a whole.
I have an update on this subject. The documentation has now been completed and published:
https://docs.arduino.cc/software/app-lab/bricks/custom-bricks/