Update utils.py
Browse files
utils.py
CHANGED
@@ -37,4 +37,39 @@ def local_css(css_path):
|
|
37 |
|
38 |
|
39 |
def remote_css(css_url):
|
40 |
-
st.markdown(f'<link href="{css_url}" rel="stylesheet">', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
|
39 |
def remote_css(css_url):
|
40 |
+
st.markdown(f'<link href="{css_url}" rel="stylesheet">', unsafe_allow_html=True)
|
41 |
+
|
42 |
+
"""Basic utilities module"""
|
43 |
+
import requests
|
44 |
+
import csv
|
45 |
+
import re
|
46 |
+
|
47 |
+
|
48 |
+
def request_ct(url):
|
49 |
+
"""Performs a get request that provides a (somewhat) useful error message."""
|
50 |
+
try:
|
51 |
+
response = requests.get(url)
|
52 |
+
except ImportError:
|
53 |
+
raise ImportError(
|
54 |
+
"Couldn't retrieve the data, check your search expression or try again later."
|
55 |
+
)
|
56 |
+
else:
|
57 |
+
return response
|
58 |
+
|
59 |
+
|
60 |
+
def json_handler(url):
|
61 |
+
"""Returns request in JSON (dict) format"""
|
62 |
+
return request_ct(url).json()
|
63 |
+
|
64 |
+
|
65 |
+
def csv_handler(url):
|
66 |
+
"""Returns request in CSV (list of records) format"""
|
67 |
+
|
68 |
+
response = request_ct(url)
|
69 |
+
decoded_content = response.content.decode("utf-8")
|
70 |
+
|
71 |
+
split_by_blank = re.split(r"\n\s*\n", decoded_content) # Extracts header info
|
72 |
+
cr = csv.reader(split_by_blank[1].splitlines(), delimiter=",")
|
73 |
+
records = list(cr)
|
74 |
+
|
75 |
+
return records
|