Warp10 and streamlit integration?

Two simple questions:

  1. Does Warp10 integrate into streamlit to feed visualisations?
  2. If so, please would you specify how this can be accomplished?

Thanking you in advance.

Best wishes,

Upvotes: -2

Views: 78

Answers (1)

Fabien Tencé
Fabien Tencé

Reputation: 91

There's no direct integration of Warp 10 in streamlit.

Although streamlit can handle any kind of data, it's mainly focused on pandas DataFrame. DataFrames are tables whereas Warp 10 Geo Time Series are time series. So even if Warp 10 was integrated in streamlit, it would require some code to properly format the data for streamlit to give its full potential.

That being said, here is a small example on how to display data stored in Warp 10 with streamlit:

import json
from datetime import datetime, timedelta

import requests
import streamlit as st
from bokeh.palettes import Category10_10 as palette
from bokeh.plotting import figure

# Should be put in a configuration file.
fetch_endpoint = 'http://localhost:8080/api/v0/fetch'
token = 'READ'  # Change that to your actual token


def load_data_as_json(selector, start, end):
    headers = {'X-Warp10-Token': token}
    params = {'selector': selector, 'start': start, 'end': end, 'format': 'json'}
    r = requests.get(fetch_endpoint, params=params, headers=headers)
    return r.text


st.title('Warp 10 Test')

# Input parameters
selector = st.text_input('Selector', value="~streamlit.*{}")
start_date = st.date_input('Start date', value=datetime.now() - timedelta(days=10))
start_time = st.time_input('Start time')
end_date = st.date_input('End date')
end_time = st.time_input('End time')

# Convert datetime.dates and datetime.times to microseconds (default time unit in Warp 10)
start = int(datetime.combine(start_date, start_time).timestamp()) * 1000000
end = int(datetime.combine(end_date, end_time).timestamp()) * 1000000

# Make the query to Warp 10 and get back a json.
json_data = load_data_as_json(selector, start, end)
gtss = json.loads(json_data)

# Iterate through the json and populate a Bokeh graph.
p = figure(title='GTSs', x_axis_label='time', y_axis_label='value')
for gts_index, gts in enumerate(gtss):
    tss = []
    vals = []
    for point in gts['v']:
        tss.append(point[0])
        vals.append(point[-1])
    p.line(x=tss, y=vals, legend_label=gts['c'] + json.dumps(gts['l']), color=palette[gts_index % len(palette)])
st.bokeh_chart(p, use_container_width=True)

# Also display the json.
st.json(json_data)

Upvotes: 1

Related Questions