In the last technology post, I mentioned that I’d be perusing through some of the concepts brought up in the Tableau Coders Initiative. This blog article goes through their first four videos and includes some insights on how/where to go if you’re struggling. I will also provide some additional fun tidbits of knowledge as, I mean, why not?
The Tableau Coders Initiative content can be found here.
Part I – Introduction to Postman
Part one of the TUG series provides some basic information on coding and walks us through the tools that we’ll be using to create the API. I prefer to work with Jupyter Notebooks. I used Jupyter Lab for Python scripting throughout the duration of this four part series.
We walk around the Postman application and get a good feel for how to work with the API. I enjoyed that they brought me back into this application that I have not touched in a while. Postman is a great way to learn the API it also allows for language versatility. That is to say; I can find the necessary request calls that I need from the API before I transition to the programming language of my choice. This eases up our exploration and troubleshooting. For example, if I were building an application that uses this API and was struggling to, say, insert a record, I can more readily isolate the cause of the bug by running the request inside of Postman – if the request fails, I know that it’s a bug within my API call and if it passes, I know to go back and debug on the IDE.
Following along and pausing the video for extended periods of time, I learned how to –
Create a collection and make a post request.
Sign in, configure access, and acquire the necessary credentials to interact with the API.
Create a variable in postman and recognize a successful sign on.
Part II Getting to Know APIs
The second video includes a quick little lesson on naming conventions which had me running over to review some PEP 8 guidelines. What a fun learn, for some reason I had a recollection of camel case being the convention for Python. I was wrong.
We take a detour away from the Tableau API and have a look at a few other APIs that we can explore with Postman. Perusing the GitHub repo is a little intimidating at this point in the series. I found that it was best to work inside of Postman to learn how to work with the API before heading into the world of programming.
There are some nifty APIs out there, one of which helps you yell all the time – another will grab pictures of dogs for you. Check them out as they can help take the edge off when it comes to familiarizing oneself with the Tableau API.
Part III – Access and Add a Project
Part III, I think this has to be my favorite part. Here we explore Python and learn how to run a program and what lists are, and all kinds of fun stuff. To begin this portion, I stopped the video and headed over to their GitHub to clone the repo using git bash. This will allow me to access and work with their code directly on my computer, which isn’t all that great considering that I’m using Jupyter Lab Notebooks which generate ipnyb files instead of py files. This means I can open the python code, but it will not run. If I want to run the file, I must call the python file and run it inside the notebook, like so:
When I run the 03 file, it tells me that there’s no pat_secret to import. So – I had to create a pat_secret.py file, import it, and set it to my secret key value.
I’m kind of an airhead, so I had to make sure it worked.
After that, I updated all the variables which enabled me to connect to the site.
Next, I wanted to list my projects, using the provided xml parsing code – I got the following error:
I hate XML. Fortunately, there’s more than one option to perform the action. I worked with the JSON script to successfully query and add a project using Python and the Tableau API.
Part 4 – TSC vs. Regular Methods.
In part 4 of the series, there’s an investigation and fun exploration underway. We see a side by side look at the Tableau library vs. working without it when using the API. Naturally, the library takes the cake. If you’re unable to install it for some reason, it is possible to work without it. There are some situations where the library can be more cumbersome than working without it – the key here is to get to know it by working with it. Documentation can be found here.
The course leaves us with some very helpful/useful functions that can be used when working with the Tableau API.
What’s the best way to store my API key so that I don’t accidentally share it with the world? The best suggestion that I could find is to setup an environmental variable. Here is how it is done in Windows.
In your Jupyter Notebook, you would import os and call the environmental variable using os.environ[“my_api_key”]. Where, of course, my_api_key is the name of the environmental variable you stored your key in.
I’m looking forward to the next part in this series. It was a great deal of fun learning what can be done with Tableau APIs.
Leave a Reply