Skip to main content
`Ctrl`+`K`

# `DataFileDialog`

This module contains an `DataFileDialog`.

class DataFileDialog(parent=None)

  Bases: `AbstractDataFileDialog`

  The DataFileDialog class provides a dialog that allow users to select any datasets or groups from an HDF5-like file.

  The DataFileDialog class enables a user to traverse the file system in order to select an HDF5-like file. Then to traverse the file to select an HDF5 node.

  The selected data is any kind of group or dataset. It can be restricted to only existing datasets or only existing groups using `setFilterMode()`. A callback can be defining using `setFilterCallback()` to filter even more data which can be returned.

  Filtering data which can be returned by a DataFileDialog can be done like that:

      # Force to return only a dataset
      dialog = DataFileDialog()
      dialog.setFilterMode(DataFileDialog.FilterMode.ExistingDataset)

      def customFilter(obj):
          if "NX_class" in obj.attrs:
              return obj.attrs["NX_class"] in [b"NXentry", u"NXentry"]
          return False

      # Force to return an NX entry
      dialog = DataFileDialog()
      # 1st, filter out everything which is not a group
      dialog.setFilterMode(DataFileDialog.FilterMode.ExistingGroup)
      # 2nd, check what NX_class is an NXentry
      dialog.setFilterCallback(customFilter)

  Executing a DataFileDialog can be done like that:

      dialog = DataFileDialog()
      result = dialog.exec()
      if result:
          print("Selection:")
          print(dialog.selectedFile())
          print(dialog.selectedUrl())
      else:
          print("Nothing selected")

  If the selection is a dataset you can access to the data using `selectedData()`.

  If the selection is a group or if you want to read the selected object on your own you can use the silx.io API.

      url = dialog.selectedUrl()
      with silx.io.open(url) as data:
          pass

  Or by loading the file first

      url = dialog.selectedDataUrl()
      with silx.io.open(url.file_path()) as h5:
          data = h5[url.data_path()]

  Or by using h5py library

      url = dialog.selectedDataUrl()
      with h5py.File(url.file_path(), mode="r") as h5:
          data = h5[url.data_path()]

  class FilterMode(*values)

    Bases: `Enum`

    This enum is used to indicate what the user may select in the dialog; i.e. what the dialog will return if the user clicks OK.

    AnyNode = 0

      Any existing node from an HDF5-like file.

    ExistingDataset = 1

      An existing HDF5-like dataset.

    ExistingGroup = 2

      An existing HDF5-like group. A file root is a group.

  selectedData()

    Returns the selected data by using the `silx.io.get_data()` API with the selected URL provided by the dialog.

    If the URL identify a group of a file it will raise an exception. For group or file you have to use on your own the API `silx.io.open()`.

    Return type:

      numpy.ndarray

    Raises:

      ValueError – If the URL do not link to a dataset

  setFilterCallback(callback)

    Set the filter callback. This filter is applied only if the filter mode (`filterMode()`) first accepts the selected data.

    It is not supposed to be set while the dialog is being used.

    Parameters:

      callback (callable) – Define a custom function returning a boolean and taking as argument an h5-like node. If the function returns true the dialog can return the associated URL.

  setFilterMode(mode)

    Set the filter mode.

    It is not supposed to be set while the dialog is being used.

    Parameters:

      mode (DataFileDialog.FilterMode) – The new filter.

  fileMode()

    Returns the filter mode.

    Return type:

      DataFileDialog.FilterMode