# Creating applets in ARTIQ --- ### Design applet GUI with Qt Creator - You can install the open source version from https://bit.ly/3s7KzIf - The GUI features can be added using the Design Tab - Save the file as the `.ui` (Qt UI Designer form) type <br /> ![Qt Creator](https://i.imgur.com/AdivNey.png) ---- ### Convert UI file to Python Code with PyQt5 - Ensure PyQt5 is installed, otherwise you can install it with `pip` ```shell= pip install PyQt5 pip install PyQt5-tools ``` - QT Creator allows you to design the UI in a drag and drop fashion and edit the XML associated with the UI file. - To ensure that an applet written in ARTIQ can render this UI, you can convert the UI file to Python code using the following command: ```shell python -m PyQt5.uic.pyuic -o XXX.py XXX.ui or $ pyuic5 -o XXX.py XXX.ui ``` ---- ### Integrating the applet code with an ARTIQ experiment - You can create an ARTIQ experiment with the `setupUi` and `retranslateUi` methods as well as additional methods for your purpose e.g. Part of the code reproduced from the [Trig Experiment](https://github.com/Atomionics/artiq-experiments/blob/main/repository/trig_experiment.py) script: ```python= # Do the applet source code path determination on import. # ARTIQ imports the experiment, then changes the current # directory to the results, then instantiates the experiment. # In Python __file__ is a relative path which is not updated # when the current directory is changed. trig_applet = os.path.abspath(os.path.join(os.path.dirname(__file__), "trig_applet.py")) class TrigExperiment(EnvExperiment): def build(self): ... def run(self): with open(trig_applet) as f: self.ccb.issue( "create_applet", "trig_applet_example", "trig_applet_dataset", code=f.read(), ) dataset = np.linspace(self.start, self.end, self.samples) self.set_dataset("trig_applet_dataset", dataset, broadcast=True) time.sleep(1) self.ccb.issue("disable_applet", "trig_applet_example") app = QtWidgets.QApplication(sys.argv) main_window = QtWidgets.QMainWindow() self.setupUi(main_window) main_window.show() sys.exit(app.exec_()) # From PyQt5 generated Python file def setupUi(self, main_window): ... def updateValues(self): self.start = self.doubleSpinBox_Start.value() self.end = self.doubleSpinBox_End.value() self.samples = self.spinBox_Samples.value() dataset = np.linspace(self.start, self.end, self.samples) self.set_dataset("trig_applet_dataset", dataset, broadcast=True) # From PyQt5 generated Python file def retranslateUi(self, main_window): ... ``` - The actual code for plots to be rendered through a plotting engine like `pyqtgraph` can be placed in the `data_changed` function of the applet Python script e.g. Part of the code reproduced from the [Trig Applet](https://github.com/Atomionics/artiq-experiments/blob/main/repository/trig_applet.py) script: ```python= class CustomApplet(QtWidgets.QMainWindow): def __init__(self, args): super().__init__() self.dataset_name = args.dataset # Called by the simple ARTIQ applet def data_changed(self, data, mods): xs = data[self.dataset_name][1] view = pg.GraphicsLayoutWidget() w1 = view.addPlot() w2 = view.addPlot() w1.addItem(pg.PlotDataItem(xs, np.cos(xs))) w2.addItem(pg.PlotDataItem(xs, np.sin(xs))) self.setCentralWidget(view) ``` ---- ### References 1. [PyQt5 tutorial](https://www.learnpyqt.com/tutorials/first-steps-qt-creator/) 2. [IONTrap- WIPM: Manual for Developers](https://github.com/GuanQunMu/IonTrap-WIPM/blob/master/Manual%20for%20Developers(English%20Version).md)
{"metaMigratedAt":"2023-06-15T18:08:27.962Z","metaMigratedFrom":"Content","title":"Creating applets in ARTIQ","breaks":true,"contributors":"[{\"id\":\"72929e48-20fa-4997-8312-925b44afed85\",\"add\":3920,\"del\":18}]"}
    1190 views