google/neuroglancer

Access existing viewer from python

Opened this issue · 3 comments

I'd like to be able to work with a Viewer instance, created outside of a Python session, from within Python.

Use-case example:

  1. Start a new Viewer from terminal, outside of Python
> python -i neuroglancer/python/examples/example.py
  1. Copy resulting URL of viewer, e.g. http://127.0.0.1:50277/v/b03e32f0163614d40bfce3bced2ab08789a412ab/
  2. In Python, access the running viewer

I think a nice interface would be something like:

viewer = neuroglancer.Viewer(url='http://127.0.0.1:50277/v/b03e32f0163614d40bfce3bced2ab08789a412ab/')

Or is this already possible?

Thanks!

There is a loop between the python kernel and the web browser.. having two kernels attached to the same viewer would not really conceptually make sense.. who is charge of the state? Where is the source of truth?

I don’t think this is possible… but maybe you could zoom out and explain what you are hoping to accomplish by doing this?

jbms commented

If the goal is to keep the web browser open across multiple restarts of the Python program, you can accomplish that by specifying a fixed port number and a fixed token to the viewer. Then the url will be stable.

Thanks for the feedback! I think I was a bit mistaken about an intended workflow before in thinking that it would be important to have a viewer started apart from python. I no longer think that is the situation, so you can go ahead and close this issue.

However, I do want to provide more context... My overall goal is to wrap neuroglancer into a modular Jupyter Notebook app. I think I've made some good progress on that today!

I'm starting a new viewer instance from python so that the state of the embedded neuroglancer app now can easily be kept in sync with other UI components! For instance, you can see the state properties on the right remain updated as I pan the viewer position.

I think this is a pretty promising approach since it allows for two primary workflows. First, it allows anyone with an existing neuroglancer url to just plop it in to the input field and voila, you have your own viewer based on that url. Alternatively, someone could start by just creating an empty viewer with this app and then programmatically build it up however they want using the app-created viewer (e.g. app.viewer).

Would you guys potentially consider a PR that added a simple app like this to the codebase?

Code:
import panel as pn
import neuroglancer

pn.extension()

class NeuroglancerViewerApp(pn.viewable.Viewer):
    DEMO_URL = 'https://neuroglancer-demo.appspot.com/#!%7B%22dimensions%22:%7B%22x%22:%5B6.000000000000001e-9%2C%22m%22%5D%2C%22y%22:%5B6.000000000000001e-9%2C%22m%22%5D%2C%22z%22:%5B3.0000000000000004e-8%2C%22m%22%5D%7D%2C%22position%22:%5B5029.42333984375%2C6217.5849609375%2C1182.5%5D%2C%22crossSectionScale%22:3.7621853549999242%2C%22projectionOrientation%22:%5B-0.05179581791162491%2C-0.8017329573631287%2C0.0831851214170456%2C-0.5895944833755493%5D%2C%22projectionScale%22:4699.372698097029%2C%22layers%22:%5B%7B%22type%22:%22image%22%2C%22source%22:%22precomputed://gs://neuroglancer-public-data/kasthuri2011/image%22%2C%22tab%22:%22source%22%2C%22name%22:%22original-image%22%7D%2C%7B%22type%22:%22image%22%2C%22source%22:%22precomputed://gs://neuroglancer-public-data/kasthuri2011/image_color_corrected%22%2C%22tab%22:%22source%22%2C%22name%22:%22corrected-image%22%7D%2C%7B%22type%22:%22segmentation%22%2C%22source%22:%22precomputed://gs://neuroglancer-public-data/kasthuri2011/ground_truth%22%2C%22tab%22:%22source%22%2C%22selectedAlpha%22:0.63%2C%22notSelectedAlpha%22:0.14%2C%22segments%22:%5B%223208%22%2C%224901%22%2C%2213%22%2C%224965%22%2C%224651%22%2C%222282%22%2C%223189%22%2C%223758%22%2C%2215%22%2C%224027%22%2C%223228%22%2C%22444%22%2C%223207%22%2C%223224%22%2C%223710%22%5D%2C%22name%22:%22ground_truth%22%7D%5D%2C%22layout%22:%224panel%22%7D'

    def __init__(self, **params):
        super().__init__(**params)
        self.viewer = neuroglancer.Viewer()
        self._setup_ui_components()
        self._configure_viewer()
        self._setup_callbacks()

    def _setup_ui_components(self):
        self.url_input = pn.widgets.TextInput(
            placeholder="Enter a Neuroglancer URL and click Load", name='Input URL', width=700
        )
        self.load_button = pn.widgets.Button(name="Load", button_type="primary", width=75)
        self.demo_button = pn.widgets.Button(name="Demo", button_type="warning", width=75)
        self.json_pane = pn.pane.JSON({}, theme='light', depth=2, name='Viewer State', height=600, width=400)
        self.shareable_url_pane = pn.pane.Markdown("**Shareable URL:**")
        self.local_url_pane = pn.pane.Markdown("**Local URL:**")
        self.iframe = pn.pane.HTML(sizing_mode='stretch_both', min_height=700, min_width=700)

    def _configure_viewer(self):
        self.update_local_url()
        self.update_iframe_with_local_url()

    def _setup_callbacks(self):
        self.load_button.on_click(self._on_load_button_clicked)
        self.demo_button.on_click(self._on_demo_button_clicked)
        self.viewer.shared_state.add_changed_callback(self._on_viewer_state_changed)

    def _on_demo_button_clicked(self, event):
        self.url_input.value = self.DEMO_URL
        self._load_neuroglancer_state_from_url(self.url_input.value)

    def _on_load_button_clicked(self, event):
        self._load_neuroglancer_state_from_url(self.url_input.value)

    def _load_neuroglancer_state_from_url(self, url):
        try:
            new_state = neuroglancer.parse_url(url)
            self.viewer.set_state(new_state)
        except Exception as e:
            print(f"Error loading Neuroglancer state: {e}")

    def _on_viewer_state_changed(self):
        self.update_shareable_url()
        self.update_json_pane()

    def update_shareable_url(self):
        shareable_url = neuroglancer.to_url(self.viewer.state)
        self.shareable_url_pane.object = self._generate_details_markup("Shareable URL", shareable_url)

    def update_local_url(self):
        self.local_url_pane.object = self._generate_details_markup("Local URL", self.viewer.get_viewer_url())

    def update_iframe_with_local_url(self):
        self.iframe.object = f'<iframe src="{self.viewer.get_viewer_url()}" width="700" height="700"></iframe>'

    def update_json_pane(self):
        self.json_pane.object = self.viewer.state.to_json()

    def _generate_details_markup(self, title, url):
        return f"""
            <details>
                <summary><b>{title}:</b></summary>
                <a href="{url}" target="_blank">{url}</a>
            </details>
        """

    def __panel__(self):
        controls_layout = pn.Column(
            pn.Row(self.demo_button, self.load_button),
            pn.Row(self.url_input))
        links_layout = pn.Column(self.local_url_pane, self.shareable_url_pane)
        return pn.Column(
            controls_layout,
            links_layout,
            pn.Row(self.iframe, self.json_pane))

app = NeuroglancerViewerApp()
app.servable()
neuroglancer_app.mov