Skip to content

Commit

Permalink
Add callback on click example
Browse files Browse the repository at this point in the history
  • Loading branch information
tboch committed Oct 6, 2017
1 parent c87d009 commit c753aac
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions examples/7_on-click-callback.ipynb
@@ -0,0 +1,114 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1315dd6331db476eb89633478199ff18",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"A Jupyter Widget"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"None \r"
]
}
],
"source": [
"from ipyaladin import Aladin\n",
"\n",
"from ipywidgets import Layout, Box, widgets\n",
"\n",
"aladin = Aladin(layout=Layout(width='70%'), target='M 36', fov=0.3)\n",
"info = widgets.HTML()\n",
"\n",
"\n",
"box_layout = Layout(display='flex',\n",
" flex_flow='row',\n",
" align_items='stretch',\n",
" width='100%')\n",
"box = Box(children=[aladin, info], layout=box_layout)\n",
"box"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import requests\n",
"def process_result(data):\n",
" info.value = ''\n",
" ra = data['ra']\n",
" dec = data['dec']\n",
" radius = min(aladin.fov / 10, 5)\n",
" query = \"\"\"SELECT TOP 1 main_id, ra, dec, DISTANCE(POINT('ICRS', %f, %f), POINT('ICRS', ra, dec)) as d FROM basic\n",
" WHERE CONTAINS(POINT('ICRS', ra, dec), CIRCLE('ICRS', %f, %f, %f))=1\n",
" ORDER BY d ASC\"\"\" % (ra, dec, ra, dec, aladin.fov / 10)\n",
" \n",
" r = requests.get('http://simbad.u-strasbg.fr/simbad/sim-tap/sync', params={'query': query, 'request': 'doQuery', 'lang': 'adql', 'format': 'json', 'phase': 'run'})\n",
" obj_name = ''\n",
" obj_coo = None\n",
" obj_data = r.json()['data']\n",
" if len(obj_data)==0:\n",
" return\n",
" \n",
" obj_name = obj_data[0][0]\n",
" obj_coo = [obj_data[0][1], obj_data[0][2]]\n",
" \n",
" sed_img = '<img src=\"http://cdsportal.u-strasbg.fr/cgi-bin/PhotVizPreview/plot?ra=%f&dec=%f&radius_arcsec=5&w=200&h=150&point_size=4\">' % (obj_coo[0], obj_coo[1])\n",
" info.value = '<h2>%s</h2><br><br>%s' % (obj_name, sed_img)\n",
" \n",
"aladin.add_listener('click', process_result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit c753aac

Please sign in to comment.