Sample Usage¶
ONCatLogin Widget¶
The following is a simple example of how to use the ONCatLogin widget in a PyQt application.
This example creates a main window with an ONCatLogin widget and two QListWidgets to display
the instrument lists for the SNS and HFIR facilities. The instrument lists are updated when
the connection status changes.
The only required argument for the ONCatLogin widget is the client ID. The client ID is a unique identifier
for the application that is used to authenticate with the ONCat server. The client ID is provided by the ONCat support team
and should exist in pyoncatqt configuration file.
Authentication uses a browser-based sign-in (the OAuth Device Authorization Grant). When the user clicks the
login button, the widget starts an interactive sign-in on a worker thread and shows a small dialog with a link.
The user opens that link in a browser and approves the sign-in with their ORNL credentials. No username or
password is entered inside the application. Once the sign-in resolves, the widget refreshes its connection
status and emits connection_updated.
import sys
from qtpy.QtWidgets import QApplication, QLabel, QListWidget, QVBoxLayout, QWidget
from pyoncatqt.login import ONCatLogin
class MainWindow(QWidget):
"""Main widget"""
def __init__(self: QWidget, key: str = "client", parent: QWidget = None) -> None:
super().__init__(parent=parent)
layout = QVBoxLayout()
# Create and add the Oncat widget
self.oncat_widget = ONCatLogin(key=key, parent=self)
self.oncat_widget.connection_updated.connect(self.update_instrument_lists)
layout.addWidget(self.oncat_widget)
# Add text input boxes for wavelength and run number
self.sns_list = QListWidget()
self.hfir_list = QListWidget()
layout.addWidget(QLabel("SNS Instruments:"))
layout.addWidget(self.sns_list)
layout.addWidget(QLabel("HFIR Instruments:"))
layout.addWidget(self.hfir_list)
self.setLayout(layout)
self.setWindowTitle("ONCat Application")
self.oncat_widget.update_connection_status()
def update_instrument_lists(self: QWidget, is_connected: bool) -> None:
"""Update the contents of the instrument lists based on the connection status."""
self.sns_list.clear()
self.hfir_list.clear()
if is_connected:
sns_instruments = self.oncat_widget.agent.Instrument.list(facility="SNS")
hfir_instruments = self.oncat_widget.agent.Instrument.list(facility="HFIR")
for instrument in sns_instruments:
self.sns_list.addItem(instrument.get("name"))
for instrument in hfir_instruments:
self.hfir_list.addItem(instrument.get("name"))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Configuration: key and client_id¶
The ONCatLogin widget creates and owns its pyoncat.ONCat agent for you, so you do not
need to build one yourself. It accepts either a key or a client_id:
If
key(an application name) is passed, the client ID is looked up from the pyoncatqt configuration file, provided an entry exists for it.If
client_idis passed, it is used directly.If both are passed,
client_idconfigures the agent whilekeystill names the token file.
Without key, the token filename uses the client-ID prefix; with key, it uses the key.
The token is stored under ~/.pyoncatqt/.
Customizing the sign-in dialog title¶
Pass login_title to override the title of the sign-in dialog window (it defaults to
"Sign in to ONCat"):
self.oncat_widget = ONCatLogin(key="client", login_title="Connect to ONCat", parent=self)