File size: 2,482 Bytes
56d7445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""# 🛸 On Click Callback

Once all cells are executed, click anywhere to get the closest star name. Be patient and the associated photometric points appear after a few seconds.

Powered by \
[ipyaladin](https://github.com/cds-astro/ipyaladin) and \
[Panel](https://panel.holoviz.org/index.html).

Check out the [**App Gallery**](./)
"""

import panel as pn
import requests
from ipyaladin import Aladin
from ipywidgets import Layout

pn.extension("ipywidgets", sizing_mode="stretch_width")

LoadingIndicator = "Loading..."

aladin = Aladin(
    layout=Layout(width="100%"),
    height=800,
    target="M 36",
    fov=0.3,
    show_fullscreen_control=False,
)
info = pn.pane.HTML("<h2>Click the widget</h2>")
image = pn.pane.PNG(embed=True, sizing_mode="fixed")
box = pn.Column(aladin, info, image)


def process_result(data: dict) -> None:
    """Process the result of a click event on the Aladin widget.

    Parameters
    ----------
    data : dict
        The data returned by the click event.
    """
    box.loading = True

    info.value = ""
    ra = data["ra"]
    dec = data["dec"]
    query = (
        f"SELECT TOP 1 main_id, ra, dec, DISTANCE(POINT('ICRS', {ra}, {dec}), "
        f"POINT('ICRS', ra, dec)) as d FROM basic "
        f"WHERE CONTAINS(POINT('ICRS', ra, dec), CIRCLE('ICRS', {ra}, {dec}, {aladin.fov.deg / 10}))=1 "
        "ORDER BY d ASC"
    )

    r = requests.get(
        "http://simbad.u-strasbg.fr/simbad/sim-tap/sync",
        params={
            "query": query,
            "request": "doQuery",
            "lang": "adql",
            "format": "json",
            "phase": "run",
        },
    )
    obj_name = ""
    obj_coo = None
    obj_data = r.json()["data"]
    if len(obj_data) == 0:
        return

    obj_name = obj_data[0][0]
    obj_coo = [obj_data[0][1], obj_data[0][2]]

    info.object = f"<h2>{obj_name}</h2><br><br>"
    image.object = f"https://cdsportal.u-strasbg.fr/cgi-bin/PhotVizPreview/plot?ra={obj_coo[0]}&dec={obj_coo[1]}&radius_arcsec=5&w=200&h=150&point_size=4"
    box.loading = False


aladin.set_listener("click", process_result)

pn.template.FastListTemplate(
    site="ipyaladin and Panel",
    site_url="./",
    title="On Click Callback",
    sidebar=[
        pn.pane.PNG(
            "https://avatars.githubusercontent.com/u/26145382?s=200&v=4",
            link_url="https://cds.unistra.fr/",
        ),
        __doc__,
    ],
    main=[box],
    accent="#296bb5",
    main_layout=None,
).servable()