GETTING STARTED | DATA EXPORT | KNIME ANALYTICS PLATFORM
How to Save Tableau Dashboard as a PNG file via Tableau API & KNIME
A step-by-step tutorial
Introduction
Tableau is a business intelligence tool that enables you to create dashboards. It also allows you to generate PNG, PDF, or CSV files from dashboards via URL requests. As a data analyst, we use the KNIME Analytics Platform for various use cases. In this instance, I will demonstrate how to create a PNG file using the Tableau API within the KNIME ‘Python Source’ node. For someone unfamiliar with KNIME, it can be likened to a virtual programming language that facilitates connecting nodes and business logic, as if you were coding with boxes instead of writing code. You can execute it through the web or schedule it.
Content Insight
- KNIME
- Tableau
- Python
- Business Intelligence
Preparing KNIME & Python Environment
You can download KNIME using the link below. You have the option to either install the software or download a self-extracting archive file:
Before running Python code, you need to install Anaconda, and you can find detailed instructions at this link:
After you have installed KNIME and Anaconda, the first thing you will do is set up Conda settings by going to File > Conda, where you choose the installation folder. Then, click on File > Preferences to create a new environment file named “py3_knime”:
Installed Programs:
Anaconda3–2023.09–0-Windows-x86_64
KNIME Analytics Platform version 4.7.3 for Windows (self extracting archive)
Tableau Desktop 2023.1.7
Tableau Server 2023.1.7
Installing Python Libraries for Tableau API
To run Tableau Python code, you should install two libraries. Simply search for ‘Anaconda’ in the search field, click on ‘Anaconda Prompt,’ and then enter the following script on the screen:
conda activate py3_knime
we are going to install both packages:
After installing both libraries, ‘tableaudocumentapi’ and ‘tableauserverclient,’ you can now run Tableau Python codes. Tableau also allows you to download reports in PNG format via URL if you are already logged in:
Here is code inside of the KNIME Python Source Node:
import tableaudocumentapi
import tableauserverclient as TSC
# Create empty table
output_table = DataFrame()
# Server Settings
TABLEAU_SERVER = "http://domainname"
TABLEAU_SITE_ID = ""
TABLEAU_USER = "user"
TABLEAU_PASSWORD = "password"
def main():
# authentication
tableau_auth = TSC.TableauAuth(username=TABLEAU_USER, password=TABLEAU_PASSWORD, site_id=TABLEAU_SITE_ID)
server = TSC.Server(server_address=TABLEAU_SERVER, use_server_version=True)
pt_name = "project name"
wb_name = "workbook name"
view_name = "view name"
file_name = "D:/test/picture.png"
with server.auth.sign_in_with_personal_access_token(tableau_auth):
all_workbooks = TSC.Pager(server.workbooks.get)
workbook = [
wb
for wb in all_workbooks
if wb.name == wb_name and wb.project_name == pt_name
][0]
server.workbooks.populate_views(workbook)
view = [v for v in workbook.views if v.name == view_name][0]
server.views.populate_image(view)
with open(file_name, "wb") as view_png:
view_png.write(view.image)
main()
You can find more info and code examples here:
If you encounter an error message like this:
“tableauserverclient.server.endpoint.exceptions.EndpointUnavailableError: This endpoint is not available in API version 2.3. Requires 3.4”
You should include “use_server_version=True” in your code
server = TSC.Server(server_address=TABLEAU_SERVER, use_server_version=True)
Portable Environment? Yes!
You can use the KNIME Conda Propagation node to make your environment portable, which is very useful if you want to run this node on a different machine or server:
Citation by KNIME: “This node ensures the existence of a specific configurable Conda environment and propagates the environment to Python, R, or other Conda-aware downstream nodes. This is useful to make workflows that contain Conda-aware nodes more portable by allowing to recreate the Conda environment used on the source machine (for example your personal computer) on the target machine (for example a KNIME Server instance).”
The next step, as shown in the picture above, is to connect two nodes to each other using a variable connection and specify the Python Source Code to utilize the Conda Propagation node settings:
In this article, I have provided an overview of how to use the KNIME & Tableau API to download PNG files. You can also download them as PDF or CSV files. For more details, you can refer to the Tableau API documentation.