Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor init options #114

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Upgrade Aladin Lite version to 3.5.1-alpha (#96)
- Upgrade Aladin Lite version to 3.5.1-beta
- instantiation options are now directly mirroring those of Aladin-Lite instead of being
hand-picked for the widget. Any option in
https://cds-astro.github.io/aladin-lite/global.html#AladinOptions will be accepted.

## [0.4.0]

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Correspondence table between ipyaladin versions and Aladin Lite versions:

| ipyaladin | Aladin-Lite |
| ---------- | ----------- |
| Unreleased | 3.5.1-alpha |
| Unreleased | 3.5.1-beta |
| 0.4.0 | 3.4.4-beta |
| 0.3.0 | 3.3.3-dev |

Expand Down
Binary file added docs/_static/notebooks_thumbnails/12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
with Path.open("../pyproject.toml", "rb") as config:
toml = tomllib.load(config)
import datetime
from ipyaladin import __version__, Aladin
from ipyaladin import __version__

project = toml["project"]["name"]
author = "Strasbourg Astronomical Date Centre (CDS)"
Expand Down Expand Up @@ -90,6 +90,7 @@

# -- Document Init Options ---------------------------------------------------

"""" # commented out since init_options are now given directly to aladin lite
init_options = Aladin().traits(only_init=True)

with Path.open("user_documentation/init_options.csv", "w"):
Expand All @@ -98,7 +99,7 @@
with Path.open("user_documentation/init_options.csv", "a") as f:
for k, v in init_options.items():
f.write(f"{k};{v.default_value};{v.help}\n")

"""

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
Expand Down
13 changes: 10 additions & 3 deletions docs/user_documentation/user_documentation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ towards ``Aladin Lite``'s own documentation.
Initialization options for the widget
*************************************

The widget is represented by a python class, :py:class:`ipyaladin.widget.Aladin`. This class has arguments
that allow to chose which buttons or functionalities will be available in the widget.
These **cannot** be edited later.
The widget is represented by a python class, :py:class:`ipyaladin.widget.Aladin`. This
class has arguments that allow to chose which buttons or functionalities will be
available in the widget. These **cannot** be edited later.

Here is a list of options that can be given to ``ipyaladin``:

.. the CSV is generated by conf.py, and grabs all traits with only_init=True don't edit it manually!

Expand All @@ -21,9 +23,14 @@ These **cannot** be edited later.
:header: "Argument";"Default Value";"Description"
:delim: ;

*****************************
Setting the view's parameters
*****************************

.. nbgallery::
../_collections/notebooks/02_Base_Commands
../_collections/notebooks/03_Functions
../_collections/notebooks/12_Planetary_surveys

*****************************
Adding elements to the widget
Expand Down
1 change: 1 addition & 0 deletions examples/02_Base_Commands.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
" Marker(\n",
" position=name,\n",
" title=name,\n",
" # the title and description can be written as plain text or as html elements\n",
" description=(\n",
" '<a href=\"https://simbad.cds.unistra.fr/simbad/'\n",
" f'sim-basic?Ident={name}&submit=SIMBAD+search\"> '\n",
Expand Down
40 changes: 2 additions & 38 deletions examples/04_Importing_Tables.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"t = QTable(\n",
" [ra, dec, name, parallax],\n",
" names=(\"ra\", \"dec\", \"name\", \"parallax\"),\n",
" meta={\"name\": \"my sample table\"},\n",
" meta={\"name\": \"my_sample_table\"},\n",
")"
]
},
Expand All @@ -101,43 +101,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.11.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": false,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": false,
"toc_window_display": false
},
"vscode": {
"interpreter": {
"hash": "85bb43f988bdbdc027a50b6d744a62eda8a76617af1f4f9b115d38242716dbac"
}
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}
8 changes: 8 additions & 0 deletions examples/12_Planetary_surveys.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@
")"
]
},
{
"cell_type": "markdown",
"id": "d151ea91",
"metadata": {},
"source": [
"Unzoom and look for the other points."
]
},
{
"cell_type": "markdown",
"id": "4af1422b",
Expand Down
2 changes: 1 addition & 1 deletion js/aladin_lite.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import A from "https://esm.sh/[email protected]alpha";
import A from "https://esm.sh/[email protected]beta";

export default A;
16 changes: 9 additions & 7 deletions js/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,33 @@ import A from "./aladin_lite";

function initAladinLite(model, el) {
setDivNumber(divNumber + 1);
let initFromPython = model.get("_init_options");
let initOptions = {};
model.get("init_options").forEach((option_name) => {
initOptions[snakeCaseToCamelCase(option_name)] = model.get(option_name);
});
for (const key in initFromPython) {
initOptions[snakeCaseToCamelCase(key)] = initFromPython[key];
}

let aladinDiv = document.createElement("div");
aladinDiv.classList.add("aladin-widget");
aladinDiv.style.height = `${initOptions["height"]}px`;
aladinDiv.style.height = `${model.get("_height")}px`;

aladinDiv.id = `aladin-lite-div-${divNumber}`;
let aladin = new A.aladin(aladinDiv, initOptions);
el.appendChild(aladinDiv);

// Set the target again after the initialization to be sure that the target is set
// from icrs coordinates because of the use of gotoObject in the Aladin Lite API
const raDec = initOptions["target"].split(" ");
const raDec = model.get("_target").split(" ");
aladin.gotoRaDec(raDec[0], raDec[1]);

// Set current FoV and WCS
const twoAxisFoV = aladin.getFov();
const twoAxisFoV = { ...aladin.getFov() };
model.set("_fov_xy", {
x: twoAxisFoV[0],
y: twoAxisFoV[1],
});
model.set("_wcs", aladin.getViewWCS());
const wcs = { ...aladin.getViewWCS() };
model.set("_wcs", wcs);
model.set("_is_loaded", true);
model.save_changes();

Expand Down
2 changes: 1 addition & 1 deletion src/ipyaladin/__about__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.4.0"
__aladin_lite_version__ = "3.5.1-alpha"
__aladin_lite_version__ = "3.5.1-beta"
114 changes: 22 additions & 92 deletions src/ipyaladin/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
Unicode,
Bool,
Any,
default,
)

SupportedRegion = Union[
Expand Down Expand Up @@ -131,6 +130,7 @@ class Aladin(anywidget.AnyWidget):
_css: Final = pathlib.Path(__file__).parent / "static" / "widget.css"

# Options for the view initialization
_init_options = traitlets.Dict().tag(sync=True)
_height = Int(400).tag(sync=True, init_option=True)
_target = Unicode(
"0 0",
Expand All @@ -153,91 +153,12 @@ class Aladin(anywidget.AnyWidget):
)
coo_frame = Unicode(
"ICRS",
help="The frame coordinate. Can be either 'ICRS', 'ICRSd', or 'Galactic'.",
help="The frame coordinate. Can be either 'ICRS', 'ICRSd', or 'galactic'.",
).tag(sync=True, init_option=True)
projection = Unicode(
"SIN",
help="The projection for the view. The keywords follow the FITS standard.",
).tag(sync=True, init_option=True)
samp = Bool(False, help="Wether to allow sending data via the SAMP protocol.").tag(
sync=True, init_option=True, only_init=True
)
# Buttons on/off
background_color = Unicode(
"rgb(60, 60, 60)", help="The color behind the surveys in RGB format."
).tag(sync=True, init_option=True, only_init=True)
show_zoom_control = Bool(
False, help="Whether to show the zoom control toolbar."
).tag(sync=True, init_option=True, only_init=True)
show_layers_control = Bool(
True, help="Whether to show the layers control button."
).tag(sync=True, init_option=True, only_init=True)
show_fullscreen_control = Bool(
True, help="Whether to show the fullscreen control toolbar."
).tag(sync=True, init_option=True, only_init=True)
show_simbad_pointer_control = Bool(
True, help="Whether to show the Simbad pointer control toolbar."
).tag(sync=True, init_option=True, only_init=True)
show_settings_control = Bool(
True, help="Whether to show the settings control toolbar."
).tag(sync=True, init_option=True, only_init=True)
show_share_control = Bool(
False, help="Whether to show the share control toolbar."
).tag(sync=True, init_option=True, only_init=True)
show_status_bar = Bool(True, help="Whether to show the status bar.").tag(
sync=True, init_option=True, only_init=True
)
show_frame = Bool(True, help="Whether to show the viewport frame.").tag(
sync=True, init_option=True, only_init=True
)
show_fov = Bool(True, help="Whether to show the field of view indicator.").tag(
sync=True, init_option=True, only_init=True
)
show_coo_location = Bool(True, help="Whether to show the coordinates bar.").tag(
sync=True, init_option=True, only_init=True
)
show_projection_control = Bool(
True, help="Whether to show the coordinate location indicator."
).tag(sync=True, init_option=True, only_init=True)
show_context_menu = Bool(
True, help="Whether the right click should start the contextual menu."
).tag(sync=True, init_option=True, only_init=True)
show_catalog = Bool(True, help="Whether to show the catalog.").tag(
sync=True, init_option=True, only_init=True
)
full_screen = Bool(False, help="Whether to start in full-screen mode.").tag(
sync=True, init_option=True, only_init=True
)
# reticle
show_reticle = Bool(
True, help="Whether to show the reticle in the middle of the view."
).tag(sync=True, init_option=True, only_init=True)
reticle_color = Unicode("rgb(178, 50, 178)", help="The color of the reticle.").tag(
sync=True, init_option=True, only_init=True
)
reticle_size = Int(20, help="Whether to show the reticle in the middle.").tag(
sync=True, init_option=True, only_init=True
)
# grid
show_coo_grid = Bool(
False, help="Whether the coordinates grid should be shown at startup."
).tag(sync=True, init_option=True, only_init=True)
show_coo_grid_control = Bool(
True, help="Whether to show the coordinate grid control toolbar."
).tag(sync=True, init_option=True, only_init=True)
grid_color = Unicode(
"rgb(178, 50, 178)",
help="Color of the grid. Can be specified as a named color "
"(see html named colors), as rgb (ex: 'rgb(178, 50, 178)'), "
"or as a hex color (ex: '#86D6AE').",
).tag(sync=True, init_option=True, only_init=True)
grid_opacity = Float(
0.5, help="Opacity of the grid and labels. It is comprised between 0 and 1."
).tag(sync=True, init_option=True, only_init=True)
grid_options = traitlets.Dict(help="More options for the grid.").tag(
sync=True, init_option=True, only_init=True
)

# Values
_ready = Bool(
False,
Expand Down Expand Up @@ -282,17 +203,26 @@ class Aladin(anywidget.AnyWidget):
"is reduced in size when hidden.",
).tag(sync=True)

init_options = traitlets.List(trait=traitlets.Any()).tag(sync=True)

@default("init_options")
def _init_options(self) -> List[str]:
return list(self.traits(init_option=True))

def __init__(self, *args: any, **kwargs: any) -> None:
super().__init__(*args, **kwargs)
self.height = kwargs.get("height", 400)
self.target = kwargs.get("target", "0 0")
self.fov = kwargs.get("fov", 60.0)
def __init__(self, *args: any, **init_options: any) -> None:
super().__init__(*args, **init_options)
# pop init options of ipywidgets.DOMWidget that would choke ipyaladin
# https://github.com/jupyter-widgets/ipywidgets/blob/main/python/ipywidgets/ipywidgets/widgets/domwidget.py
for key in ["layout", "tabbable", "tooltip"]:
init_options.pop(key, None)
# some init options are properties here
self.height = init_options.get("height", self._height)
self.target = init_options.get("target", self._target)
self.fov = init_options.get("fov", self._fov)
# apply different default options from Aladin-Lite
ipyaladin_default = {
"show_simbad_pointer_control": True,
"show_coo_grid_control": True,
"show_settings_control": True,
"show_context_menu": True,
}
init_options = {**ipyaladin_default, **init_options}
# set the traitlet
self._init_options = init_options
self.on_msg(self._handle_custom_message)

def _handle_custom_message(self, _: any, message: dict, buffers: any) -> None:
Expand Down
Loading