Skip to content

Instantly share code, notes, and snippets.

@nmearl
Last active March 4, 2020 20:32
Show Gist options
  • Save nmearl/0727f1a2ac26f1d6d31f4ca22dda4d49 to your computer and use it in GitHub Desktop.
Save nmearl/0727f1a2ac26f1d6d31f4ca22dda4d49 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Manual Plugin Creation in CubeViz\n",
"\n",
"This demonstrates how to create a plugin ad-hoc and append it directly to the UI. Note that currently we can't use the `registry` infrastructure without another special incantation (i.e. re-calling the configuration setup). This is the simplest and most straightforward, but note that the styling of the new plugin does not match the rest of the application. We'll have to talk to Mario/Maarten about that."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create the cubeviz application. It's rendered empty, so we'll add some data \"for fun\" in the next cell. This isn't strictly necessary, because this example doesn't actually use any of the loaded data."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "cf68f87944c64195b5fc0c10723596a3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Application(components={'g-viewer-area': ViewerArea(items=[Item(horizontal=True, stacks=[Item(stacks=[Item(hor…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from jdaviz.app import Application\n",
"\n",
"app = Application(configuration='cubeviz')\n",
"app"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"app.load_data(\"/Users/nearl/data/cubeviz/MaNGA/manga-7495-12704-LOGCUBE.fits\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functional plugin\n",
"\n",
"This is an example of creating a simple button that calls an \"analysis\" task that could, in practice, be grabbing some data and doing something to it. This is a very functional approach in which we instantiate a button, define a function, and attach the function as the on click callback."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "95198cd819894cb0ae1ecc9ccce9f48e",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Btn(children=[Icon(children=['home'])])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import ipyvuetify as v\n",
"\n",
"def my_analysis_func(*args, **kwargs):\n",
" print(app.session.data_collection[0])\n",
"\n",
"plugin_button = v.Btn(children=[v.Icon(children=['home'])])\n",
"plugin_button.on_event('click', my_analysis_func)\n",
"\n",
"plugin_button"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we attach the plugin to the UI. Essentially, the `Toolbar` component can be accessed through the `app` instance. From there, we just add the tool to the `tools` traitlet, and the UI automatically updates to include it. (Again, the styling is another issue we'll have to deal with later.)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"toolbar = app.components.get('g-default-toolbar')\n",
"toolbar.tools = toolbar.tools + [plugin_button]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Object-oriented plugin\n",
"\n",
"Below produces the same effect as above, but does so using inheritance. This would be a \"next step\" toward making a \"real\" plugin that gets installed like the Gaussian Smoooth one that exists now. Note that for the step after this one, we'd have to allow `MyAnalysisTool` to accept a `session` argument. It does *not* need to inherit from `TemplateMixin` or use the template format, it can stay a strict widget subclass as long as it can take a session object on instantiation."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"class MyAnalysisTool(v.Btn):\n",
" def __init__(self, *args, **kwargs):\n",
" super().__init__(*args, **kwargs)\n",
" \n",
" self.on_event('click', self.clicked)\n",
" self.children = [v.Icon(children=['home'])]\n",
" \n",
" def clicked(self, *args, **kwargs):\n",
" print(\"CLICKED\")\n",
" \n",
"new_butt = MyAnalysisTool()\n",
"new_butt"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"toolbar.tools = toolbar.tools + [MyAnalysisTool()]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment