Skip to content

Commit

Permalink
add rest api and cli examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dankilman committed Feb 7, 2019
1 parent 0541dc8 commit 6e3a1a6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

CHART_ID="chart1"
LAYOUT="Chart: [[data: [[0]], transform: numbers, id: $CHART_ID]]"

start_page()
{
# start the server, providing an initial layout
awe start --open-browser --obj "${LAYOUT}" &

# cleanup to stop server when script is done
local server_pid=$?
trap "kill ${server_pid}" EXIT
}

wait_for_started()
{
for i in $(seq 5); do
echo "waiting for page to start (attempt: $i)"
awe --quiet status && break || sleep 1
done
}

add_data()
{
# add points to chart in a loop
for index in $(seq 1 10); do
echo "index: $index"
# data is a list of entries. in this case, because we are using the numbers transformer,
# each entry is a list of numbers itself.
awe -q call --element-id ${CHART_ID} -m add -k "data: [[$index]]"
sleep 0.3
done
}

main()
{
start_page
wait_for_started
add_data
}

main
27 changes: 27 additions & 0 deletions examples/rest_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import time

from awe import Page, APIClient


def main():
chart_id = 'chart1'

# normally, when using the API client, the page is started elsewhere.
# for the sake of keeping this example simple, the page is started here.
page = Page()
page.new_chart(data=[[0]], transform='numbers', id=chart_id)
page.start()

# create an API client instance
client = APIClient()

# add points to the chart in a loop
for i in range(10):
# data is a list of entries. in this case, because we are using the numbers transformer,
# each entry is a list of numbers itself.
client.call_method(chart_id, 'add', {'data': [[i]]})
time.sleep(1)


if __name__ == '__main__':
main()

0 comments on commit 6e3a1a6

Please sign in to comment.