Exemple #1
0
    def _start_server(self):
        """Start up a viewing server."""
        # Start viewer server
        # IMPORTANT: if running in an IPython/Jupyter notebook,
        # use the no_ioloop=True option
        from ginga.web.pgw import ipg
        if not self._port:
            import socket
            import errno
            socket.setdefaulttimeout(0.05)
            ports = [
                p for p in range(8800, 9000)
                if socket.socket().connect_ex(('127.0.0.1',
                                               p)) not in (errno.EAGAIN,
                                                           errno.EWOULDBLOCK)
            ]
            self._port = ports[0]

        self._server = ipg.make_server(host=self._host,
                                       port=self._port,
                                       use_opencv=self.use_opencv,
                                       numthreads=self._threads)

        try:
            backend_check = get_ipython().config
        except NameError:
            backend_check = {}
        no_ioloop = False  # ipython terminal
        if 'IPKernelApp' in backend_check:
            no_ioloop = True  # jupyter console and notebook

        self._server.start(no_ioloop=no_ioloop)
Exemple #2
0
    def _start_server(self):
        """Start up a viewing server."""
        # Start viewer server
        # IMPORTANT: if running in an IPython/Jupyter notebook,
        # use the no_ioloop=True option
        from ginga.web.pgw import ipg
        if not self._port:
            import socket
            import errno
            socket.setdefaulttimeout(0.05)
            ports = [p for p in range(8800, 9000) if
                     socket.socket().connect_ex(('127.0.0.1', p)) not in
                     (errno.EAGAIN, errno.EWOULDBLOCK)]
            self._port = ports[0]

        self._server = ipg.make_server(host=self._host,
                                       port=self._port,
                                       use_opencv=self.use_opencv,
                                       numthreads=self._threads)

        try:
            backend_check = get_ipython().config
        except NameError:
            backend_check = []
        no_ioloop = False  # ipython terminal
        if 'IPKernelApp' in backend_check:
            no_ioloop = True  # jupyter console and notebook

        self._server.start(no_ioloop=no_ioloop)
    def __init__(self,
                 display,
                 verbose=False,
                 open=False,
                 host='localhost',
                 port=9914,
                 use_opencv=False,
                 no_ioloop=True,
                 canvas_format='jpeg',
                 *args,
                 **kwargs):
        """
        Initialise a ginga display

        canvas_type  file type for displays ('jpeg': fast; 'png' : better, slow)
        """
        if not DisplayImpl.server:
            DisplayImpl.server = ipg.make_server(host=host,
                                                 port=port,
                                                 use_opencv=use_opencv)
            try:
                DisplayImpl.server.start(no_ioloop=no_ioloop)
            except Exception as e:
                print("Failed to start ginga server on %s:%d : %s" %
                      (host, port, e))

        virtualDevice.DisplayImpl.__init__(self, display, verbose=False)
        self._viewer = DisplayImpl.server.get_viewer(
            str(display.frame) if display else None)
        self._canvas = self._viewer.add_canvas()
        self._canvas.enable_draw(False)
        self._viewer.ipg_parent.pixel_base = 0.0  # 0-indexed coordinates

        global _maskTransparency
        self._maskTransparency = _maskTransparency

        # JPEG is faster, PNG looks better
        canvas_types = (
            'jpeg',
            'png',
        )
        if canvas_format in canvas_types:
            settings = self._viewer.get_settings()
            settings.set(html5_canvas_format=canvas_format)
        else:
            print("Unknown format \"%s\" (allowed: \"%s\")" %
                  (canvas_format, '", "'.join(canvas_types)),
                  file=sys.stderr)
        #
        # This may not be necessary, but the worst case is that the user has to close a tab
        #
        # N.b. the user may well want to call Display.embed() (a ginga specific call)
        #
        if open:
            self._viewer.open()
def initGinga(host='localhost', port=9914, use_opencv=False, no_ioloop=True):
    """
    Initialize ginga
    
    set use_opencv to True if you have a non-buggy python OpenCv bindings--it greatly speeds up some operations
    IMPORTANT: if running in an IPython/Jupyter notebook, use no_ioloop=True
    """

    global server
    if not server:
        server = ipg.make_server(host=host, port=port, use_opencv=use_opencv)
        server.start(no_ioloop=no_ioloop)
Exemple #5
0
    def _start_server(self):
        # Start viewer server
        # IMPORTANT: if running in an IPython/Jupyter notebook, use the no_ioloop=True option
        from ginga.web.pgw import ipg
        if not self._port:
            self._port=9904 #still working on autoport

        self._server = ipg.make_server(host=self._host,
                                 port=self._port,
                                 use_opencv=self.use_opencv,
                                 numthreads=self._threads)

        backend=get_backend().lower()
        if 'nbagg' in backend:
            self._server.start(no_ioloop=True) #assume in notebook
        elif 'tkagg' in backend:
            self._server.start(no_ioloop=False)
        elif "pylab" in backend:
            raise NotImplementedError
        elif "qt4agg" in backend:
            raise NotImplementedError
        else:
            print("not designed to work with {0:s}".format(backend))
            raise NotImplementedError
Exemple #6
0
class ImageViewer(Box):
    """
    Basic image viewer Jupyter widget which wraps a ginga widget.

    Set the content of the image viewer via the ``image`` attribute.

    Parameters
    ----------

    image : str, FITS HDU, or numpy array
        Content to display in the widget

    """
    _server = ipg.make_server(host='localhost',
                                       port=9987,
                                       use_opencv=False)
    _server_started = False
    _number_views = 0

    def __init__(self, image=None, width=600, height=600,
                 show_position=False, **kwd):
        super(ImageViewer, self).__init__(**kwd)

        self._image_display = Output(value="I will have an image soon")


        self.children = [self._image_display]
        if not ImageViewer._server_started:
            ImageViewer._server.start(no_ioloop=True)
            ImageViewer._server_started = True
        #self._server = None
        self._viewer = None
        self._width = width
        self._height = height
        self._show_position = show_position
        if self._show_position:
            self._height += 30
        # This call sets both server and viewer
        self._ginga_viewer()

        width, height = self._viewer.get_window_size()

        with self._image_display:
            display(IFrame(self._viewer.url, width, height))

        self.image = image

    def _ginga_viewer(self):
        # Set this to True if you have a non-buggy python OpenCv
        # bindings--it greatly speeds up some operations
        use_opencv = False

        if self._show_position:
            viewer_class = ipg.EnhancedCanvasView
        else:
            viewer_class = ipg.BasicCanvasView

        ImageViewer._number_views += 1
        self._viewer = self._server.get_viewer(str(ImageViewer._number_views),
                                               width=self._width,
                                               height=self._height,
                                               viewer_class=viewer_class)
        self._viewer.set_zoom_algorithm('rate')
        self._viewer.set_zoomrate(1.01)

    @property
    def image(self):
        return self._image

    @image.setter
    def image(self, value):
        self._image = value
        if value is not None:
            if isinstance(value, str):
                # Hope this is the name of a FITS file!
                self._viewer.load_fits(value)
            elif isinstance(value, HDUList):
                self._viewer.load_hdu(value)
            else:
                # Sure hope this is really numpy data
                self._viewer.load_data(value)
Exemple #7
0
#!/usr/bin/env python
# coding: utf-8

# In[96]:

import ginga
from ginga.util import iqcalc
import numpy as np
from ginga.web.pgw import ipg

# In[100]:

use_opencv = False

server = ipg.make_server(host='localhost', port=8701, use_opencv=use_opencv)
server.start(no_ioloop=True)
v1 = server.get_viewer('v1')
v1.open()
v1.load('bgc.fits')