Build data apps with Streamlit + ThoughtSpot APIs

I’ve been following the Streamlit framework for a while, since Snowflake announced that they would acquire it to enable data engineers to quick spin up data apps. I decided to play around with it and see how we could leverage the speed of creating an app along with the benefits that ThoughtSpot provides, especially around the ability to use NLP for search terms. 

Streamlit is built in Python. Once you’ve installed the framework, you will want to grab the ThoughtSpot Python library from CodeSpot. This library provides authentication and API wrappers for working with the ThoughtSpot REST endpoints.

# fetch from secrets.toml file
username = st.secrets["thoughtspot"]["ts_username"]
password = st.secrets["thoughtspot"]["ts_password"]
server = st.secrets["thoughtspot"]["ts_server"] 
logging.info(server)  
 
# Auth with ThoughtSpot
ts: ThoughtSpot = ThoughtSpot(server_url=server)
try:
   ts.login(username=username, password=password)
   logging.info("logged into thoughtspot. Hello "+username)
except requests.exceptions.HTTPError as e:
   print(e)
   print(e.response.content)

Once logged in, let’s make a call to searchdata. searchData takes a query string, just like what you would use via the ThoughtSpot UI and pass in a data source id. In my example, I’m using the Sample Retail data set which is included in every ThoughtSpot Free Trial environment.

data = ts.tsrest.searchdata(query_string="[sales] [region]", 
                    data_source_guid="cd252e5c-b552-49a8-821d-3eadaa049cca")

All that is left to do is map the JSON results of the search data call to a pandas DataFrame, then bind it to a streamlit chart. If you prefer to use another charting library like D3, this is certainly possible too. Check out the D3 samples in CodeSpot for a jumpstart.

dframe = pandas.DataFrame(columns=data['columnNames'],data=data['data'])
dframe = pandas.to_numeric(dframe['Total sales'])
st.bar_chart(data=dframe, width=500, height=500, use_container_width=True)

This simple example demonstrates what is possible when you combine Streamlit and ThoughtSpot. I’m really excited to keep digging in and see how I can use this approach to very quickly create data apps. You can grab the sample code and try it out yourself. I’d love to see what you build.