-
Notifications
You must be signed in to change notification settings - Fork 3
/
flipbuilder.py
131 lines (116 loc) · 5.18 KB
/
flipbuilder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import requests
import json
import time
import streamlit as st
import pandas as pd
import plotly.express as px
st.set_page_config(layout="wide")
variable = st.text_input("Enter your date_variable", "day")
SQL_QUERY = """select *, date_trunc('""" + variable + """', block_timestamp) as dayz from flipside_prod_db.aave.liquidations order by block_timestamp desc"""
API_KEY = ""
API_KEY = st.text_input("Enter your API key", API_KEY )
SQL_QUERY = st.text_input("Enter your SQL query", SQL_QUERY)
TTL_MINUTES = 15
st.code(SQL_QUERY)
def create_query():
r = requests.post(
'https://node-api.flipsidecrypto.com/queries',
data=json.dumps({
"sql": SQL_QUERY,
"ttlMinutes": TTL_MINUTES
}),
headers={"Accept": "application/json", "Content-Type": "application/json", "x-api-key": API_KEY},
)
if r.status_code != 200:
raise Exception("Error creating query, got response: " + r.text + "with status code: " + str(r.status_code))
return json.loads(r.text)
def get_query_results(token):
r = requests.get(
'https://node-api.flipsidecrypto.com/queries/' + token,
headers={"Accept": "application/json", "Content-Type": "application/json", "x-api-key": API_KEY}
)
if r.status_code != 200:
raise Exception("Error getting query results, got response: " + r.text + "with status code: " + str(r.status_code))
data = json.loads(r.text)
if data['status'] == 'running':
time.sleep(10)
return get_query_results(token)
return data
# run_query = st.button("Run query")
# if run_query:
query = create_query()
token = query.get('token')
data = get_query_results(token)
# print(data['columnLabels'])
# for row in data['results']:
# print(row)
# return data
# placeholder = st.empty()
# with placeholder:
df = pd.DataFrame(data['results'], columns=data['columnLabels'])
st.write(df.head())
st.write(df.columns)
see_full = st.checkbox("See full data")
if see_full:
st.write(df)
df = df.fillna('null')
chart_type = st.selectbox("Chart type", ["line", "bar", "scatter"])
number_of_y_axis = st.number_input("Number of y values to plot", value=1, min_value=1, max_value=3)
color = st.checkbox("Color sort?")
if number_of_y_axis == 1:
x = st.selectbox("X axis", df.columns, index = 2)
y = st.selectbox("Y axis", df.columns, index = 3)
if color:
color_sort = st.selectbox("Color by", df.columns)
if chart_type == "line":
st.plotly_chart(px.line(df, y =y, x =x, color=color_sort), use_container_width=True)
if chart_type == "bar":
st.plotly_chart(px.bar(df, y =y, x =x, color=color_sort), use_container_width=True)
if chart_type == "scatter":
st.plotly_chart(px.scatter(df, y =y, x =x, color=color_sort), use_container_width=True)
else:
if chart_type == "line":
st.plotly_chart(px.line(df, y =y, x =x), use_container_width=True)
if chart_type == "bar":
st.plotly_chart(px.bar(df, y =y, x =x), use_container_width=True)
if chart_type == "scatter":
st.plotly_chart(px.scatter(df, y =y, x =x), use_container_width=True)
if number_of_y_axis == 2:
x = st.selectbox("X axis", df.columns)
y1 = st.selectbox("Y axis 1", df.columns)
y2 = st.selectbox("Y axis 2", df.columns)
if color:
color_sort = st.selectbox("Color by", df.columns)
if chart_type == "line":
st.plotly_chart(px.line(df, y =[y1, y2], x =x, color=color_sort), use_container_width=True)
if chart_type == "bar":
st.plotly_chart(px.bar(df, y =[y1, y2], x =x, color=color_sort), use_container_width=True)
if chart_type == "scatter":
st.plotly_chart(px.scatter(df, y =[y1, y2], x =x, color=color_sort), use_container_width=True)
else:
if chart_type == "line":
st.plotly_chart(px.line(df, y =[y1, y2], x =x), use_container_width=True)
if chart_type == "bar":
st.plotly_chart(px.bar(df, y =[y1, y2], x =x), use_container_width=True)
if chart_type == "scatter":
st.plotly_chart(px.scatter(df, y =[y1, y2], x =x), use_container_width=True)
if number_of_y_axis == 3:
x = st.selectbox("X axis", df.columns)
y1 = st.selectbox("Y axis 1", df.columns)
y2 = st.selectbox("Y axis 2", df.columns)
y3 = st.selectbox("Y axis 3", df.columns)
if color:
color_sort = st.selectbox("Color by", df.columns)
if chart_type == "line":
st.plotly_chart(px.line(df, y =[y1, y2, y3], x =x, color=color_sort), use_container_width=True)
if chart_type == "bar":
st.plotly_chart(px.bar(df, y =[y1, y2, y3], x =x, color=color_sort), use_container_width=True)
if chart_type == "scatter":
st.plotly_chart(px.scatter(df, y =[y1, y2, y3], x =x, color=color_sort), use_container_width=True)
else:
if chart_type == "line":
st.plotly_chart(px.line(df, y =[y1, y2, y3], x =x), use_container_width=True)
if chart_type == "bar":
st.plotly_chart(px.bar(df, y =[y1, y2, y3], x =x), use_container_width=True)
if chart_type == "scatter":
st.plotly_chart(px.scatter(df, y =[y1, y2, y3], x =x), use_container_width=True)