Install Third-party libraries

Hello :grinning:
With the docker compose, is there any doc talking about installing “Third-party libraries”
how can we install libraries like jinja for example ?
Thanks

1 Like

Hi @Sao,

If you are comfortable with docker, you can make a version of Grist with extra python libraries as follows. Make an empty directory, and in it create a file called Dockerfile containing the following:

FROM gristlabs/grist

RUN \
  apt update && apt install -y openssl && \
  python3 -m pip install Jinja2

Then build a docker image from this using the docker command:

docker build -t paulfitz/grist .

(I’m calling the image paulfitz/grist but you could call it what you like). If all goes well, you should see a lot of lines about installation, including:

Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.1

Edited to add: in newer versions of docker, you may need to explicitly ask to see build steps:

docker build --progress=plain --no-cache -t paulfitz/grist .

Now you can do a quick test of the result by starting a container from your new image:

docker run -p 8484:8484 -it paulfitz/grist

You should see a Welcome to Grist message and other lines about how Grist is set up. Now if you go to your browser and visit:

http://localhost:8484

You should see an empty Grist installation. Click Create Empty Document and stick this formula in a cell:

import jinja2
jinja2.__version__

All going well the cell should show something like 3.1.2 (that’s what it did for me), showing that Jinja is now available.

If you are using docker compose, then all you need to do now is get your image to wherever you are running Grist, and swap it in for gristlabs/grist. Let me know if you need help with that step.

Good luck! :smiley:

2 Likes

There’s a good proposal for a friendlier way to do this: Custom Python packages in Docker image (but currently making your own image is still the only way)

One additional question: once we have added custom python libraries inside the virtualenv, is there a way to expose a custom function so it’s available in formulas without needing to import it ? Thanks :slight_smile:

2 Likes

One way is to write a formula which adds whatever names you want to use (modules, functions, variables, etc.) to the Python builtins module. For example, if I wanted to use the json module a lot without having to re-import it in each formula, I could have one formula which does this:

import json
import builtins
builtins.json = json
return None  # just to make Grist happy that the formula is returning something
2 Likes

Another way to have libraries pre-imported is to add them here:

Credit: @dmitry-grist

1 Like

Hi, @paul-grist. Today I tried to build, push and pull the image. Thank to GPT, I succeed. No more question here. Thanks for the guide. :grinning: :grinning: