python-3.xtableau-apitabpy

Use user defined python functions. "ModuleNotFoundError", Tableau, Tabpy


I am trying to call a python function using Tableau using the Tabpy interface but I am receiving a "No module named interface_v1" error. The steps I have taken:

  1. Install tabpy using the git instructions here
  2. Defined my function inside a file called interface_v1.py. The function is below
def call_matlab(a):
    return a
  1. Started the tabpy server
  2. Opened a new Tableau workbook and connected to the server
  3. Created a new calculated field with:
SCRIPT_INT("import interface_v1 as inter
return inter.call_matlab(_arg1)", SUM([Body]))

When I try to use the calculated field I receive the above error ModuleNotFoundError in the tabpy server log. I have the workbook and the python file in the same directory, and I also tried to put it in the tabpy\modules\scripts folder.

Note: I am not using tabpy in a virtual environment and I am very new to Tableau so sorry if I am just missing something. Thanks for any help.


Solution

  • Your best solution here is going to be to deploy your 'interface_v1.py' as an endpoint on the tabpy server.

    This has the benefit of being much faster, loaded in memory etc... Otherwise you will need to explore loading the file you reference in the same directory that tabpy is running. I would highly suggest the endpoint.

    The documentation is fairly straight forward.

    Basically you will do the following with your information:

    from tabpy.tabpy_tools.client import Client
    
    client = Client('http://localhost:9004/')
    
    def call_matlab(a):
        return(a)
    client.deploy('call_matlab', call_matlab, 'Pass data to call_matlab')
    

    Then from tableau you should be able to call the named function

    script_int("tabpy.query('call_matlab',_arg1)",[COL_TO_SEND])
    

    Obviously adjusting for your specific use case.