Dash DAQ helps you make interactive, modern UI components in Python. In just over 300 lines of code, this app helps you control and read a Pfeiffer vacuum gauge controller.
We used PySerial and an RS232-to-USB cable to interface between Dash DAQ and the MGC4000 controller. But modules like PyVisa can be used as well!
You can try out the demo app online.
The Dash DAQ app
Our first step is to import Plotly’s Dash libraries, as well as some general Python packages:
Note the import of the class
TPG256A from the module
pfeiffer_instruments. We coded this module separately as a Python wrapper around the instrument’s manufacturer-provided communication protocol. It allows us to keep things modular – if a user is writing an app for a two-gauge controller, there is no need to copy and paste the code. Just create two instances of this class.
Creating the layout
As with any Dash app, we create a layout to describe how our app will look. Our app will have two sections: the top section, which resembles a control panel for the gauges, and the bottom section, where the instrument readout will be displayed.
For the first section of the layout, we unpack some information about the instrument’s layout from the class
TPG256A in the module
pfeiffer_instruments. We’ll learn more about the instrument’s layout later. For now, the only thing we need to do is create an instance of this class:
mock argument set to
True. This allows for testing of the app without a live instrument connection. By default, it will create a gauge controller with 4 gauges in millibar, named P1 through P4 . For custom gauge numbers, names, and units, the argument
gauge_dict can be passed to the instance of the class:
Next, we place the instrument inside a virtual instrument rack. This maintains the lab analogy, where several instruments populate the rack. Here, the
instrument_rack is simply a list of instrument classes:
Then each instrument in the rack iteratively calls its method
setup_layout. This will automatically set up the layout from the code in the
pfeiffer_instruments module by returning an HTML layout with the instrument’s specific Dash DAQ components. Components are defined as an attribute of the instrument, to allow more than one layout of the same instrument in future versions.
A list of Dash DAQ Gauge components is created with
self.gauge_list. The Dash DAQ components we will be using in our app layout – PowerButton, StopButton, and Indicator – are defined upon initialization of the instrument class, as
We arrange the components into the HTML layout of the gauge controller, which is returned by the method
The first line of our app layout contains the power button and the custom name of the gauge controller:
The second line of the app layout allows you to textually enter an instrument port to connect with, via the “Connect” button (which is disabled in the mock version). Next to the button is a drop-down combo box that allows you to choose which gauges to read from. The last item on this list is an indicator component indicating if the instrument is in mock mode:
The third line in the layout contains the list of gauges, with various settings including maximum and minimum ranges, display units, log or linear, etc.:
That’s it for the first part of the gauge controller layout!
The second part of the layout is defined in the main app file. A stop button component will start and stop the measurement. This will trigger the method
measure of each instrument for each of their selected parameters in the drop-down menu (e.g., gauge names). It will also turn on a second indicator component to show that the measurements are running:
Beneath, a graph from Dash’s core components displays measurement readouts:
Creating the callbacks
Callbacks for the user’s click on the “Start” button are defined to set the indicator component’s value
The graph from Dash’s core components is updated via a callback triggered by another Dash core component, Interval:
The callback is only effective when the value of Indicator
is_measuring is set to
The gauges are set to the correct value via callbacks which are defined in the class
The callbacks are generated automatically by calling the
generate_callbacks method of the class
This way, each gauge will have a unique identifier generated automatically, and a callback is assigned to each gauge in the layout, without having to copy and paste.
Finally, we load the callbacks into the main app by calling the instrument’s method: