Skip to content

Instantly share code, notes, and snippets.

@MrMaksimize
Created February 9, 2017 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrMaksimize/ca46df0aa4f21b01ff5b4375ec2affa5 to your computer and use it in GitHub Desktop.
Save MrMaksimize/ca46df0aa4f21b01ff5b4375ec2affa5 to your computer and use it in GitHub Desktop.
Iron-ajax and px-data-table
import sys, os, json
here = os.path.dirname(os.path.realpath(__file__))
#sys.path.append(os.path.join(here, "./vendored"))
env_path = os.path.join(here, "./venv/lib/python2.7/site-packages/")
sys.path.append(env_path)
import requests
from contextlib import closing
from itertools import izip
import csv
def dataset_preview(event, context):
#https://datatables.net/forums/discussion/29260/dynamic-column-headers-via-ajax
url = "http://seshat.datasd.org.s3.amazonaws.com/special_events/special_events_list_datasd.csv"
rows = []
counter = 0
cols = []
#http://stackoverflow.com/questions/4576115/convert-a-list-to-a-dictionary-in-python
with closing(requests.get(url, stream=True)) as r:
reader = csv.reader(r.iter_lines(), delimiter=',', quotechar='"')
for row in reader:
if counter == 0:
cols = row
elif counter == 3:
break
else:
i = iter(cols)
row_dict = dict(izip(i, row))
rows.append(row_dict)
counter = counter + 1
columns = []
for col in cols:
columns.append({"title": col, "data": col})
#resp_body = {"data": rows, "columns": columns}
resp_body = rows
# Access-Control for CORS
# Content-Type for resp type
response = {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json;charset=utf-8"
},
"body": json.dumps(
resp_body, ensure_ascii=True)
}
return response
<iron-ajax
auto
url="https://8kf9rxn2x1.execute-api.us-east-1.amazonaws.com/dev/dataset/preview"
handle-as="json"
last-response="{{jsondata}}"
debounce-duration="300"></iron-ajax>
<px-data-table table-data='[[jsondata]]' striped="true" enable-column-reorder="true" enable-column-resize="true" hide-pagination-control="true"></px-data-table>
<px-data-table table-data='[{"event_url":"www.hillcrestfarmersmarket.com","longitude":"-117.1498828","event_type":"FARMERS","event_end":"2017-12-31 14:00:00","event_title":"2017 Hillcrest Farmers Market (Sundays)","event_desc":"This farmers market in Hillcrest offers an array of groceries and goodies. Featured items include fresh fruits and vegetables, meat, seafood, dairy products, dry goods, prepared foods, and specialty treats. There are also art & craft items, jewelry, and clothing. Come and shop!","exp_attendance":"2,000","event_start":"2017-12-31 09:00:00","event_id":"48856","exp_participants":"400","event_loc":"DMV parking lot at 3690 Normal Street ~ Normal Street between Lincoln Avenue & University Avenue","latitude":"32.7485387","event_address":"3690 Normal Street","event_subtitle":"","event_host":"Hillcrest Business Association"}]' striped="true" enable-column-reorder="true" enable-column-resize="true" hide-pagination-control="true"></px-data-table>
<px-data-table table-data='[{"first":"Valentine","last":"Meyer","email":"valentinemeyer@scentric.com"},{"first":"Silva","last":"Alexander","email":"silvaalexander@gmail.com"},{"first":"Hopkins","last":"Wong","email":"hopkinswong@hotmail.com"}]' striped="true" show-column-chooser="true" enable-column-reorder="true" enable-column-resize="true" hide-pagination-control="true"></px-data-table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment