How to Stream (Push) Data to a Real-Time Dashboard API in Python
By: Initial State
Tags: API, Python, Data Science, Dashboard, Web Development

If you are building projects in Python or using Python for data analysis, there is a good chance you will want to capture different types of data. You might even want to create meaningful data visualizations out of those data sets. For example, a dashboard of performance data (CPU/memory/network usage). Or, a dashboard full of sensor data (temperature/motion/contact/energy). Maybe a dashboard displaying the status of critical processes (running/exited). If it is data you can access in your Python code, you can easily build a real-time or IoT dashboard from that data.

This tutorial will show you three different ways to use the basics of Python to stream/push different types of data to a real-time dashboard API. While this tutorial will leverage Initial State’s API as an example, you can apply the principles in this tutorial to interface similar APIs.

Background Info: Initial State’s Events API

Initial State is a streaming service that allows you to visualize and analyze data in your web browser. It’s a powerful tool to prepare data for analysis by putting it in a well-designed dashboard. You can enjoy a 14-day free trial to experience all features of the platform.

Interfacing the Initial State Events API is quite simple, which makes it a good API to use as an example. The basic concept of streaming data in Initial State is as follows:

When you create an Initial State account, you get a private account streaming access key (e.g. ist_TS0QPEcCQ8CLiahhFmB5EbEzPrCZACdr).

Data is streamed into a data bucket (Initial State’s organizational terminology … more info), which has a bucket key you specify when you create the bucket (e.g. abc123).

These two keys give you the ability to write data into that specific data bucket through Initial State’s Events API at any time. That means you can start/stop/append data from any number of applications as long as you have these two keys.

These two keys do NOT give you the right to read data for security reasons.

A data bucket can contain any number of data streams — a collection of time-series data points with a common name (e.g. temperature).

A dashboard is automatically created for you in your Initial State account when you create a data bucket and send data into that bucket.

You simply need two keys, some data, and knowledge of how to use the Initial State Events API to create a live updating, customizable dashboard.

Method 1: A Python Module

Once you have settled on a data service, the next thing you should do is check for an existing Python module you can use to interface their API. There’s a good chance a well-written, well-maintained module will contain several goodies that will save you lots of time and lines of code.

The Initial State Python Data Streamer module is a good example of what you want to find (github repo and documentation). You don’t have to look at any API documentation to use the Initial State events API if you utilize this module. One line of code will create/append to a data bucket and one line of code will push your data to that destination.

To install the ISStreamer module on a Unix based system:

sudo pip install ISStreamer

Below is an example script that uses the ISStreamer module to create a data bucket and send the number 7 into the data stream myNumber. You simply need to place your account access key on line 3 before running.

You can modify the script to stream different numbers into the myNumber data stream or create additional data streams such as

streamer.log("myMessage", "hi")

streamer.log("myLocation", "44.5013406,-88.0644023")

Log into your Initial State account and click on the newly created data bucket to watch the dashboard update as you run your script.