##// END OF EJS Templates
Bring in Qt import logic from `ipykernel`...
Emilio Graff -
Show More
@@ -40,6 +40,82 b' class UnknownBackend(KeyError):'
40 ', '.join(backends + sorted(registered)))
40 ', '.join(backends + sorted(registered)))
41
41
42
42
43 def set_qt_api(gui):
44 """Sets the `QT_API` environment variable if it isn't already set."""
45 # TODO: how do we do this here?
46 # if hasattr(kernel, "app"):
47 # raise RuntimeError("Kernel already running a Qt event loop.")
48
49 # if gui != "qt" and hasattr(kernel, "last_qt_version"):
50 # if kernel.last_qt_version != gui:
51 # raise ValueError(
52 # "Cannot switch Qt versions for this session; "
53 # f"must use {kernel.last_qt_version}."
54 # )
55
56 qt_api = os.environ.get("QT_API", None)
57 if qt_api is not None and gui != "qt":
58 env2gui = {
59 "pyside": "qt4",
60 "pyqt": "qt4",
61 "pyside2": "qt5",
62 "pyqt5": "qt5",
63 "pyside6": "qt6",
64 "pyqt6": "qt6",
65 }
66 if env2gui[qt_api] != gui:
67 print(
68 f'Request for "{gui}" will be ignored because `QT_API` '
69 f'environment variable is set to "{qt_api}"'
70 )
71 else:
72 if gui == "qt4":
73 try:
74 import PyQt # noqa
75
76 os.environ["QT_API"] = "pyqt"
77 except ImportError:
78 try:
79 import PySide # noqa
80
81 os.environ["QT_API"] = "pyside"
82 except ImportError:
83 # Neither implementation installed; set it to something so IPython gives an error
84 os.environ["QT_API"] = "pyqt"
85 elif gui == "qt5":
86 try:
87 import PyQt5 # noqa
88
89 os.environ["QT_API"] = "pyqt5"
90 except ImportError:
91 try:
92 import PySide2 # noqa
93
94 os.environ["QT_API"] = "pyside2"
95 except ImportError:
96 os.environ["QT_API"] = "pyqt5"
97 elif gui == "qt6":
98 try:
99 import PyQt6 # noqa
100
101 os.environ["QT_API"] = "pyqt6"
102 except ImportError:
103 try:
104 import PySide6 # noqa
105
106 os.environ["QT_API"] = "pyside6"
107 except ImportError:
108 os.environ["QT_API"] = "pyqt6"
109 elif gui == "qt":
110 # Don't set QT_API; let IPython logic choose the version.
111 if "QT_API" in os.environ.keys():
112 del os.environ["QT_API"]
113 else:
114 raise ValueError(
115 f'Unrecognized Qt version: {gui}. Should be "qt4", "qt5", "qt6", or "qt".'
116 )
117
118
43 def get_inputhook_name_and_func(gui):
119 def get_inputhook_name_and_func(gui):
44 print(f'`get_inputhook_name_and_func` called with {gui=}')
120 print(f'`get_inputhook_name_and_func` called with {gui=}')
45 if gui in registered:
121 if gui in registered:
@@ -49,20 +125,13 b' def get_inputhook_name_and_func(gui):'
49 raise UnknownBackend(gui)
125 raise UnknownBackend(gui)
50
126
51 if gui in aliases:
127 if gui in aliases:
128 print('gui has an alias')
52 return get_inputhook_name_and_func(aliases[gui])
129 return get_inputhook_name_and_func(aliases[gui])
53
130
54 gui_mod = gui
131 gui_mod = gui
55 if gui == "qt5":
132 if gui.startswith("qt"):
56 os.environ["QT_API"] = "pyqt5"
133 set_qt_api(gui)
57 gui_mod = "qt"
58 elif gui == "qt6":
59 # XXX: this locks us into pyqt6 even if pyside6 is installed.
60 os.environ["QT_API"] = "pyqt6"
61 gui_mod = "qt"
134 gui_mod = "qt"
62
135
63 print(f'{gui_mod=}')
136 mod = importlib.import_module("IPython.terminal.pt_inputhooks." + gui_mod)
64 # Note: `IPython.terminal.pt_inputhooks.qt` imports `IPython.external.qt_for_kernel` and that's
65 # where the environment variable locks us into `pyqt6`, despite the fact that it seems `PySide6`
66 # is supported.
67 mod = importlib.import_module('IPython.terminal.pt_inputhooks.'+gui_mod)
68 return gui, mod.inputhook
137 return gui, mod.inputhook
General Comments 0
You need to be logged in to leave comments. Login now