Use Python scripts in your flow (2024)

Disclaimer:This topic includes information about a third-party product. Please note that while we make every effort to keep references to third-party content accurate, the information we provide here might change without notice as python changes. For the most up-to-date information, please consult the python documentation and support.

Python is a widely used high-level programming language for general-purpose programming. By sending Python commands to an external service through Tableau Prep Builder, you can easily extend your data preparation options by performing actions like adding row numbers, ranking fields, filling down fields and performing other cleaning operations that you might otherwise do using calculated fields.

To include Python scripts in your flow, you need to configure a connection between Tableau and a TabPy server. Then you can use Python scripts to apply supported functions to data from your flow using a pandas dataframe. When you add a script step to your flow and specify the configuration details, file, and function that you want to use, data is securely passed to the TabPy server, the expressions in the script are applied, and the results are returned as a table that you can clean or output as needed.

You can run flows that include script steps in Tableau Server as long as you have configured a connection to your TabPy server. Running flows with script steps in Tableau Cloud, isn't currently supported. To configure TableauServer, see Configure the Tableau Python (TabPy) server for Tableau Server.

For information about how to configure sites on Tableau Server with analytics extensions for workbooks, see Configure Connections with Analytics Extensions.

Prerequisites

To include Python scripts in your flow, complete the following setup. Creating or running flows with script steps in Tableau Cloud isn't currently supported.

  1. Download and install Python(Link opens in a new window). Download and install the most current version of Python for Linux, Mac or Windows.

  2. Download and install the Tableau Python server (TabPy(Link opens in a new window)). Follow the installation and configuration instructions for installing TabPy. Tableau Prep Builder uses TabPy to pass data from your flow through TabPy as the input, applies your script, then returns the results back to the flow.

  3. Install Pandas. Run pip3 install pandas. You must use a pandas data frame in your scripts to integrate with Tableau Prep Builder.

Configure the Tableau Python (TabPy) server for Tableau Server

If you plan to publish, create, edit, and run flows that include script steps in Tableau Server, you will need to configure a connection between your TabPy server and Tableau Server.

  • Version 2019.3 and later: You can run published flows that include script steps in Tableau Server.
  • Version 2020.4.1 and later: You can create, edit, and run flows that include script steps in Tableau Server.
  • Tableau Cloud: Creating or running flows with script steps isn't currently supported.
  1. Open the TSM command line/shell .
  2. Enter the following commands to set the host address, port values and connect timeout:

    tsm security maestro-tabpy-ssl enable --connection-type {maestro-tabpy-secure/maestro-tabpy} --tabpy-host <TabPy IP address or host name> --tabpy-port <TabPy port> --tabpy-username <TabPy username> --tabpy-password <TabPy password> --tabpy-connect-timeout-ms <TabPy connect timeout>

    • Select {maestro-tabpy-secure} to enable a secure connection or {maestro-tabpy} to enable an unsecured connection.
    • If you select {maestro-tabpy-secure}, specify the certificate file -cf<certificate file path> in the command line.
    • Specify the --tabpy-connect-timeout-ms <TabPy connect timeout> in milliseconds. For example --tabpy-connect-timeout-ms 900000.
  3. To disable the TabPy connection enter the following command

    tsm security maestro-tabpy-ssl disable

Create your python script

When you create your script, include a function that specifies a pandas (pd.DataFrame) as an argument of the function. This will call your data from Tableau Prep Builder. You will also need to return the results in a pandas (pd.DataFrame) using supported data types.

For example to add encoding to a set of fields in a flow, you could write the following script:

def encode(input): le = preprocessing.LabelEncoder() Return pd.DataFrame({ 'Opportunity Number' : input['Opportunity Number'], 'Supplies Subgroup Encoded' : le.fit_transform(input['Supplies Subgroup']), 'Region Encoded' : le.fit_transform(input['Region']), 'Route To Market Encoded' : le.fit_transform(input['Route To Market']), 'Opportunity Result Encoded' : le.fit_transform(input['Opportunity Result']), 'Competitor Type Encoded' : le.fit_transform(input['Competitor Type']), 'Supplies Group Encoded' : le.fit_transform(input['Supplies Group']),})

The following data types are supported:

