Tip: Use Custom Python Module

The integrated Python has integrated with some modules. But there is always needs to use custom modules. Here is a script to upload the .py file to a table and load it as a module.

Appreciate to share other solutions, eg. a folder to upload custom script?

""" 
The script is attached to a table '_lib' which contains Python module files.

Columns of table "_lib":
    module: text
    file:   file

Upload the Python module file to the table and define a module name.

Use `load(module_name)` to load the module file. The file will be downloaded to
Python running folder, and named as the module name defined in the table.

"""
# Init SeaTable API
from seatable_api import Base, context
server_url = context.server_url
api_token = context.api_token
base = Base(api_token, server_url)
base.auth()

def main():
    Seatable = load('module_name')

    # Code goes here
  
    return None

def load(module, lib = '_lib'):
    import importlib
    mod = base.query(f"SELECT * from {lib} WHERE module = '{module}'")
    url = mod[0]['file'][0]['url']
    base.download_file(url, f"{module}.py")

    return importlib.import_module(module)

main()
1 Like

This is what I do:

docker-compose.yaml
---
services:
  [...]
  python-runner:
    build:
      context: .
      dockerfile: ./Dockerfile.seatable-python-runner
      args:
        # renovate: datasource=docker depName=seatable/seatable-python-runner
        - SEATABLE_PYTHON_RUNNER_VERSION=4.1.2
    container_name: seatable-python-runner
    restart: unless-stopped
    networks:
      - seatable
  [...]
Dockerfile.seatable-python-runner
ARG SEATABLE_PYTHON_RUNNER_VERSION

FROM seatable/seatable-python-runner:${SEATABLE_PYTHON_RUNNER_VERSION}

# install python dns package
RUN pip install module1 module2

Every runner will have all the packages I need.

I update dependencies with renovate and could even check a requirements file like that.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.