Adding a File Importer


This section is under development

These are the basic steps to add a new file format importer to NeXpy.

  1. Create a Python source code module named where abcde is the name of the new support and does not conflict with existing names.

  2. Place that file in the source code tree, in path <nexpy>/src/nexpy/readers/

  3. Inside that file, create several required structures as described below.

  4. Create other structure as necessary to support the reader.

  5. Provide an example data file (or files) in the <nexpy>/src/nexpy/examples/ directory and update the README.rst file there, describing the new example(s).


All new file format importers must be placed in the NeXpy source code tree in the readers subdirectory.

Required Content

Start with this basic template:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 4 """describe this importer"""
 6 from import *
 7 from nexpy.gui.importdialog import NXImportDialog
 9 filetype = 'my file format'   # these words go in the import menu
11 class ImportDialog(NXImportDialog):
12     """Dialog to import my file format"""
14     def __init__(self, parent=None):
15         super(ImportDialog, self).__init__(parent)
17         self.accepted = False
18         self.import_file = None     # must set in self.get_data()
20         self.set_layout(self.filebox(), self.progress_layout(save=True))
22         self.set_title(f"Import {filetype}")
24     def get_data(self):
25        """Read the data and return either a NXroot or NXentry group"""
26        # MUST define self.import_file as chosen file name
27        # use convenience method to get from dialog widget
28        self.import_file = self.get_filename()
30        x = range(0,10)     # example data
31        y = range(1,11)
32        return NXroot(NXentry(NXdata(y,x)))

About the GUI layout

Each importer needs to layout the GUI buttons in class ImportDialog(NXImportDialog) necessary for defining the imported file and its attributes and the single module, get_data(), which returns either an NXroot or NXentry object. This will be added to the NeXpy tree.

Features from the Superclass

Three GUI convenience elements are provided from the superclass nexpy.gui.importdialog.NXImportDialog:

ImportDialog.filebox: Contains a “Choose File” button and a text box. Both can be used to set the path to the imported file. This can be retrieved as a string using self.get_filename().

ImportDialog.close_buttons: Contains a “Cancel” and “OK” button to close the dialog. This should be placed at the bottom of all import dialogs.