summaryrefslogtreecommitdiff
path: root/silx/sx/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'silx/sx/__init__.py')
-rw-r--r--silx/sx/__init__.py70
1 files changed, 46 insertions, 24 deletions
diff --git a/silx/sx/__init__.py b/silx/sx/__init__.py
index bdec6e6..e3641c8 100644
--- a/silx/sx/__init__.py
+++ b/silx/sx/__init__.py
@@ -28,11 +28,14 @@ It loads the main features of silx and provides high-level functions.
>>> from silx import sx
When used in an interpreter is sets-up Qt and loads some silx widgets.
-When used in a `jupyter <https://jupyter.org/>`_ /
-`IPython <https://ipython.org/>`_ notebook, neither Qt nor silx widgets are loaded.
+In a `jupyter <https://jupyter.org/>`_ / `IPython <https://ipython.org/>`_
+notebook, to set-up Qt and loads silx widgets, you must then call:
+
+>>> sx.enable_gui()
When used in `IPython <https://ipython.org/>`_, it also runs ``%pylab``,
-thus importing `numpy <http://www.numpy.org/>`_ and `matplotlib <https://matplotlib.org/>`_.
+thus importing `numpy <http://www.numpy.org/>`_ and
+`matplotlib <https://matplotlib.org/>`_.
"""
@@ -43,6 +46,7 @@ __date__ = "16/01/2017"
import logging as _logging
import sys as _sys
+import os as _os
_logger = _logging.getLogger(__name__)
@@ -52,6 +56,9 @@ _logger = _logging.getLogger(__name__)
if hasattr(_sys, 'ps1'):
_logging.basicConfig()
+# Probe DISPLAY available on linux
+_NO_DISPLAY = _sys.platform.startswith('linux') and not _os.environ.get('DISPLAY')
+
# Probe ipython
try:
from IPython import get_ipython as _get_ipython
@@ -68,46 +75,61 @@ else:
_IS_NOTEBOOK = False
-# Load Qt and widgets only if running from console
-if _IS_NOTEBOOK:
- _logger.warning(
- 'Not loading silx.gui features: Running from the notebook')
+def enable_gui():
+ """Populate silx.sx module with silx.gui features and initialise Qt"""
+ if _NO_DISPLAY: # Missing DISPLAY under linux
+ _logger.warning(
+ 'Not loading silx.gui features: No DISPLAY available')
+ return
-else:
- from silx.gui import qt
+ global qt, qapp
+
+ if _IS_NOTEBOOK:
+ _get_ipython().enable_pylab(gui='qt', import_all=False)
- if hasattr(_sys, 'ps1'): # If from console, make sure QApplication runs
- qapp = qt.QApplication.instance() or qt.QApplication([])
+ from silx.gui import qt
+ qapp = qt.QApplication.instance() or qt.QApplication([])
+ if hasattr(_sys, 'ps1'): # If from console, change windows icon
# Change windows default icon
- from silx.gui import icons as _icons
- qapp.setWindowIcon(_icons.getQIcon('silx'))
- del _icons # clean-up namespace
+ from silx.gui import icons
+ qapp.setWindowIcon(icons.getQIcon('silx'))
+
+ global ImageView, PlotWidget, PlotWindow, Plot1D
+ global Plot2D, StackView, ScatterView, TickMode
+ from silx.gui.plot import (ImageView, PlotWidget, PlotWindow, Plot1D,
+ Plot2D, StackView, ScatterView, TickMode) # noqa
- from silx.gui.plot import * # noqa
- from ._plot import plot, imshow, ginput # noqa
+ global plot, imshow, scatter, ginput
+ from ._plot import plot, imshow, scatter, ginput # noqa
try:
- import OpenGL as _OpenGL
+ import OpenGL
except ImportError:
_logger.warning(
'Not loading silx.gui.plot3d features: PyOpenGL is not installed')
else:
- del _OpenGL # clean-up namespace
+ global contour3d, points3d
from ._plot3d import contour3d, points3d # noqa
+# Load Qt and widgets only if running from console and display available
+if _IS_NOTEBOOK:
+ _logger.warning(
+ 'Not loading silx.gui features: Running from the notebook')
+else:
+ enable_gui()
+
+
# %pylab
if _get_ipython is not None and _get_ipython() is not None:
- _get_ipython().enable_pylab(gui='inline' if _IS_NOTEBOOK else 'qt',
- import_all=False)
+ if not _NO_DISPLAY: # Not loading pylab without display
+ from IPython.core.pylabtools import import_pylab as _import_pylab
+ _import_pylab(_get_ipython().user_ns, import_all=False)
# Clean-up
-del _sys
-del _get_ipython
-del _IS_NOTEBOOK
-
+del _os
# Load some silx stuff in namespace
from silx import version # noqa