Data type in Tableau Prep Builder Data type inPython
String Standard UTF-8 string
Decimal Double
Int Integer
Bool Boolean
Date String in ISO_DATE format “YYYY-MM-DD” with optional zone offset. For example, “2011-12-03” is a valid date.
DateTime String in ISO_DATE_TIME format “YYYY-MM-DDT:HH:mm:ss” with optional zone offset. For example, “2011-12-03T10:15:30+01:00” is a vslid date.

Note: Date and DateTime must always be returned as a valid string.

If you want to return different fields than what you input, you'll need to include a get_output_schema function in your script that defines the output and data types. Otherwise, the output will use the fields from the input data, which are taken from the step that is prior to the script step in the flow.

Use the following syntax when specifying the data types for your fields in the get_output_schema:

Function in Python Resulting data type
prep_string() String
prep_decimal() Decimal
prep_int() Integer
prep_bool() Boolean
prep_date() Date
prep_datetime() DateTime

Square brackets can be used to access rows from a DataFrame. Single brackets output a Pandas Series, while a double bracket will output a Pandas DataFrame.

def create_key(df):return pd.DataFrame({"Key": ['12345']})

The following example shows the get_output_schema function added to the field encoding python script:

def get_output_schema(): return pd.DataFrame({ 'Opportunity Number' : prep_int(), 'Supplies Subgroup Encoded' : prep_int(), 'Region Encoded' : prep_int(), 'Route To Market Encoded' : prep_int(), 'Opportunity Result Encoded' : prep_int(), 'Competitor Type Encoded' : prep_int(), 'Supplies Group Encoded' : prep_int()})

Connect to your Tableau Python (TabPy) server

Important: Starting in Tableau Prep Builder version 2020.3.3, you can configure your server connection once from the top Help menu instead of setting up your connection per flow in the Script step by clicking Connect to Tableau Python (TabPy) Server and entering your connection details. You will need to reconfigure your connection using this new menu for any flows that were created in an older version of Tableau Prep Builder that you open in version 2020.3.3.

  1. Select Help > Settings and Performance > Manage Analytics Extension Connection.
  2. In the Select an Analytics Extension drop-down list, select Tableau Python (TabPy) Server.

    Use Python scripts in your flow (1)

  3. Enter your credentials:
    • Port 9004 is the default port for TabPy.
    • If the server requires credentials, enter a username and password.
    • If the server uses SSL encryption, select the Require SSL check box, then click the No custom configuration file specified... link to select a certificate for the connection. This is your SSL server certificate file.

      Note: Tableau Prep Builder doesn't provide a way to test the connection. If there is a problem with the connection an error message shows.

Add a script to your flow

Start your TabPy server then complete the following steps:

Note: TabPy requires tornado package version 5.1.1 to run. If you receive the error 'tornado.web' has no attribute 'asynchronous' when trying to start TabPy, from the command line run pip list to check the version of tornado that was installed. If you have a different version installed, download the tornado package version 5.1.1(Link opens in a new window). Then run pip uninstall tornado to uninstall your current version, then run pip install tornado==5.1.1 to install the required version.

  1. Open Tableau Prep Builder and click the Add connectionUse Python scripts in your flow (2) button.

    In web authoring, from the Home page, click Create > Flow or from the Explore page, click New > Flow. Then click Connect to Data.

  2. From the list of connectors, select the file type or server that hosts your data. If prompted, enter the information needed to sign in and access your data.

  3. Click the plus Use Python scripts in your flow (3) icon, and select Add Script from the context menu.

    Use Python scripts in your flow (4)

  4. In the Script pane, in the Connection type section, select Tableau Python (TabPy) Server.

    Use Python scripts in your flow (5)

  5. In the File Name section, click Browse to select your script file.
  6. Enter the Function Name then press Enter to run your script.

    Use Python scripts in your flow (6)

Use Python scripts in your flow (2024)

FAQs

How do I run a Python script in power automate flow? ›

How to Run a Python Script in Power Automate?
  1. Step 1: Create a New Flow in Power Automate. To create a new flow in Power Automate, follow these steps: ...
  2. Step 2: Choose a Trigger for Your Flow. ...
  3. Step 3: Add an Action to Run a Python Script. ...
  4. Step 4: Configure the Python Script Action. ...
  5. Step 5: Test and Run Your Flow.

How do I run a Python script in Tableau prep? ›

Download and install the Tableau Python server (TabPy(Link opens in a new window)). Follow the installation and configuration instructions for installing TabPy. Tableau Prep Builder uses TabPy to pass data from your flow through TabPy as the input, applies your script, then returns the results back to the flow.

