TA-Lib Streamlit Deploy Error

Hi @randyzwitch and @juwimana

I’d call it a hack instead of a solution, I hope they’ll find a better way to support it soon. i.e. Heroku has a buildpack option which is a solution for this.

So first of all some information:

  • It’s a docker container where we’re not root (they did a nice job, it’s simple and work quite nice)
  • ta-lib doesn’t have a debian package on the official repo
  • Since we’re not root we can’t simply add a repo, or install a package on the system

In the end we need to follow these steps:

  • Add gcc and make to packages.txt
  • Remove ta-lib from requirements.txt
  • Add requests to requirements.txt
  • Change the code:
    Download ta-lib
    Build it and install to your home directory
    Install python package, pointing to home directory
    Add the library to your code, since python is already running and will not get new library dir

NOTE: The first time will take a few minutes, bc it needs to build the lib

Code:

 import requests
 import os
 import sys
# check if the library folder already exists, to avoid building everytime you load the pahe
if not os.path.isdir("/tmp/ta-lib"):

    # Download ta-lib to disk
    with open("/tmp/ta-lib-0.4.0-src.tar.gz", "wb") as file:
        response = requests.get(
            "http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz"
        )
        file.write(response.content)
    # get our current dir, to configure it back again. Just house keeping
    default_cwd = os.getcwd()
    os.chdir("/tmp")
    # untar
    os.system("tar -zxvf ta-lib-0.4.0-src.tar.gz")
    os.chdir("/tmp/ta-lib")
    # build
    os.system("./configure --prefix=/home/appuser")
    os.system("make")
    # install
    os.system("make install")
    # install python package
    os.system(
        'pip3 install --global-option=build_ext --global-option="-L/home/appuser/lib/" --global-option="-I/home/appuser/include/" ta-lib'
    )
    # back to the cwd
    os.chdir(default_cwd)
    print(os.getcwd())
    sys.stdout.flush()

# add the library to our current environment
from ctypes import *

lib = CDLL("/home/appuser/lib/libta_lib.so.0")
# import library
import talib

# here goes your code

I hope that helps.

Cheers

4 Likes