Export data from Python to Tableau directly
Asked Answered
L

3

10

I have a python code that exports data into a csv, then I load that csv into Tableau. I don't want to load a csv into tableau.

Is there a way I can just take the return value of my python script and put it directly into tableau?

Question is similar to Export data from Python into Tableau using JSON? , but they inquire about Json format and the answer was to use csv. I want to skip csv step if possible. The data im returning will be in a tabular format.

Lectern answered 26/2, 2019 at 14:26 Comment(2)
Why do you want that? CSV is the format for tabular data exchange, simple, compact, text only and accessible from almost any language or software. What other format would you want to use?Paleoclimatology
I want to skip the middle step of writing to csv, then have tableau load it. I dont want it in another format, sorry wasnt clear. I just want a direct connection of python to tableau.Lectern
W
4

Consider using TabPy. It returns values created by scripts as calculated fields within a workbook.

Otherwise, Tableau purely operates as a layer on top of a source of data. There is no such thing, unfortunately, as loading data directly into it.

The way I typically handle jobs like this is to have Python load data directly into a data store of some sort. All new information - or overwritten information - is then viewable with the existing data connection of your Tableau workbook.

A simple way to do this, without having to use a formal database is to consistently load the CSV into the same place with the same name. Then once Tableau is opened with the pre-existing connection, all that is needed is a click of 'refresh.'

I understand that your goal is to have Tableau populate your data directly from Python, and I'm sorry this isn't the answer you probably wanted, but there is some backend work that needs to be done. If done properly, though, the end effect to any user could appear to render directly from Python.

Wonderment answered 26/2, 2019 at 21:50 Comment(2)
Thanks Daniel, So TabPy would allow me to create scripts as calculated fields but i'd have to use a data store aswell? I'll have to read more about TabPy. I'm thinking of just doing the csv thing, I really wanted to skip that middle step but if its easiest, maybe its best.Lectern
I have not tried using TabPy with no pre-existing data in the Tableau workbook. Typically how its used is to call a script using fields in Tableau - when a user wants to do things that Tableau cannot natively do. (Think K means clustering or Correlation.) Its theoretically possible that it could return data to Tableau without having pre-existing data, but yes, well worth looking into. (As an example, my Python scripts output directly to CSV in AWS S3, which is then read by AWS Athena through Tableau. Its very efficient, but somewhat cumbersome if you're just looking for something quick.)Wonderment
S
2

Make sure your current Tableau extract is a "Tableau Data Source" and then use the "Tableau Data Extract Command-Line Utility" to push the data into Tableau. You use os.system or subprocess to call the tableau executable. One caveat is this thing only work on Windows.

https://onlinehelp.tableau.com/current/pro/desktop/en-us/extracting_TDE.htm

Loading a csv file example from the Tableau site:

C:\Program Files\Tableau\Tableau 2019.1\bin>tableau addfiletoextract --server https://our_server_name --username OurServerSignIn --password "OurServerPwd" --project "New Animations" --datasource "CurrentYrOverYrStats" --file "C:\Users\user1\Documents\DataUploadFiles\AprMay.csv"
Soniasonic answered 26/2, 2019 at 16:5 Comment(3)
Hi, i dont want to load a csv, i want to load python data directly.Lectern
If you're using Tableau Server, you'd be better off pushing data to Google Sheets with Python and the Sheets API, then using Tableau to reference that Sheet as a datasource.Detachment
ah, im justing using tableau workbook. There is no way to take an input file of python into tableau?Lectern
C
1

One of the existing options in the Tableau Desktop "Connect" screen is "Web Data Connector," which can connect to any kind of web server and get data through a well-documented protocol. I suspect this would include a web server running on localhost written in Python.

If you want to go this route, it would require either changing your existing Python script to turn it into a web server that transmits the script's output over the web data connection, or (preferably) making some very general Python code that could that could wrap an arbitrary script of any type in this way, exposing its output as a web data connection.

You will, of course, have to leave that Python script running, listening for connections from Tableau.

Cordula answered 25/2, 2020 at 22:5 Comment(1)
i think this is above my technical know-how! but thank you!Lectern

© 2022 - 2024 — McMap. All rights reserved.