How to run a script in Python? ›

To execute a Python script, first open a terminal, then navigate to the directory where the script is located, and finally, run the script using the 'python' command followed by the script's name.

Can I code Python in Tableau? ›

TabPy framework allows Tableau to remotely execute Python code and saved functions. TabPy (the Tableau Python Server) is an Analytics Extension implementation that expands Tableau's capabilities by allowing users to execute Python scripts and saved functions via Tableau's table calculations.

Can you run a Python script on Microsoft flow? ›

Power Automate online browser version does not have Python script connector, Thus you need to either run your python script in Azure Automation account or in Azure Functions and call it inside Power Automate flow.

How do I run a Python script in automation Anywhere? ›

Running the Python Script in Automation Anywhere V11
  1. Get your Python script ready. Write it in an IDE or text editor, and save it as a . ...
  2. Open the task you want to run the script in. Add the 'Run Script' command to your workflow.
  3. Configure the command. ...
  4. Execute the task.

How do I run a Python script in Azure pipeline? ›

To learn how to install Python on your agent, see UsePythonVersion.
  1. Fork the sample code. Fork the sample Python repository to your GitHub account. ...
  2. Create your pipeline. Go to Azure Pipelines and select Start free. ...
  3. Customize your pipeline. Replace the generated azure-pipelines. ...
  4. Run your pipeline. Save and run your pipeline.
Mar 27, 2024

How do I run a Python script from Azure? ›

To develop Python applications using Azure, you first want to configure your local development environment. Configuration includes creating an Azure account, installing tools for Azure development, and connecting those tools to your Azure account. Developing on Azure requires Python 3.8 or higher.

How do I run a Python script in Azure Data Factory pipeline? ›

Under Factory Resources, select the + icon, and then select Pipeline. In the Properties pane on the right, change the name of the pipeline to Run Python. In the Activities pane, expand Batch Service, and drag the Custom activity to the pipeline designer surface.

What is the simplest way to run a Python script? ›

The most basic and easy way to run a Python script is by using the python command. You need to open a command line and type the word python followed by the path to your script file like this: python first_script.py Hello World! Then you hit the ENTER button from the keyboard, and that's it.

How do I run a Python script from another Python script? ›

Run Python Script From Another Script Using importlib module

While it's typically used for importing modules, we can also use it to execute Python scripts. By importing the script as a module we can call its functions and pass arguments to them.

How can I run a Python script online for free? ›

Paiza.IO is an online coding platform with support for Python and various other programming languages. It provides a simple and intuitive interface, making it easy for users to run and test Python code snippets.

Why use Tableau instead of Python? ›

The integration of Tableau with Python allows users to leverage the strengths of both tools. Python can handle complex data manipulation tasks, statistical analysis, and machine learning models, while Tableau can provide a user-friendly interface for interactive visualizations and sharing insights.

Does Tableau have a Python API? ›

The Tableau Server Client (TSC) is a Python library for the Tableau Server REST API. Using the TSC library, you can manage and change many of the Tableau Server and Tableau Cloud resources programmatically.

Can Python replace Tableau? ›

Introducing PyGWalker: Replace your tableau in Python with an open-source library. Welcome to the cutting edge of data science, where PyGWalker, an innovative Python library, transforms Jupyter Notebooks into dynamic, interactive data visualization studios.

How do I run a script in Power Automate? ›

Run the script through Power Automate

From the main Power Automate page, select My flows. Select My tutorial flow from the list of flows displayed in the My flows tab. This shows the details of the flow we previously created. Select Run.

Can Power Automate work with Python? ›

Power Automate for desktop enables you to automate complex scenarios using scripts in VBScript, JavaScript, PowerShell, and Python.

Can you run code in Power Automate? ›

To find more information regarding running Power Automate as an administrator, go to Run Power Automate with elevated rights. Scripting actions enable you to run blocks of code and implement custom behavior in your desktop flows. As announced on October 2023, VBScript is deprecated from Windows.

How do I automate a script running? ›

Automating Script Execution
  1. Click Start, then Control Panel, and then Administrative Tools.
  2. Open Task Scheduler.
  3. Select Action, and then Create Basic Task.
  4. Enter a task name and an optional description, and then click Next.
  5. In Task Trigger, select a schedule for running the script, and then click Next.

Top Articles
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 5451

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.