Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
katsar0v committed Oct 26, 2019
0 parents commit 338d702
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
venv
.ipynb_checkpoints
__pycache__
29 changes: 29 additions & 0 deletions README.md
@@ -0,0 +1,29 @@
# Self-updating Voila Dashboard Example

This is an example to create a self-updating dashboard, which gets data from an api interface.

This example contains `requiremements.txt` for dependencies.

## Local Setup

To test locally (with venv):

```
python -m venv venv
source activate venv/bin/activate
pip install -r requirements.txt
gunicorn api:app -b 127.0.0.1:9999 --daemon
voila dashboard.ipynb
```

To test locally (with conda):

```
conda env create self-updating-dashboard
conda activate create self-updating-dashboard
pip install -r requirements.txt
gunicorn api:app -b 127.0.0.1:9999 --daemon
voila dashboard.ipynb
```

This will open a new browser tab at http://localhost:8866 serving the dashboard.
20 changes: 20 additions & 0 deletions api.py
@@ -0,0 +1,20 @@
# Let's get this party started!
import falcon
import json
import random
import numpy


# Falcon follows the REST architectural style, meaning (among
# other things) that you think in terms of resources and state
# transitions, which map to HTTP verbs.
class SampleAPI(object):
def on_get(self, req, resp):
"""Handles GET requests"""
resp.status = falcon.HTTP_200
resp.content_type = 'application/json'
resp.body = json.dumps([numpy.random.normal(size = 2).tolist() for x in range(random.randint(100,1000))])

app = falcon.API()

app.add_route('/api', SampleAPI())
113 changes: 113 additions & 0 deletions dashboard.ipynb
@@ -0,0 +1,113 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 0,
"metadata": {},
"outputs": [],
"source": [
"import asyncio\n",
"\n",
"%matplotlib widget\n",
"import ipympl\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"import requests\n",
"import json\n",
"\n",
"\n",
"SLEEP = 0.5"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {},
"outputs": [],
"source": [
"def get_api_data():\n",
" try:\n",
" r = requests.get(\"http://127.0.0.1:9999/api\")\n",
" return json.loads(r.text)\n",
" except:\n",
" raise Exception(\"Could not connect to the API server!\")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2ff4c6ed411c49cf9e09b847412025ac",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<Task pending coro=<do() running at <ipython-input-4-27831e97e6cd>:12>>"
]
},
"execution_count": 0,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"all_tasks = asyncio.all_tasks()\n",
"for task in all_tasks:\n",
" task.cancel()\n",
"\n",
"\n",
"fig1, axes1 = plt.subplots()\n",
"fig1.suptitle(\"Self-updating API data\", fontsize=20)\n",
"plt.show(fig1)\n",
"axes1.cla()\n",
"\n",
"\n",
"async def do():\n",
" while True:\n",
" api_data = get_api_data()\n",
" data1 = pd.DataFrame(api_data, columns=['horizontal','vertical'])\n",
" axes1.cla()\n",
" data1.plot(kind=\"scatter\", x='horizontal', y='vertical', ax=axes1)\n",
" plt.draw()\n",
" await asyncio.sleep(SLEEP)\n",
"\n",
"asyncio.create_task(do())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
7 changes: 7 additions & 0 deletions requirements.txt
@@ -0,0 +1,7 @@
jupyterlab
ipympl
pandas
numpy
falcon
gunicorn
requests

0 comments on commit 338d702

Please sign in to comment.