def __setupUi(self): layout = QFormLayout() layout.setRowWrapPolicy(QFormLayout.WrapAllRows) layout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow) self.name_edit = LineEdit(self) self.name_edit.setPlaceholderText(self.tr("无标题")) self.name_edit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) self.desc_edit = QTextEdit(self) self.desc_edit.setTabChangesFocus(True) layout.addRow(self.tr("标题"), self.name_edit) layout.addRow(self.tr("描述"), self.desc_edit) self.setLayout(layout)
def minimumSizeHint(self): """Reimplemented to allow the Splitter to resize the widget with a continuous animation. """ hint = QTextEdit.minimumSizeHint(self) return QSize(hint.width(), 0)
def test_dock(self): reg = registry_tests.small_testing_registry() reg = QtWidgetRegistry(reg, parent=self.app) toolbox = WidgetToolBox() toolbox.setObjectName("widgets-toolbox") toolbox.setModel(reg.model()) text = QTextEdit() splitter = QSplitter() splitter.setOrientation(Qt.Vertical) splitter.addWidget(toolbox) splitter.addWidget(text) dock = CollapsibleDockWidget() dock.setExpandedWidget(splitter) toolbar = QToolBar() toolbar.addAction("1") toolbar.setOrientation(Qt.Vertical) toolbar.setMovable(False) toolbar.setFloatable(False) dock.setCollapsedWidget(toolbar) dock.show() self.qWait()
def main(argv): app = QApplication(argv) mw = QMainWindow() dock = CollapsibleDockWidget() w1 = QTreeView() w1.header().hide() w2 = QToolButton() w2.setFixedSize(38, 200) dock.setExpandedWidget(w1) dock.setCollapsedWidget(w2) mw.addDockWidget(Qt.LeftDockWidgetArea, dock) mw.setCentralWidget(QTextEdit()) mw.show() a = QAction("Expand", mw, checkable=True, shortcut=QKeySequence(Qt.ControlModifier | Qt.Key_D)) a.triggered[bool].connect(dock.setExpanded) mw.addAction(a) return app.exec()
def test1(self): class FT(QToolBar): def paintEvent(self, e): pass w = QMainWindow() ftt, ftb = FT(), FT() ftt.setFixedHeight(15) ftb.setFixedHeight(15) w.addToolBar(Qt.TopToolBarArea, ftt) w.addToolBar(Qt.BottomToolBarArea, ftb) f = dropshadow.DropShadowFrame() te = QTextEdit() c = QWidget() c.setLayout(QVBoxLayout()) c.layout().setContentsMargins(20, 0, 20, 0) c.layout().addWidget(te) w.setCentralWidget(c) f.setWidget(te) f.radius = 15 f.color = QColor(Qt.blue) w.show() self.singleShot(3000, lambda: f.setColor(Qt.red)) self.singleShot(4000, lambda: f.setRadius(30)) self.singleShot(5000, lambda: f.setRadius(40)) self.app.exec_()
def __setupUi(self): layout = QFormLayout() layout.setRowWrapPolicy(QFormLayout.WrapAllRows) layout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow) self.name_edit = LineEdit(self) self.name_edit.setPlaceholderText(self.tr("untitled")) self.name_edit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) self.desc_edit = QTextEdit(self) self.desc_edit.setTabChangesFocus(True) layout.addRow(self.tr("Title"), self.name_edit) layout.addRow(self.tr("Description"), self.desc_edit) self.__schemeIsUntitled = True self.setLayout(layout)
def test_dock_mainwinow(self): mw = QMainWindow() dock = CollapsibleDockWidget() w1 = QTextEdit() w2 = QToolButton() w2.setFixedSize(38, 200) dock.setExpandedWidget(w1) dock.setCollapsedWidget(w2) mw.addDockWidget(Qt.LeftDockWidgetArea, dock) mw.setCentralWidget(QTextEdit()) mw.show() timer = QTimer(dock, interval=200) timer.timeout.connect(lambda: dock.setExpanded(not dock.expanded())) timer.start()
def _add_tables_controls(self): vbox = gui.vBox(self.controlArea, "Tables", addSpace=True) box = gui.vBox(vbox) self.tables = TableModel() self.tablecombo = QComboBox( minimumContentsLength=35, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLength) self.tablecombo.setModel(self.tables) self.tablecombo.setToolTip('table') self.tablecombo.activated[int].connect(self.select_table) box.layout().addWidget(self.tablecombo) self.custom_sql = gui.vBox(box) self.custom_sql.setVisible(False) self.sqltext = QTextEdit(self.custom_sql) self.sqltext.setPlainText(self.sql) self.custom_sql.layout().addWidget(self.sqltext) mt = gui.hBox(self.custom_sql) cb = gui.checkBox(mt, self, 'materialize', 'Materialize to table ') cb.setToolTip('Save results of the query in a table') le = gui.lineEdit(mt, self, 'materialize_table_name') le.setToolTip('Save results of the query in a table') gui.button(self.custom_sql, self, 'Execute', callback=self.open_table) box.layout().addWidget(self.custom_sql) gui.checkBox(box, self, "guess_values", "Auto-discover categorical variables", callback=self.open_table) self.downloadcb = gui.checkBox(box, self, "download", "Download data to local memory", callback=self.open_table)
def test_dock_mainwinow(self): mw = QMainWindow() dock = CollapsibleDockWidget() w1 = QTextEdit() w2 = QToolButton() w2.setFixedSize(38, 200) dock.setExpandedWidget(w1) dock.setCollapsedWidget(w2) mw.addDockWidget(Qt.LeftDockWidgetArea, dock) mw.setCentralWidget(QTextEdit()) mw.show() def toogle(): dock.setExpanded(not dock.expanded()) self.singleShot(2000, toogle) toogle() self.app.exec_()
def extraSelections(self): """ In normal mode - QTextEdit.ExtraSelection which highlightes the cursor """ if not isinstance(self._mode, Normal): return [] selection = QTextEdit.ExtraSelection() selection.format.setBackground(QColor('#ffcc22')) selection.format.setForeground(QColor('#000000')) selection.cursor = self._qpart.textCursor() selection.cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor) return [selection]
def selections(self): """Build list of extra selections for rectangular selection""" selections = [] cursors = self.cursors() if cursors: background = self._qpart.palette().color(QPalette.Highlight) foreground = self._qpart.palette().color(QPalette.HighlightedText) for cursor in cursors: selection = QTextEdit.ExtraSelection() selection.format.setBackground(background) selection.format.setForeground(foreground) selection.cursor = cursor selections.append(selection) return selections
def test_splitter_resizer(self): w = QSplitter(orientation=Qt.Vertical) w.addWidget(QWidget()) text = QTextEdit() w.addWidget(text) resizer = SplitterResizer(w) resizer.setSplitterAndWidget(w, text) def toogle(): if resizer.size() == 0: resizer.open() else: resizer.close() self.singleShot(1000, toogle) w.show() self.singleShot(0, toogle) self.app.exec_()
def test_prop(self): w = QWidget() layout = QVBoxLayout() cb = QCheckBox("Check", w) sp = QSpinBox(w) le = QLineEdit(w) textw = QTextEdit(w, readOnly=True) textw.setProperty("checked_", False) textw.setProperty("spin_", 0) textw.setProperty("line_", "") textexpr = PropertyBindingExpr( r""" ("Check box is {0}\n" "Spin has value {1}\n" "Line contains {2}").format( "checked" if checked else "unchecked", spin, line) """, dict( checked=binding_for(cb, "checked"), spin=binding_for(sp, "value"), line=binding_for(le, "text"), ), ) layout.addWidget(cb) layout.addWidget(sp) layout.addWidget(le) layout.addWidget(textw) manager = BindingManager(submitPolicy=BindingManager.AutoSubmit) manager.bind(PropertyBinding(textw, "plainText", "textChanged"), textexpr) w.setLayout(layout) w.show() self.app.exec_()
def test_offset(self): w = QWidget() w.setLayout(QHBoxLayout()) w.setContentsMargins(30, 30, 30, 30) ww = QTextEdit() w.layout().addWidget(ww) f = dropshadow.DropShadowFrame(radius=20) f.setWidget(ww) oanim = QVariantAnimation( f, startValue=0.0, endValue=2 * math.pi, loopCount=-1, duration=2000, ) @oanim.valueChanged.connect def _(value): f.setOffset(QPoint(15 * math.cos(value), 15 * math.sin(value))) oanim.start() w.show() self.app.exec_()
def test_splitter_resizer(self): w = QSplitter(orientation=Qt.Vertical) w.addWidget(QWidget()) text = QTextEdit() w.addWidget(text) resizer = SplitterResizer(parent=None) resizer.setSplitterAndWidget(w, text) def toogle(): if resizer.size() == 0: resizer.open() else: resizer.close() w.show() timer = QTimer(resizer, interval=1000) timer.timeout.connect(toogle) timer.start() self.app.exec_()
def test1(self): class FT(QToolBar): def paintEvent(self, e): pass w = QMainWindow() ftt, ftb = FT(), FT() ftt.setFixedHeight(15) ftb.setFixedHeight(15) w.addToolBar(Qt.TopToolBarArea, ftt) w.addToolBar(Qt.BottomToolBarArea, ftb) f = dropshadow.DropShadowFrame() te = QTextEdit() c = QWidget() c.setLayout(QVBoxLayout()) c.layout().setContentsMargins(20, 0, 20, 0) c.layout().addWidget(te) w.setCentralWidget(c) f.setWidget(te) f.radius = 15 f.color = QColor(Qt.blue) w.show() canim = QPropertyAnimation(f, b"color_", f, startValue=QColor(Qt.red), endValue=QColor(Qt.blue), loopCount=-1, duration=2000) canim.start() ranim = QPropertyAnimation(f, b"radius_", f, startValue=30, endValue=40, loopCount=-1, duration=3000) ranim.start() self.app.exec_()
def _makeMatchSelection(self, block, columnIndex, matched): """Make matched or unmatched QTextEdit.ExtraSelection """ selection = QTextEdit.ExtraSelection() darkMode = QApplication.instance().property('darkMode') if matched: fgColor = self.MATCHED_COLOR else: fgColor = self.UNMATCHED_COLOR selection.format.setForeground(fgColor) # repaint hack selection.format.setBackground( Qt.white if not darkMode else QColor('#111111')) selection.cursor = QTextCursor(block) selection.cursor.setPosition(block.position() + columnIndex) selection.cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor) return selection
def test_prop(self): w = QWidget() layout = QVBoxLayout() cb = QCheckBox("Check", w) sp = QSpinBox(w) le = QLineEdit(w) textw = QTextEdit(w, readOnly=True) textw.setProperty("checked_", False) textw.setProperty("spin_", 0) textw.setProperty("line_", "") textexpr = PropertyBindingExpr( r""" ("Check box is {0}\n" "Spin has value {1}\n" "Line contains {2}").format( "checked" if checked else "unchecked", spin, line) """, dict(checked=binding_for(cb, "checked"), spin=binding_for(sp, "value"), line=binding_for(le, "text")), ) layout.addWidget(cb) layout.addWidget(sp) layout.addWidget(le) layout.addWidget(textw) manager = BindingManager(submitPolicy=BindingManager.AutoSubmit) manager.bind(PropertyBinding(textw, "plainText", "textChanged"), textexpr) w.setLayout(layout) w.show() self.app.exec_()
def __init__(self): super().__init__() self.backend = None self.data_desc_table = None self.database_desc = None vbox = gui.vBox(self.controlArea, "Server", addSpace=True) box = gui.vBox(vbox) self.backends = BackendModel(Backend.available_backends()) self.backendcombo = QComboBox(box) if len(self.backends): self.backendcombo.setModel(self.backends) else: self.Error.no_backends() box.setEnabled(False) box.layout().addWidget(self.backendcombo) self.servertext = QLineEdit(box) self.servertext.setPlaceholderText("Server") self.servertext.setToolTip("Server") self.servertext.editingFinished.connect(self._load_credentials) if self.host: self.servertext.setText(self.host if not self.port else "{}:{}". format(self.host, self.port)) box.layout().addWidget(self.servertext) self.databasetext = QLineEdit(box) self.databasetext.setPlaceholderText("Database[/Schema]") self.databasetext.setToolTip("Database or optionally Database/Schema") if self.database: self.databasetext.setText( self.database if not self.schema else "{}/{}". format(self.database, self.schema)) box.layout().addWidget(self.databasetext) self.usernametext = QLineEdit(box) self.usernametext.setPlaceholderText("Username") self.usernametext.setToolTip("Username") box.layout().addWidget(self.usernametext) self.passwordtext = QLineEdit(box) self.passwordtext.setPlaceholderText("Password") self.passwordtext.setToolTip("Password") self.passwordtext.setEchoMode(QLineEdit.Password) box.layout().addWidget(self.passwordtext) self._load_credentials() self.tables = TableModel() tables = gui.hBox(box) self.tablecombo = QComboBox( minimumContentsLength=35, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLength, ) self.tablecombo.setModel(self.tables) self.tablecombo.setToolTip("table") tables.layout().addWidget(self.tablecombo) self.connect() index = self.tablecombo.findText(str(self.table)) if index != -1: self.tablecombo.setCurrentIndex(index) # set up the callback to select_table in case of selection change self.tablecombo.activated[int].connect(self.select_table) self.connectbutton = gui.button(tables, self, "↻", callback=self.connect) self.connectbutton.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) tables.layout().addWidget(self.connectbutton) self.custom_sql = gui.vBox(box) self.custom_sql.setVisible(False) self.sqltext = QTextEdit(self.custom_sql) self.sqltext.setPlainText(self.sql) self.custom_sql.layout().addWidget(self.sqltext) mt = gui.hBox(self.custom_sql) cb = gui.checkBox(mt, self, "materialize", "Materialize to table ") cb.setToolTip("Save results of the query in a table") le = gui.lineEdit(mt, self, "materialize_table_name") le.setToolTip("Save results of the query in a table") self.executebtn = gui.button(self.custom_sql, self, "Execute", callback=self.open_table) box.layout().addWidget(self.custom_sql) gui.checkBox( box, self, "guess_values", "Auto-discover categorical variables", callback=self.open_table, ) gui.checkBox( box, self, "download", "Download data to local memory", callback=self.open_table, ) gui.rubber(self.buttonsArea) QTimer.singleShot(0, self.select_table)
def __init__(self): super().__init__() self.backend = None self.data_desc_table = None self.database_desc = None vbox = gui.vBox(self.controlArea, "Server", addSpace=True) box = gui.vBox(vbox) self.backendmodel = BackendModel(Backend.available_backends()) self.backendcombo = QComboBox(box) if len(self.backendmodel): self.backendcombo.setModel(self.backendmodel) else: self.Error.no_backends() box.setEnabled(False) box.layout().addWidget(self.backendcombo) self.servertext = QLineEdit(box) self.servertext.setPlaceholderText('Server') self.servertext.setToolTip('Server') self.servertext.editingFinished.connect(self._load_credentials) if self.host: self.servertext.setText(self.host if not self.port else '{}:{}'. format(self.host, self.port)) box.layout().addWidget(self.servertext) self.databasetext = QLineEdit(box) self.databasetext.setPlaceholderText('Database[/Schema]') self.databasetext.setToolTip('Database or optionally Database/Schema') if self.database: self.databasetext.setText( self.database if not self.schema else '{}/{}'. format(self.database, self.schema)) box.layout().addWidget(self.databasetext) self.usernametext = QLineEdit(box) self.usernametext.setPlaceholderText('Username') self.usernametext.setToolTip('Username') box.layout().addWidget(self.usernametext) self.passwordtext = QLineEdit(box) self.passwordtext.setPlaceholderText('Password') self.passwordtext.setToolTip('Password') self.passwordtext.setEchoMode(QLineEdit.Password) box.layout().addWidget(self.passwordtext) self._load_credentials() tables = gui.hBox(box) self.tablemodel = TableModel() self.tablecombo = QComboBox( minimumContentsLength=35, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLength) self.tablecombo.setModel(self.tablemodel) self.tablecombo.setToolTip('table') tables.layout().addWidget(self.tablecombo) self.tablecombo.activated[int].connect(self.select_table) self.connectbutton = gui.button(tables, self, '↻', callback=self.connect) self.connectbutton.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) tables.layout().addWidget(self.connectbutton) self.custom_sql = gui.vBox(box) self.custom_sql.setVisible(False) self.sqltext = QTextEdit(self.custom_sql) self.sqltext.setPlainText(self.sql) self.custom_sql.layout().addWidget(self.sqltext) mt = gui.hBox(self.custom_sql) cb = gui.checkBox(mt, self, 'materialize', 'Materialize to table ') cb.setToolTip('Save results of the query in a table') le = gui.lineEdit(mt, self, 'materialize_table_name') le.setToolTip('Save results of the query in a table') self.executebtn = gui.button(self.custom_sql, self, 'Execute', callback=self.open_table) box.layout().addWidget(self.custom_sql) gui.checkBox(box, self, "guess_values", "Auto-discover discrete variables", callback=self.open_table) gui.checkBox(box, self, "download", "Download data to local memory", callback=self.open_table) gui.rubber(self.buttonsArea) QTimer.singleShot(0, self.connect)
class OWSql(OWWidget): name = "SQL Table" id = "orange.widgets.data.sql" description = "Load data set from SQL." icon = "icons/SQLTable.svg" priority = 10 category = "Data" keywords = ["data", "file", "load", "read"] class Outputs: data = Output( "Data", Table, doc="Attribute-valued data set read from the input file.") settings_version = 2 want_main_area = False resizing_enabled = False host = Setting(None) port = Setting(None) database = Setting(None) schema = Setting(None) username = "" password = "" table = Setting(None) sql = Setting("") guess_values = Setting(True) download = Setting(False) materialize = Setting(False) materialize_table_name = Setting("") class Information(OWWidget.Information): data_sampled = Msg("Data description was generated from a sample.") class Error(OWWidget.Error): connection = Msg("{}") no_backends = Msg("Please install a backend to use this widget") missing_extension = Msg("Database is missing extension{}: {}") def __init__(self): super().__init__() self.backend = None self.data_desc_table = None self.database_desc = None vbox = gui.vBox(self.controlArea, "Server", addSpace=True) box = gui.vBox(vbox) self.backendmodel = BackendModel(Backend.available_backends()) self.backendcombo = QComboBox(box) if len(self.backendmodel): self.backendcombo.setModel(self.backendmodel) else: self.Error.no_backends() box.setEnabled(False) box.layout().addWidget(self.backendcombo) self.servertext = QLineEdit(box) self.servertext.setPlaceholderText('Server') self.servertext.setToolTip('Server') self.servertext.editingFinished.connect(self._load_credentials) if self.host: self.servertext.setText(self.host if not self.port else '{}:{}'. format(self.host, self.port)) box.layout().addWidget(self.servertext) self.databasetext = QLineEdit(box) self.databasetext.setPlaceholderText('Database[/Schema]') self.databasetext.setToolTip('Database or optionally Database/Schema') if self.database: self.databasetext.setText( self.database if not self.schema else '{}/{}'. format(self.database, self.schema)) box.layout().addWidget(self.databasetext) self.usernametext = QLineEdit(box) self.usernametext.setPlaceholderText('Username') self.usernametext.setToolTip('Username') box.layout().addWidget(self.usernametext) self.passwordtext = QLineEdit(box) self.passwordtext.setPlaceholderText('Password') self.passwordtext.setToolTip('Password') self.passwordtext.setEchoMode(QLineEdit.Password) box.layout().addWidget(self.passwordtext) self._load_credentials() tables = gui.hBox(box) self.tablemodel = TableModel() self.tablecombo = QComboBox( minimumContentsLength=35, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLength) self.tablecombo.setModel(self.tablemodel) self.tablecombo.setToolTip('table') tables.layout().addWidget(self.tablecombo) self.tablecombo.activated[int].connect(self.select_table) self.connectbutton = gui.button(tables, self, '↻', callback=self.connect) self.connectbutton.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) tables.layout().addWidget(self.connectbutton) self.custom_sql = gui.vBox(box) self.custom_sql.setVisible(False) self.sqltext = QTextEdit(self.custom_sql) self.sqltext.setPlainText(self.sql) self.custom_sql.layout().addWidget(self.sqltext) mt = gui.hBox(self.custom_sql) cb = gui.checkBox(mt, self, 'materialize', 'Materialize to table ') cb.setToolTip('Save results of the query in a table') le = gui.lineEdit(mt, self, 'materialize_table_name') le.setToolTip('Save results of the query in a table') self.executebtn = gui.button(self.custom_sql, self, 'Execute', callback=self.open_table) box.layout().addWidget(self.custom_sql) gui.checkBox(box, self, "guess_values", "Auto-discover discrete variables", callback=self.open_table) gui.checkBox(box, self, "download", "Download data to local memory", callback=self.open_table) gui.rubber(self.buttonsArea) QTimer.singleShot(0, self.connect) def _load_credentials(self): self._parse_host_port() cm = self._credential_manager(self.host, self.port) self.username = cm.username self.password = cm.password if self.username: self.usernametext.setText(self.username) if self.password: self.passwordtext.setText(self.password) def _save_credentials(self): cm = self._credential_manager(self.host, self.port) cm.username = self.username cm.password = self.password def _credential_manager(self, host, port): return CredentialManager("SQL Table: {}:{}".format(host, port)) def error(self, id=0, text=""): super().error(id, text) err_style = 'QLineEdit {border: 2px solid red;}' if 'server' in text or 'host' in text: self.servertext.setStyleSheet(err_style) else: self.servertext.setStyleSheet('') if 'role' in text: self.usernametext.setStyleSheet(err_style) else: self.usernametext.setStyleSheet('') if 'database' in text: self.databasetext.setStyleSheet(err_style) else: self.databasetext.setStyleSheet('') def _parse_host_port(self): hostport = self.servertext.text().split(':') self.host = hostport[0] self.port = hostport[1] if len(hostport) == 2 else None def connect(self): self._parse_host_port() self.database, _, self.schema = self.databasetext.text().partition('/') self.username = self.usernametext.text() or None self.password = self.passwordtext.text() or None try: if self.backendcombo.currentIndex() < 0: return backend = self.backendmodel[self.backendcombo.currentIndex()] self.backend = backend( dict(host=self.host, port=self.port, database=self.database, user=self.username, password=self.password)) self.Error.connection.clear() self._save_credentials() self.database_desc = OrderedDict( (("Host", self.host), ("Port", self.port), ("Database", self.database), ("User name", self.username))) self.refresh_tables() self.select_table() except BackendError as err: error = str(err).split('\n')[0] self.Error.connection(error) self.database_desc = self.data_desc_table = None self.tablecombo.clear() def refresh_tables(self): self.tablemodel.clear() self.Error.missing_extension.clear() if self.backend is None: self.data_desc_table = None return self.tablemodel.append("Select a table") self.tablemodel.extend(self.backend.list_tables(self.schema)) self.tablemodel.append("Custom SQL") def select_table(self): curIdx = self.tablecombo.currentIndex() if self.tablecombo.itemText(curIdx) != "Custom SQL": self.custom_sql.setVisible(False) return self.open_table() else: self.custom_sql.setVisible(True) self.data_desc_table = None self.database_desc["Table"] = "(None)" self.table = None #self.Error.missing_extension( # 's' if len(missing) > 1 else '', # ', '.join(missing), # shown=missing) def open_table(self): table = self.get_table() self.data_desc_table = table self.Outputs.data.send(table) def get_table(self): if self.tablecombo.currentIndex() <= 0: if self.database_desc: self.database_desc["Table"] = "(None)" self.data_desc_table = None return if self.tablecombo.currentIndex() < self.tablecombo.count() - 1: self.table = self.tablemodel[self.tablecombo.currentIndex()] self.database_desc["Table"] = self.table if "Query" in self.database_desc: del self.database_desc["Query"] else: self.sql = self.table = self.sqltext.toPlainText() if self.materialize: import psycopg2 if not self.materialize_table_name: self.Error.connection( "Specify a table name to materialize the query") return try: with self.backend.execute_sql_query( "DROP TABLE IF EXISTS " + self.materialize_table_name): pass with self.backend.execute_sql_query( "CREATE TABLE " + self.materialize_table_name + " AS " + self.table): pass with self.backend.execute_sql_query( "ANALYZE " + self.materialize_table_name): pass self.table = self.materialize_table_name except (psycopg2.ProgrammingError, BackendError) as ex: self.Error.connection(str(ex)) return try: table = SqlTable(dict(host=self.host, port=self.port, database=self.database, user=self.username, password=self.password), self.table, backend=type(self.backend), inspect_values=False) except BackendError as ex: self.Error.connection(str(ex)) return self.Error.connection.clear() sample = False if table.approx_len() > LARGE_TABLE and self.guess_values: confirm = QMessageBox(self) confirm.setIcon(QMessageBox.Warning) confirm.setText("Attribute discovery might take " "a long time on large tables.\n" "Do you want to auto discover attributes?") confirm.addButton("Yes", QMessageBox.YesRole) no_button = confirm.addButton("No", QMessageBox.NoRole) sample_button = confirm.addButton("Yes, on a sample", QMessageBox.YesRole) confirm.exec() if confirm.clickedButton() == no_button: self.guess_values = False elif confirm.clickedButton() == sample_button: sample = True self.Information.clear() if self.guess_values: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) if sample: s = table.sample_time(1) domain = s.get_domain(inspect_values=True) self.Information.data_sampled() else: domain = table.get_domain(inspect_values=True) QApplication.restoreOverrideCursor() table.domain = domain if self.download: if table.approx_len() > MAX_DL_LIMIT: QMessageBox.warning( self, 'Warning', "Data is too big to download.\n" "Consider using the Data Sampler widget to download " "a sample instead.") self.download = False elif table.approx_len() > AUTO_DL_LIMIT: confirm = QMessageBox.question( self, 'Question', "Data appears to be big. Do you really " "want to download it to local memory?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if confirm == QMessageBox.No: self.download = False if self.download: table.download_data(MAX_DL_LIMIT) table = Table(table) return table def send_report(self): if not self.database_desc: self.report_paragraph("No database connection.") return self.report_items("Database", self.database_desc) if self.data_desc_table: self.report_items("Data", report.describe_data(self.data_desc_table)) @classmethod def migrate_settings(cls, settings, version): if version < 2: # Until Orange version 3.4.4 username and password had been stored # in Settings. cm = cls._credential_manager(settings["host"], settings["port"]) cm.username = settings["username"] cm.password = settings["password"]
def __init__(self): super().__init__() self.output_corpus = None self.pubmed_api = None self.progress = None self.email_is_valid = False self.record_count = 0 self.download_running = False # To hold all the controls. Makes access easier. self.pubmed_controls = [] h_box = gui.hBox(self.controlArea) label = gui.label(h_box, self, 'Email:') label.setMaximumSize(label.sizeHint()) # Drop-down for recent emails. self.email_combo = QComboBox(h_box) self.email_combo.setMinimumWidth(150) self.email_combo.setEditable(True) self.email_combo.lineEdit().textChanged.connect(self.sync_email) h_box.layout().addWidget(self.email_combo) self.email_combo.activated[int].connect(self.select_email) # RECORD SEARCH self.search_tabs = gui.tabWidget(self.controlArea) # --- Regular search --- regular_search_box = gui.widgetBox(self.controlArea, addSpace=True) # Author self.author_input = gui.lineEdit(regular_search_box, self, 'author', 'Author:', orientation=Qt.Horizontal) self.pubmed_controls.append(self.author_input) h_box = gui.hBox(regular_search_box) year_box = gui.widgetBox(h_box, orientation=Qt.Horizontal) min_date = QDate.fromString( self.MIN_DATE.strftime(self.PY_DATE_FORMAT), self.QT_DATE_FORMAT) if not self.pub_date_from: self.pub_date_from = self.MIN_DATE.strftime(self.PY_DATE_FORMAT) if not self.pub_date_to: self.pub_date_to = date.today().strftime(self.PY_DATE_FORMAT) self.date_from = QDateEdit(QDate.fromString(self.pub_date_from, self.QT_DATE_FORMAT), displayFormat=self.QT_DATE_FORMAT, minimumDate=min_date, calendarPopup=True) self.date_to = QDateEdit(QDate.fromString(self.pub_date_to, self.QT_DATE_FORMAT), displayFormat=self.QT_DATE_FORMAT, minimumDate=min_date, calendarPopup=True) self.date_from.dateChanged.connect(lambda date: setattr( self, 'pub_date_from', date.toString(self.QT_DATE_FORMAT))) self.date_to.dateChanged.connect(lambda date: setattr( self, 'pub_date_to', date.toString(self.QT_DATE_FORMAT))) self.pubmed_controls.append(self.date_from) self.pubmed_controls.append(self.date_to) gui.label(year_box, self, 'From:') year_box.layout().addWidget(self.date_from) gui.label(year_box, self, 'to:') year_box.layout().addWidget(self.date_to) # Keywords. h_box = gui.hBox(regular_search_box) label = gui.label(h_box, self, 'Query:') label.setMaximumSize(label.sizeHint()) self.keyword_combo = QComboBox(h_box) self.keyword_combo.setMinimumWidth(150) self.keyword_combo.setEditable(True) h_box.layout().addWidget(self.keyword_combo) self.keyword_combo.activated[int].connect(self.select_keywords) self.pubmed_controls.append(self.keyword_combo) tab_height = regular_search_box.sizeHint() regular_search_box.setMaximumSize(tab_height) # --- Advanced search --- advanced_search_box = gui.widgetBox(self.controlArea, addSpace=True) # Advanced search query. h_box = gui.hBox(advanced_search_box) self.advanced_query_input = QTextEdit(h_box) h_box.layout().addWidget(self.advanced_query_input) self.advanced_query_input.setMaximumSize(tab_height) self.pubmed_controls.append(self.advanced_query_input) gui.createTabPage(self.search_tabs, 'Regular search', regular_search_box) gui.createTabPage(self.search_tabs, 'Advanced search', advanced_search_box) # Search info label. self.search_info_label = gui.label(self.controlArea, self, 'Number of records found: /') # Search for records button. self.run_search_button = gui.button( self.controlArea, self, 'Find records', callback=self.run_search, tooltip='Performs a search for articles that fit the ' 'specified parameters.') self.pubmed_controls.append(self.run_search_button) h_line = QFrame() h_line.setFrameShape(QFrame.HLine) h_line.setFrameShadow(QFrame.Sunken) self.controlArea.layout().addWidget(h_line) # RECORD RETRIEVAL # Text includes box. text_includes_box = gui.widgetBox(self.controlArea, 'Text includes', addSpace=True) self.authors_checkbox = gui.checkBox(text_includes_box, self, 'includes_authors', 'Authors') self.title_checkbox = gui.checkBox(text_includes_box, self, 'includes_title', 'Article title') self.mesh_checkbox = gui.checkBox(text_includes_box, self, 'includes_mesh', 'Mesh headings') self.abstract_checkbox = gui.checkBox(text_includes_box, self, 'includes_abstract', 'Abstract') self.url_checkbox = gui.checkBox(text_includes_box, self, 'includes_url', 'URL') self.pubmed_controls.append(self.authors_checkbox) self.pubmed_controls.append(self.title_checkbox) self.pubmed_controls.append(self.mesh_checkbox) self.pubmed_controls.append(self.abstract_checkbox) self.pubmed_controls.append(self.url_checkbox) # Num. records. h_box = gui.hBox(self.controlArea) label = gui.label(h_box, self, 'Retrieve') label.setMaximumSize(label.sizeHint()) self.num_records_input = gui.spin(h_box, self, 'num_records', minv=1, maxv=10000) self.max_records_label = gui.label(h_box, self, 'records from /.') self.max_records_label.setMaximumSize( self.max_records_label.sizeHint()) self.pubmed_controls.append(self.num_records_input) # Download articles. # Search for records button. self.retrieve_records_button = gui.button( self.controlArea, self, 'Retrieve records', callback=self.retrieve_records, tooltip='Retrieves the specified documents.') self.pubmed_controls.append(self.retrieve_records_button) # Num. retrieved records info label. self.retrieval_info_label = gui.label( self.controlArea, self, 'Number of records retrieved: /') # Load the most recent emails. self.set_email_list() # Load the most recent queries. self.set_keyword_list() # Check the email and enable controls accordingly. if self.recent_emails: email = self.recent_emails[0] self.email_is_valid = validate_email(email) self.enable_controls()
def __init__(self): super().__init__() self.backend = None self.data_desc_table = None self.database_desc = None vbox = gui.vBox(self.controlArea, "Server", addSpace=True) box = gui.vBox(vbox) self.backends = BackendModel(Backend.available_backends()) self.backendcombo = QComboBox(box) if len(self.backends): self.backendcombo.setModel(self.backends) else: self.Error.no_backends() box.setEnabled(False) box.layout().addWidget(self.backendcombo) self.servertext = QLineEdit(box) self.servertext.setPlaceholderText('Server') self.servertext.setToolTip('Server') self.servertext.editingFinished.connect(self._load_credentials) if self.host: self.servertext.setText(self.host if not self.port else '{}:{}'.format(self.host, self.port)) box.layout().addWidget(self.servertext) self.databasetext = QLineEdit(box) self.databasetext.setPlaceholderText('Database[/Schema]') self.databasetext.setToolTip('Database or optionally Database/Schema') if self.database: self.databasetext.setText( self.database if not self.schema else '{}/{}'.format(self.database, self.schema)) box.layout().addWidget(self.databasetext) self.usernametext = QLineEdit(box) self.usernametext.setPlaceholderText('Username') self.usernametext.setToolTip('Username') box.layout().addWidget(self.usernametext) self.passwordtext = QLineEdit(box) self.passwordtext.setPlaceholderText('Password') self.passwordtext.setToolTip('Password') self.passwordtext.setEchoMode(QLineEdit.Password) box.layout().addWidget(self.passwordtext) self._load_credentials() self.tables = TableModel() tables = gui.hBox(box) self.tablecombo = QComboBox( minimumContentsLength=35, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLength ) self.tablecombo.setModel(self.tables) self.tablecombo.setToolTip('table') tables.layout().addWidget(self.tablecombo) self.connect() index = self.tablecombo.findText(str(self.table)) if index != -1: self.tablecombo.setCurrentIndex(index) # set up the callback to select_table in case of selection change self.tablecombo.activated[int].connect(self.select_table) self.connectbutton = gui.button( tables, self, '↻', callback=self.connect) self.connectbutton.setSizePolicy( QSizePolicy.Fixed, QSizePolicy.Fixed) tables.layout().addWidget(self.connectbutton) self.custom_sql = gui.vBox(box) self.custom_sql.setVisible(False) self.sqltext = QTextEdit(self.custom_sql) self.sqltext.setPlainText(self.sql) self.custom_sql.layout().addWidget(self.sqltext) mt = gui.hBox(self.custom_sql) cb = gui.checkBox(mt, self, 'materialize', 'Materialize to table ') cb.setToolTip('Save results of the query in a table') le = gui.lineEdit(mt, self, 'materialize_table_name') le.setToolTip('Save results of the query in a table') self.executebtn = gui.button( self.custom_sql, self, 'Execute', callback=self.open_table) box.layout().addWidget(self.custom_sql) gui.checkBox(box, self, "guess_values", "Auto-discover categorical variables", callback=self.open_table) gui.checkBox(box, self, "download", "Download data to local memory", callback=self.open_table) gui.rubber(self.buttonsArea) QTimer.singleShot(0, self.select_table)
class OWSql(OWWidget): name = "SQL Table" id = "orange.widgets.data.sql" description = "Load data set from SQL." icon = "icons/SQLTable.svg" priority = 30 category = "Data" keywords = ["data", "file", "load", "read", "SQL"] class Outputs: data = Output("Data", Table, doc="Attribute-valued data set read from the input file.") settings_version = 2 want_main_area = False resizing_enabled = False host = Setting(None) port = Setting(None) database = Setting(None) schema = Setting(None) username = "" password = "" table = Setting(None) sql = Setting("") guess_values = Setting(True) download = Setting(False) materialize = Setting(False) materialize_table_name = Setting("") class Information(OWWidget.Information): data_sampled = Msg("Data description was generated from a sample.") class Error(OWWidget.Error): connection = Msg("{}") no_backends = Msg("Please install a backend to use this widget") missing_extension = Msg("Database is missing extension{}: {}") def __init__(self): super().__init__() self.backend = None self.data_desc_table = None self.database_desc = None vbox = gui.vBox(self.controlArea, "Server", addSpace=True) box = gui.vBox(vbox) self.backends = BackendModel(Backend.available_backends()) self.backendcombo = QComboBox(box) if len(self.backends): self.backendcombo.setModel(self.backends) else: self.Error.no_backends() box.setEnabled(False) box.layout().addWidget(self.backendcombo) self.servertext = QLineEdit(box) self.servertext.setPlaceholderText('Server') self.servertext.setToolTip('Server') self.servertext.editingFinished.connect(self._load_credentials) if self.host: self.servertext.setText(self.host if not self.port else '{}:{}'.format(self.host, self.port)) box.layout().addWidget(self.servertext) self.databasetext = QLineEdit(box) self.databasetext.setPlaceholderText('Database[/Schema]') self.databasetext.setToolTip('Database or optionally Database/Schema') if self.database: self.databasetext.setText( self.database if not self.schema else '{}/{}'.format(self.database, self.schema)) box.layout().addWidget(self.databasetext) self.usernametext = QLineEdit(box) self.usernametext.setPlaceholderText('Username') self.usernametext.setToolTip('Username') box.layout().addWidget(self.usernametext) self.passwordtext = QLineEdit(box) self.passwordtext.setPlaceholderText('Password') self.passwordtext.setToolTip('Password') self.passwordtext.setEchoMode(QLineEdit.Password) box.layout().addWidget(self.passwordtext) self._load_credentials() self.tables = TableModel() tables = gui.hBox(box) self.tablecombo = QComboBox( minimumContentsLength=35, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLength ) self.tablecombo.setModel(self.tables) self.tablecombo.setToolTip('table') tables.layout().addWidget(self.tablecombo) self.connect() index = self.tablecombo.findText(str(self.table)) if index != -1: self.tablecombo.setCurrentIndex(index) # set up the callback to select_table in case of selection change self.tablecombo.activated[int].connect(self.select_table) self.connectbutton = gui.button( tables, self, '↻', callback=self.connect) self.connectbutton.setSizePolicy( QSizePolicy.Fixed, QSizePolicy.Fixed) tables.layout().addWidget(self.connectbutton) self.custom_sql = gui.vBox(box) self.custom_sql.setVisible(False) self.sqltext = QTextEdit(self.custom_sql) self.sqltext.setPlainText(self.sql) self.custom_sql.layout().addWidget(self.sqltext) mt = gui.hBox(self.custom_sql) cb = gui.checkBox(mt, self, 'materialize', 'Materialize to table ') cb.setToolTip('Save results of the query in a table') le = gui.lineEdit(mt, self, 'materialize_table_name') le.setToolTip('Save results of the query in a table') self.executebtn = gui.button( self.custom_sql, self, 'Execute', callback=self.open_table) box.layout().addWidget(self.custom_sql) gui.checkBox(box, self, "guess_values", "Auto-discover categorical variables", callback=self.open_table) gui.checkBox(box, self, "download", "Download data to local memory", callback=self.open_table) gui.rubber(self.buttonsArea) QTimer.singleShot(0, self.select_table) def _load_credentials(self): self._parse_host_port() cm = self._credential_manager(self.host, self.port) self.username = cm.username self.password = cm.password if self.username: self.usernametext.setText(self.username) if self.password: self.passwordtext.setText(self.password) def _save_credentials(self): cm = self._credential_manager(self.host, self.port) cm.username = self.username or '' cm.password = self.password or '' def _credential_manager(self, host, port): return CredentialManager("SQL Table: {}:{}".format(host, port)) def error(self, id=0, text=""): super().error(id, text) err_style = 'QLineEdit {border: 2px solid red;}' if 'server' in text or 'host' in text: self.servertext.setStyleSheet(err_style) else: self.servertext.setStyleSheet('') if 'role' in text: self.usernametext.setStyleSheet(err_style) else: self.usernametext.setStyleSheet('') if 'database' in text: self.databasetext.setStyleSheet(err_style) else: self.databasetext.setStyleSheet('') def _parse_host_port(self): hostport = self.servertext.text().split(':') self.host = hostport[0] self.port = hostport[1] if len(hostport) == 2 else None def connect(self): self._parse_host_port() self.database, _, self.schema = self.databasetext.text().partition('/') self.username = self.usernametext.text() or None self.password = self.passwordtext.text() or None try: if self.backendcombo.currentIndex() < 0: return backend = self.backends[self.backendcombo.currentIndex()] self.backend = backend(dict( host=self.host, port=self.port, database=self.database, user=self.username, password=self.password )) self.Error.connection.clear() self._save_credentials() self.database_desc = OrderedDict(( ("Host", self.host), ("Port", self.port), ("Database", self.database), ("User name", self.username) )) self.refresh_tables() except BackendError as err: error = str(err).split('\n')[0] self.Error.connection(error) self.database_desc = self.data_desc_table = None self.tablecombo.clear() def refresh_tables(self): self.tables.clear() self.Error.missing_extension.clear() if self.backend is None: self.data_desc_table = None return self.tables.append("Select a table") self.tables.append("Custom SQL") self.tables.extend(self.backend.list_tables(self.schema)) # Called on tablecombo selection change: def select_table(self): curIdx = self.tablecombo.currentIndex() if self.tablecombo.itemText(curIdx) != "Custom SQL": self.custom_sql.setVisible(False) return self.open_table() else: self.custom_sql.setVisible(True) self.data_desc_table = None self.database_desc["Table"] = "(None)" self.table = None if len(str(self.sql)) > 14: return self.open_table() #self.Error.missing_extension( # 's' if len(missing) > 1 else '', # ', '.join(missing), # shown=missing) def open_table(self): table = self.get_table() self.data_desc_table = table self.Outputs.data.send(table) def get_table(self): curIdx = self.tablecombo.currentIndex() if curIdx <= 0: if self.database_desc: self.database_desc["Table"] = "(None)" self.data_desc_table = None return if self.tablecombo.itemText(curIdx) != "Custom SQL": self.table = self.tables[self.tablecombo.currentIndex()] self.database_desc["Table"] = self.table if "Query" in self.database_desc: del self.database_desc["Query"] what = self.table else: what = self.sql = self.sqltext.toPlainText() self.table = "Custom SQL" if self.materialize: import psycopg2 if not self.materialize_table_name: self.Error.connection( "Specify a table name to materialize the query") return try: with self.backend.execute_sql_query("DROP TABLE IF EXISTS " + self.materialize_table_name): pass with self.backend.execute_sql_query("CREATE TABLE " + self.materialize_table_name + " AS " + self.sql): pass with self.backend.execute_sql_query("ANALYZE " + self.materialize_table_name): pass except (psycopg2.ProgrammingError, BackendError) as ex: self.Error.connection(str(ex)) return try: table = SqlTable(dict(host=self.host, port=self.port, database=self.database, user=self.username, password=self.password), what, backend=type(self.backend), inspect_values=False) except BackendError as ex: self.Error.connection(str(ex)) return self.Error.connection.clear() sample = False if table.approx_len() > LARGE_TABLE and self.guess_values: confirm = QMessageBox(self) confirm.setIcon(QMessageBox.Warning) confirm.setText("Attribute discovery might take " "a long time on large tables.\n" "Do you want to auto discover attributes?") confirm.addButton("Yes", QMessageBox.YesRole) no_button = confirm.addButton("No", QMessageBox.NoRole) sample_button = confirm.addButton("Yes, on a sample", QMessageBox.YesRole) confirm.exec() if confirm.clickedButton() == no_button: self.guess_values = False elif confirm.clickedButton() == sample_button: sample = True self.Information.clear() if self.guess_values: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) if sample: s = table.sample_time(1) domain = s.get_domain(inspect_values=True) self.Information.data_sampled() else: domain = table.get_domain(inspect_values=True) QApplication.restoreOverrideCursor() table.domain = domain if self.download: if table.approx_len() > MAX_DL_LIMIT: QMessageBox.warning( self, 'Warning', "Data is too big to download.\n" "Consider using the Data Sampler widget to download " "a sample instead.") self.download = False elif table.approx_len() > AUTO_DL_LIMIT: confirm = QMessageBox.question( self, 'Question', "Data appears to be big. Do you really " "want to download it to local memory?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if confirm == QMessageBox.No: self.download = False if self.download: table.download_data(MAX_DL_LIMIT) table = Table(table) return table def send_report(self): if not self.database_desc: self.report_paragraph("No database connection.") return self.report_items("Database", self.database_desc) if self.data_desc_table: self.report_items("Data", report.describe_data(self.data_desc_table)) @classmethod def migrate_settings(cls, settings, version): if version < 2: # Until Orange version 3.4.4 username and password had been stored # in Settings. cm = cls._credential_manager(settings["host"], settings["port"]) cm.username = settings["username"] cm.password = settings["password"]
def __init__(self): super().__init__() self.output_corpus = None self.pubmed_api = None self.progress = None self.email_is_valid = False self.record_count = 0 self.download_running = False # To hold all the controls. Makes access easier. self.pubmed_controls = [] h_box = gui.hBox(self.controlArea) label = gui.label(h_box, self, 'Email:') label.setMaximumSize(label.sizeHint()) # Drop-down for recent emails. self.email_combo = QComboBox(h_box) self.email_combo.setMinimumWidth(150) self.email_combo.setEditable(True) self.email_combo.lineEdit().textChanged.connect(self.sync_email) h_box.layout().addWidget(self.email_combo) self.email_combo.activated[int].connect(self.select_email) # RECORD SEARCH self.search_tabs = gui.tabWidget(self.controlArea) # --- Regular search --- regular_search_box = gui.widgetBox(self.controlArea, addSpace=True) # Author self.author_input = gui.lineEdit(regular_search_box, self, 'author', 'Author:', orientation=Qt.Horizontal) self.pubmed_controls.append(self.author_input) h_box = gui.hBox(regular_search_box) year_box = gui.widgetBox(h_box, orientation=Qt.Horizontal) min_date = QDate.fromString( self.MIN_DATE.strftime(self.PY_DATE_FORMAT), self.QT_DATE_FORMAT ) if not self.pub_date_from: self.pub_date_from = self.MIN_DATE.strftime(self.PY_DATE_FORMAT) if not self.pub_date_to: self.pub_date_to = date.today().strftime(self.PY_DATE_FORMAT) self.date_from = QDateEdit( QDate.fromString(self.pub_date_from, self.QT_DATE_FORMAT), displayFormat=self.QT_DATE_FORMAT, minimumDate=min_date, calendarPopup=True ) self.date_to = QDateEdit( QDate.fromString(self.pub_date_to, self.QT_DATE_FORMAT), displayFormat=self.QT_DATE_FORMAT, minimumDate=min_date, calendarPopup=True ) self.date_from.dateChanged.connect( lambda date: setattr(self, 'pub_date_from', date.toString(self.QT_DATE_FORMAT))) self.date_to.dateChanged.connect( lambda date: setattr(self, 'pub_date_to', date.toString(self.QT_DATE_FORMAT))) self.pubmed_controls.append(self.date_from) self.pubmed_controls.append(self.date_to) gui.label(year_box, self, 'From:') year_box.layout().addWidget(self.date_from) gui.label(year_box, self, 'to:') year_box.layout().addWidget(self.date_to) # Keywords. h_box = gui.hBox(regular_search_box) label = gui.label(h_box, self, 'Query:') label.setMaximumSize(label.sizeHint()) self.keyword_combo = QComboBox(h_box) self.keyword_combo.setMinimumWidth(150) self.keyword_combo.setEditable(True) h_box.layout().addWidget(self.keyword_combo) self.keyword_combo.activated[int].connect(self.select_keywords) self.pubmed_controls.append(self.keyword_combo) tab_height = regular_search_box.sizeHint() regular_search_box.setMaximumSize(tab_height) # --- Advanced search --- advanced_search_box = gui.widgetBox(self.controlArea, addSpace=True) # Advanced search query. h_box = gui.hBox(advanced_search_box) self.advanced_query_input = QTextEdit(h_box) h_box.layout().addWidget(self.advanced_query_input) self.advanced_query_input.setMaximumSize(tab_height) self.pubmed_controls.append(self.advanced_query_input) gui.createTabPage(self.search_tabs, 'Regular search', regular_search_box) gui.createTabPage(self.search_tabs, 'Advanced search', advanced_search_box) # Search info label. self.search_info_label = gui.label( self.controlArea, self, 'Number of records found: /') # Search for records button. self.run_search_button = gui.button( self.controlArea, self, 'Find records', callback=self.run_search, tooltip='Performs a search for articles that fit the ' 'specified parameters.') self.pubmed_controls.append(self.run_search_button) h_line = QFrame() h_line.setFrameShape(QFrame.HLine) h_line.setFrameShadow(QFrame.Sunken) self.controlArea.layout().addWidget(h_line) # RECORD RETRIEVAL # Text includes box. text_includes_box = gui.widgetBox(self.controlArea, 'Text includes', addSpace=True) self.authors_checkbox = gui.checkBox(text_includes_box, self, 'includes_authors', 'Authors') self.title_checkbox = gui.checkBox(text_includes_box, self, 'includes_title', 'Article title') self.mesh_checkbox = gui.checkBox(text_includes_box, self, 'includes_mesh', 'Mesh headings') self.abstract_checkbox = gui.checkBox(text_includes_box, self, 'includes_abstract', 'Abstract') self.url_checkbox = gui.checkBox(text_includes_box, self, 'includes_url', 'URL') self.pubmed_controls.append(self.authors_checkbox) self.pubmed_controls.append(self.title_checkbox) self.pubmed_controls.append(self.mesh_checkbox) self.pubmed_controls.append(self.abstract_checkbox) self.pubmed_controls.append(self.url_checkbox) # Num. records. h_box = gui.hBox(self.controlArea) label = gui.label(h_box, self, 'Retrieve') label.setMaximumSize(label.sizeHint()) self.num_records_input = gui.spin(h_box, self, 'num_records', minv=1, maxv=100000) self.max_records_label = gui.label(h_box, self, 'records from /.') self.max_records_label.setMaximumSize(self.max_records_label .sizeHint()) self.pubmed_controls.append(self.num_records_input) # Download articles. # Search for records button. self.retrieve_records_button = gui.button( self.controlArea, self, 'Retrieve records', callback=self.retrieve_records, tooltip='Retrieves the specified documents.') self.pubmed_controls.append(self.retrieve_records_button) # Num. retrieved records info label. self.retrieval_info_label = gui.label( self.controlArea, self, 'Number of records retrieved: /') # Load the most recent emails. self.set_email_list() # Load the most recent queries. self.set_keyword_list() # Check the email and enable controls accordingly. if self.recent_emails: email = self.recent_emails[0] self.email_is_valid = validate_email(email) self.enable_controls()
class OWSql(OWBaseSql): name = "SQL Table" id = "orange.widgets.data.sql" description = "Load dataset from SQL." icon = "icons/SQLTable.svg" priority = 30 category = "Data" keywords = ["load"] class Outputs: data = Output("Data", Table, doc="Attribute-valued dataset read from the input file.") settings_version = 2 table = Setting(None) sql = Setting("") guess_values = Setting(True) download = Setting(False) materialize = Setting(False) materialize_table_name = Setting("") class Information(OWBaseSql.Information): data_sampled = Msg("Data description was generated from a sample.") class Warning(OWBaseSql.Warning): missing_extension = Msg("Database is missing extensions: {}") class Error(OWBaseSql.Error): no_backends = Msg("Please install a backend to use this widget.") def __init__(self): # Lint self.backends = None self.backendcombo = None self.tables = None self.tablecombo = None self.sqltext = None self.custom_sql = None self.downloadcb = None super().__init__() def _setup_gui(self): super()._setup_gui() self._add_backend_controls() self._add_tables_controls() def _add_backend_controls(self): box = self.serverbox self.backends = BackendModel(Backend.available_backends()) self.backendcombo = QComboBox(box) if self.backends: self.backendcombo.setModel(self.backends) else: self.Error.no_backends() box.setEnabled(False) box.layout().insertWidget(0, self.backendcombo) def _add_tables_controls(self): vbox = gui.vBox(self.controlArea, "Tables", addSpace=True) box = gui.vBox(vbox) self.tables = TableModel() self.tablecombo = QComboBox( minimumContentsLength=35, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLength) self.tablecombo.setModel(self.tables) self.tablecombo.setToolTip('table') self.tablecombo.activated[int].connect(self.select_table) box.layout().addWidget(self.tablecombo) self.custom_sql = gui.vBox(box) self.custom_sql.setVisible(False) self.sqltext = QTextEdit(self.custom_sql) self.sqltext.setPlainText(self.sql) self.custom_sql.layout().addWidget(self.sqltext) mt = gui.hBox(self.custom_sql) cb = gui.checkBox(mt, self, 'materialize', 'Materialize to table ') cb.setToolTip('Save results of the query in a table') le = gui.lineEdit(mt, self, 'materialize_table_name') le.setToolTip('Save results of the query in a table') gui.button(self.custom_sql, self, 'Execute', callback=self.open_table) box.layout().addWidget(self.custom_sql) gui.checkBox(box, self, "guess_values", "Auto-discover categorical variables", callback=self.open_table) self.downloadcb = gui.checkBox(box, self, "download", "Download data to local memory", callback=self.open_table) def highlight_error(self, text=""): err = ['', 'QLineEdit {border: 2px solid red;}'] self.servertext.setStyleSheet(err['server' in text or 'host' in text]) self.usernametext.setStyleSheet(err['role' in text]) self.databasetext.setStyleSheet(err['database' in text]) def get_backend(self): if self.backendcombo.currentIndex() < 0: return None return self.backends[self.backendcombo.currentIndex()] def on_connection_success(self): if getattr(self.backend, 'missing_extension', False): self.Warning.missing_extension(", ".join( self.backend.missing_extension)) self.download = True self.downloadcb.setEnabled(False) if not is_postgres(self.backend): self.download = True self.downloadcb.setEnabled(False) super().on_connection_success() self.refresh_tables() self.select_table() def on_connection_error(self, err): super().on_connection_error(err) self.highlight_error(str(err).split("\n")[0]) def clear(self): super().clear() self.Warning.missing_extension.clear() self.downloadcb.setEnabled(True) self.highlight_error() self.tablecombo.clear() self.tablecombo.repaint() def refresh_tables(self): self.tables.clear() if self.backend is None: self.data_desc_table = None return self.tables.append("Select a table") self.tables.append("Custom SQL") self.tables.extend(self.backend.list_tables(self.schema)) index = self.tablecombo.findText(str(self.table)) self.tablecombo.setCurrentIndex(index if index != -1 else 0) self.tablecombo.repaint() # Called on tablecombo selection change: def select_table(self): curIdx = self.tablecombo.currentIndex() if self.tablecombo.itemText(curIdx) != "Custom SQL": self.custom_sql.setVisible(False) return self.open_table() else: self.custom_sql.setVisible(True) self.data_desc_table = None self.database_desc["Table"] = "(None)" self.table = None if len(str(self.sql)) > 14: return self.open_table() return None def get_table(self): curIdx = self.tablecombo.currentIndex() if curIdx <= 0: if self.database_desc: self.database_desc["Table"] = "(None)" self.data_desc_table = None return None if self.tablecombo.itemText(curIdx) != "Custom SQL": self.table = self.tables[self.tablecombo.currentIndex()] self.database_desc["Table"] = self.table if "Query" in self.database_desc: del self.database_desc["Query"] what = self.table else: what = self.sql = self.sqltext.toPlainText() self.table = "Custom SQL" if self.materialize: if not self.materialize_table_name: self.Error.connection( "Specify a table name to materialize the query") return None try: with self.backend.execute_sql_query( "DROP TABLE IF EXISTS " + self.materialize_table_name): pass with self.backend.execute_sql_query( "CREATE TABLE " + self.materialize_table_name + " AS " + self.sql): pass with self.backend.execute_sql_query( "ANALYZE " + self.materialize_table_name): pass except BackendError as ex: self.Error.connection(str(ex)) return None try: table = SqlTable(dict(host=self.host, port=self.port, database=self.database, user=self.username, password=self.password), what, backend=type(self.backend), inspect_values=False) except BackendError as ex: self.Error.connection(str(ex)) return None self.Error.connection.clear() sample = False if table.approx_len() > LARGE_TABLE and self.guess_values: confirm = QMessageBox(self) confirm.setIcon(QMessageBox.Warning) confirm.setText("Attribute discovery might take " "a long time on large tables.\n" "Do you want to auto discover attributes?") confirm.addButton("Yes", QMessageBox.YesRole) no_button = confirm.addButton("No", QMessageBox.NoRole) if is_postgres(self.backend): sample_button = confirm.addButton("Yes, on a sample", QMessageBox.YesRole) confirm.exec() if confirm.clickedButton() == no_button: self.guess_values = False elif is_postgres(self.backend) and \ confirm.clickedButton() == sample_button: sample = True self.Information.clear() if self.guess_values: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) if sample: s = table.sample_time(1) domain = s.get_domain(inspect_values=True) self.Information.data_sampled() else: domain = table.get_domain(inspect_values=True) QApplication.restoreOverrideCursor() table.domain = domain if self.download: if table.approx_len() > AUTO_DL_LIMIT: if is_postgres(self.backend): confirm = QMessageBox(self) confirm.setIcon(QMessageBox.Warning) confirm.setText("Data appears to be big. Do you really " "want to download it to local memory?") if table.approx_len() <= MAX_DL_LIMIT: confirm.addButton("Yes", QMessageBox.YesRole) no_button = confirm.addButton("No", QMessageBox.NoRole) sample_button = confirm.addButton("Yes, a sample", QMessageBox.YesRole) confirm.exec() if confirm.clickedButton() == no_button: return None elif confirm.clickedButton() == sample_button: table = table.sample_percentage( AUTO_DL_LIMIT / table.approx_len() * 100) else: if table.approx_len() > MAX_DL_LIMIT: QMessageBox.warning(self, 'Warning', "Data is too big to download.\n") return None else: confirm = QMessageBox.question( self, 'Question', "Data appears to be big. Do you really " "want to download it to local memory?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if confirm == QMessageBox.No: return None table.download_data(MAX_DL_LIMIT) table = Table(table) return table @classmethod def migrate_settings(cls, settings, version): if version < 2: # Until Orange version 3.4.4 username and password had been stored # in Settings. cm = cls._credential_manager(settings["host"], settings["port"]) cm.username = settings["username"] cm.password = settings["password"]
def focusOutEvent(self, event): self.set_placeholder() QTextEdit.focusOutEvent(self, event)
def __init__(self): super().__init__() RecentPathsWComboMixin.__init__(self) self.current_file = None self.find_text = "" self.selected_lines = [] box = gui.widgetBox( self.controlArea, box="File", orientation=QGridLayout()) box.layout().addWidget(self.file_combo, 0, 0, 1, 2) self.file_combo.activated[int].connect(self.select_file) box.layout().addWidget( gui.button( None, self, 'Open', callback=self.browse_file, autoDefault=False, icon=self.style().standardIcon(QStyle.SP_DirOpenIcon)), 1, 0) box.layout().addWidget( gui.button( None, self, "Reload", callback=self.open_file, autoDefault=False, icon=self.style().standardIcon(QStyle.SP_BrowserReload)), 1, 1) box = gui.widgetBox(self.controlArea, box="Pattern") lineedit = gui.lineEdit(box, self, "pattern") lineedit.returnPressed.connect(self.grep_lines) gui.checkBox( box, self, "case_sensitive", label="Case sensitive", callback=self.grep_lines) gui.checkBox( box, self, "regular_expression", label="Regular expression", callback=self.grep_lines) box = gui.widgetBox(self.controlArea, box="Format") gui.spin( box, self, "skip_lines", 0, 10, label="Skipped lines: ", callback=self.grep_lines) gui.spin( box, self, "block_length", 1, 100000, label="Block length: ", callback=self.grep_lines) gui.checkBox( box, self, "has_header_row", label="Block(s) start with header row", tooltip="Only the header at the first block will be used.", callback=self.has_header_changed) gui.rubber(self.controlArea) gui.auto_commit(self.controlArea, self, "auto_send", "Send") box = gui.hBox(self.mainArea) gui.widgetLabel(box, "Input text") gui.rubber(box) find_line = gui.lineEdit( box, self, "find_text", label="Find: ", orientation=Qt.Horizontal, callback=self.find_changed, callbackOnType=True) find_line.returnPressed.connect(self.find_changed) self.in_view = QTextEdit(readOnly=True) self.mainArea.layout().addWidget(self.in_view) gui.widgetLabel(self.mainArea, "Used lines") self.out_view = QTextEdit(readOnly=True) self.mainArea.layout().addWidget(self.out_view) self.set_file_list() # Must not call select_file from within __init__ to avoid reentering # the event loop by a progress bar, when we have it if self.recent_paths: QTimer.singleShot(0, self.select_file)
class OWPubmed(OWWidget): name = 'Pubmed' description = 'Fetch data from Pubmed.' icon = 'icons/Pubmed.svg' priority = 20 outputs = [(Output.CORPUS, Corpus)] want_main_area = False resizing_enabled = False QT_DATE_FORMAT = 'yyyy-MM-dd' PY_DATE_FORMAT = '%Y-%m-%d' MIN_DATE = date(1800, 1, 1) # Settings. recent_emails = Setting([]) author = Setting('') pub_date_from = Setting('') pub_date_to = Setting('') recent_keywords = Setting([]) last_advanced_query = Setting('') num_records = Setting(1000) # Text includes checkboxes. includes_authors = Setting(True) includes_title = Setting(True) includes_mesh = Setting(True) includes_abstract = Setting(True) includes_url = Setting(True) class Warning(OWWidget.Warning): no_query = Msg('Please specify the keywords for this query.') class Error(OWWidget.Error): api_error = Msg('API error: {}.') def __init__(self): super().__init__() self.output_corpus = None self.pubmed_api = None self.progress = None self.email_is_valid = False self.record_count = 0 self.download_running = False # To hold all the controls. Makes access easier. self.pubmed_controls = [] h_box = gui.hBox(self.controlArea) label = gui.label(h_box, self, 'Email:') label.setMaximumSize(label.sizeHint()) # Drop-down for recent emails. self.email_combo = QComboBox(h_box) self.email_combo.setMinimumWidth(150) self.email_combo.setEditable(True) self.email_combo.lineEdit().textChanged.connect(self.sync_email) h_box.layout().addWidget(self.email_combo) self.email_combo.activated[int].connect(self.select_email) # RECORD SEARCH self.search_tabs = gui.tabWidget(self.controlArea) # --- Regular search --- regular_search_box = gui.widgetBox(self.controlArea, addSpace=True) # Author self.author_input = gui.lineEdit(regular_search_box, self, 'author', 'Author:', orientation=Qt.Horizontal) self.pubmed_controls.append(self.author_input) h_box = gui.hBox(regular_search_box) year_box = gui.widgetBox(h_box, orientation=Qt.Horizontal) min_date = QDate.fromString( self.MIN_DATE.strftime(self.PY_DATE_FORMAT), self.QT_DATE_FORMAT ) if not self.pub_date_from: self.pub_date_from = self.MIN_DATE.strftime(self.PY_DATE_FORMAT) if not self.pub_date_to: self.pub_date_to = date.today().strftime(self.PY_DATE_FORMAT) self.date_from = QDateEdit( QDate.fromString(self.pub_date_from, self.QT_DATE_FORMAT), displayFormat=self.QT_DATE_FORMAT, minimumDate=min_date, calendarPopup=True ) self.date_to = QDateEdit( QDate.fromString(self.pub_date_to, self.QT_DATE_FORMAT), displayFormat=self.QT_DATE_FORMAT, minimumDate=min_date, calendarPopup=True ) self.date_from.dateChanged.connect( lambda date: setattr(self, 'pub_date_from', date.toString(self.QT_DATE_FORMAT))) self.date_to.dateChanged.connect( lambda date: setattr(self, 'pub_date_to', date.toString(self.QT_DATE_FORMAT))) self.pubmed_controls.append(self.date_from) self.pubmed_controls.append(self.date_to) gui.label(year_box, self, 'From:') year_box.layout().addWidget(self.date_from) gui.label(year_box, self, 'to:') year_box.layout().addWidget(self.date_to) # Keywords. h_box = gui.hBox(regular_search_box) label = gui.label(h_box, self, 'Query:') label.setMaximumSize(label.sizeHint()) self.keyword_combo = QComboBox(h_box) self.keyword_combo.setMinimumWidth(150) self.keyword_combo.setEditable(True) h_box.layout().addWidget(self.keyword_combo) self.keyword_combo.activated[int].connect(self.select_keywords) self.pubmed_controls.append(self.keyword_combo) tab_height = regular_search_box.sizeHint() regular_search_box.setMaximumSize(tab_height) # --- Advanced search --- advanced_search_box = gui.widgetBox(self.controlArea, addSpace=True) # Advanced search query. h_box = gui.hBox(advanced_search_box) self.advanced_query_input = QTextEdit(h_box) h_box.layout().addWidget(self.advanced_query_input) self.advanced_query_input.setMaximumSize(tab_height) self.pubmed_controls.append(self.advanced_query_input) gui.createTabPage(self.search_tabs, 'Regular search', regular_search_box) gui.createTabPage(self.search_tabs, 'Advanced search', advanced_search_box) # Search info label. self.search_info_label = gui.label( self.controlArea, self, 'Number of records found: /') # Search for records button. self.run_search_button = gui.button( self.controlArea, self, 'Find records', callback=self.run_search, tooltip='Performs a search for articles that fit the ' 'specified parameters.') self.pubmed_controls.append(self.run_search_button) h_line = QFrame() h_line.setFrameShape(QFrame.HLine) h_line.setFrameShadow(QFrame.Sunken) self.controlArea.layout().addWidget(h_line) # RECORD RETRIEVAL # Text includes box. text_includes_box = gui.widgetBox(self.controlArea, 'Text includes', addSpace=True) self.authors_checkbox = gui.checkBox(text_includes_box, self, 'includes_authors', 'Authors') self.title_checkbox = gui.checkBox(text_includes_box, self, 'includes_title', 'Article title') self.mesh_checkbox = gui.checkBox(text_includes_box, self, 'includes_mesh', 'Mesh headings') self.abstract_checkbox = gui.checkBox(text_includes_box, self, 'includes_abstract', 'Abstract') self.url_checkbox = gui.checkBox(text_includes_box, self, 'includes_url', 'URL') self.pubmed_controls.append(self.authors_checkbox) self.pubmed_controls.append(self.title_checkbox) self.pubmed_controls.append(self.mesh_checkbox) self.pubmed_controls.append(self.abstract_checkbox) self.pubmed_controls.append(self.url_checkbox) # Num. records. h_box = gui.hBox(self.controlArea) label = gui.label(h_box, self, 'Retrieve') label.setMaximumSize(label.sizeHint()) self.num_records_input = gui.spin(h_box, self, 'num_records', minv=1, maxv=100000) self.max_records_label = gui.label(h_box, self, 'records from /.') self.max_records_label.setMaximumSize(self.max_records_label .sizeHint()) self.pubmed_controls.append(self.num_records_input) # Download articles. # Search for records button. self.retrieve_records_button = gui.button( self.controlArea, self, 'Retrieve records', callback=self.retrieve_records, tooltip='Retrieves the specified documents.') self.pubmed_controls.append(self.retrieve_records_button) # Num. retrieved records info label. self.retrieval_info_label = gui.label( self.controlArea, self, 'Number of records retrieved: /') # Load the most recent emails. self.set_email_list() # Load the most recent queries. self.set_keyword_list() # Check the email and enable controls accordingly. if self.recent_emails: email = self.recent_emails[0] self.email_is_valid = validate_email(email) self.enable_controls() def sync_email(self): email = self.email_combo.currentText() self.email_is_valid = validate_email(email) self.enable_controls() def enable_controls(self): # Enable/disable controls accordingly. for control in self.pubmed_controls: control.setEnabled(self.email_is_valid) if self.pubmed_api is None or self.pubmed_api.search_record_count == 0: self.retrieve_records_button.setEnabled(False) if not self.email_is_valid: self.email_combo.setFocus() def run_search(self): self.Error.clear() self.Warning.clear() self.run_search_button.setEnabled(False) self.retrieve_records_button.setEnabled(False) # Add the email to history. email = self.email_combo.currentText() if email not in self.recent_emails: self.recent_emails.insert(0, email) # Check if the PubMed object is present. if self.pubmed_api is None: self.pubmed_api = Pubmed( email=email, progress_callback=self.api_progress_callback, error_callback=self.api_error_callback, ) if self.search_tabs.currentIndex() == 0: # Get query parameters. terms = self.keyword_combo.currentText().split() authors = self.author_input.text().split() error = self.pubmed_api._search_for_records( terms, authors, self.pub_date_from, self.pub_date_to ) if error is not None: self.Error.api_error(str(error)) return if self.keyword_combo.currentText() not in self.recent_keywords: self.recent_keywords.insert( 0, self.keyword_combo.currentText() ) else: query = self.advanced_query_input.toPlainText() if not query: self.Warning.no_query() self.run_search_button.setEnabled(True) self.retrieve_records_button.setEnabled(True) return error = self.pubmed_api._search_for_records(advanced_query=query) if error is not None: self.Error.api_error(str(error)) return self.last_advanced_query = query self.enable_controls() self.update_search_info() def retrieve_records(self): self.Warning.clear() self.Error.clear() if self.pubmed_api is None: return if self.download_running: self.download_running = False self.run_search_button.setEnabled(True) self.retrieve_records_button.setText('Retrieve records') self.pubmed_api.stop_retrieving() return self.download_running = True self.run_search_button.setEnabled(False) self.output_corpus = None # Clear the old records. # Change the button label. self.retrieve_records_button.setText('Stop retrieving') # Text fields. text_includes_params = [ self.includes_authors, self.includes_title, self.includes_mesh, self.includes_abstract, self.includes_url, True, # Publication date field; included always. ] required_text_fields = [ field for field_name, field in zip(text_includes_params, PUBMED_TEXT_FIELDS) if field_name ] batch_size = min(Pubmed.MAX_BATCH_SIZE, self.num_records) + 1 with self.progressBar(self.num_records/batch_size) as progress: self.progress = progress self.output_corpus = self.pubmed_api._retrieve_records( self.num_records, required_text_fields ) self.retrieve_records_button.setText('Retrieve records') self.download_running = False self.send(Output.CORPUS, self.output_corpus) self.update_retrieval_info() self.run_search_button.setEnabled(True) def api_progress_callback(self, start_at=None): if start_at is not None: self.progress.count = start_at else: self.progress.advance() def api_error_callback(self, error): self.Error.api_error(str(error)) if self.progress is not None: self.progress.finish() def update_search_info(self): max_records_count = min( self.pubmed_api.MAX_RECORDS, self.pubmed_api.search_record_count ) self.search_info_label.setText( 'Number of retrievable records for ' 'this search query: {} '.format(max_records_count) ) self.max_records_label.setText( 'records from {}.'.format(max_records_count) ) self.max_records_label.setMaximumSize(self.max_records_label .sizeHint()) self.num_records_input.setMaximum(max_records_count) self.retrieve_records_button.setFocus() def update_retrieval_info(self): document_count = 0 if self.output_corpus is not None: document_count = len(self.output_corpus) self.retrieval_info_label.setText( 'Number of records retrieved: {} '.format(document_count) ) self.retrieval_info_label.setMaximumSize( self.retrieval_info_label.sizeHint() ) def select_email(self, n): if n < len(self.recent_emails): email = self.recent_emails[n] del self.recent_emails[n] self.recent_emails.insert(0, email) if len(self.recent_emails) > 0: self.set_email_list() def set_email_list(self): self.email_combo.clear() for email in self.recent_emails: self.email_combo.addItem(email) def select_keywords(self, n): if n < len(self.recent_keywords): keywords = self.recent_keywords[n] del self.recent_keywords[n] self.recent_keywords.insert(0, keywords) if len(self.recent_keywords) > 0: self.set_keyword_list() def set_keyword_list(self): self.keyword_combo.clear() if not self.recent_keywords: # Sample queries. self.recent_keywords.append('orchid') self.recent_keywords.append('hypertension') self.recent_keywords.append('blood pressure') self.recent_keywords.append('radiology') for keywords in self.recent_keywords: self.keyword_combo.addItem(keywords) def open_calendar(self, widget): cal_dlg = CalendarDialog(self, 'Date picker') if cal_dlg.exec_(): widget.setText(cal_dlg.picked_date)
class OWPubmed(OWWidget): name = 'Pubmed' description = 'Fetch data from Pubmed.' icon = 'icons/Pubmed.svg' priority = 140 outputs = [(Output.CORPUS, Corpus)] want_main_area = False resizing_enabled = False QT_DATE_FORMAT = 'yyyy-MM-dd' PY_DATE_FORMAT = '%Y-%m-%d' MIN_DATE = date(1800, 1, 1) # Settings. recent_emails = Setting([]) author = Setting('') pub_date_from = Setting('') pub_date_to = Setting('') recent_keywords = Setting([]) last_advanced_query = Setting('') num_records = Setting(1000) # Text includes checkboxes. includes_authors = Setting(True) includes_title = Setting(True) includes_mesh = Setting(True) includes_abstract = Setting(True) includes_url = Setting(True) class Warning(OWWidget.Warning): no_query = Msg('Please specify the keywords for this query.') class Error(OWWidget.Error): api_error = Msg('API error: {}.') def __init__(self): super().__init__() self.output_corpus = None self.pubmed_api = None self.progress = None self.email_is_valid = False self.record_count = 0 self.download_running = False # To hold all the controls. Makes access easier. self.pubmed_controls = [] h_box = gui.hBox(self.controlArea) label = gui.label(h_box, self, 'Email:') label.setMaximumSize(label.sizeHint()) # Drop-down for recent emails. self.email_combo = QComboBox(h_box) self.email_combo.setMinimumWidth(150) self.email_combo.setEditable(True) self.email_combo.lineEdit().textChanged.connect(self.sync_email) h_box.layout().addWidget(self.email_combo) self.email_combo.activated[int].connect(self.select_email) # RECORD SEARCH self.search_tabs = gui.tabWidget(self.controlArea) # --- Regular search --- regular_search_box = gui.widgetBox(self.controlArea, addSpace=True) # Author self.author_input = gui.lineEdit(regular_search_box, self, 'author', 'Author:', orientation=Qt.Horizontal) self.pubmed_controls.append(self.author_input) h_box = gui.hBox(regular_search_box) year_box = gui.widgetBox(h_box, orientation=Qt.Horizontal) min_date = QDate.fromString( self.MIN_DATE.strftime(self.PY_DATE_FORMAT), self.QT_DATE_FORMAT) if not self.pub_date_from: self.pub_date_from = self.MIN_DATE.strftime(self.PY_DATE_FORMAT) if not self.pub_date_to: self.pub_date_to = date.today().strftime(self.PY_DATE_FORMAT) self.date_from = QDateEdit(QDate.fromString(self.pub_date_from, self.QT_DATE_FORMAT), displayFormat=self.QT_DATE_FORMAT, minimumDate=min_date, calendarPopup=True) self.date_to = QDateEdit(QDate.fromString(self.pub_date_to, self.QT_DATE_FORMAT), displayFormat=self.QT_DATE_FORMAT, minimumDate=min_date, calendarPopup=True) self.date_from.dateChanged.connect(lambda date: setattr( self, 'pub_date_from', date.toString(self.QT_DATE_FORMAT))) self.date_to.dateChanged.connect(lambda date: setattr( self, 'pub_date_to', date.toString(self.QT_DATE_FORMAT))) self.pubmed_controls.append(self.date_from) self.pubmed_controls.append(self.date_to) gui.label(year_box, self, 'From:') year_box.layout().addWidget(self.date_from) gui.label(year_box, self, 'to:') year_box.layout().addWidget(self.date_to) # Keywords. h_box = gui.hBox(regular_search_box) label = gui.label(h_box, self, 'Query:') label.setMaximumSize(label.sizeHint()) self.keyword_combo = QComboBox(h_box) self.keyword_combo.setMinimumWidth(150) self.keyword_combo.setEditable(True) h_box.layout().addWidget(self.keyword_combo) self.keyword_combo.activated[int].connect(self.select_keywords) self.pubmed_controls.append(self.keyword_combo) tab_height = regular_search_box.sizeHint() regular_search_box.setMaximumSize(tab_height) # --- Advanced search --- advanced_search_box = gui.widgetBox(self.controlArea, addSpace=True) # Advanced search query. h_box = gui.hBox(advanced_search_box) self.advanced_query_input = QTextEdit(h_box) h_box.layout().addWidget(self.advanced_query_input) self.advanced_query_input.setMaximumSize(tab_height) self.pubmed_controls.append(self.advanced_query_input) gui.createTabPage(self.search_tabs, 'Regular search', regular_search_box) gui.createTabPage(self.search_tabs, 'Advanced search', advanced_search_box) # Search info label. self.search_info_label = gui.label(self.controlArea, self, 'Number of records found: /') # Search for records button. self.run_search_button = gui.button( self.controlArea, self, 'Find records', callback=self.run_search, tooltip='Performs a search for articles that fit the ' 'specified parameters.') self.pubmed_controls.append(self.run_search_button) h_line = QFrame() h_line.setFrameShape(QFrame.HLine) h_line.setFrameShadow(QFrame.Sunken) self.controlArea.layout().addWidget(h_line) # RECORD RETRIEVAL # Text includes box. text_includes_box = gui.widgetBox(self.controlArea, 'Text includes', addSpace=True) self.authors_checkbox = gui.checkBox(text_includes_box, self, 'includes_authors', 'Authors') self.title_checkbox = gui.checkBox(text_includes_box, self, 'includes_title', 'Article title') self.mesh_checkbox = gui.checkBox(text_includes_box, self, 'includes_mesh', 'Mesh headings') self.abstract_checkbox = gui.checkBox(text_includes_box, self, 'includes_abstract', 'Abstract') self.url_checkbox = gui.checkBox(text_includes_box, self, 'includes_url', 'URL') self.pubmed_controls.append(self.authors_checkbox) self.pubmed_controls.append(self.title_checkbox) self.pubmed_controls.append(self.mesh_checkbox) self.pubmed_controls.append(self.abstract_checkbox) self.pubmed_controls.append(self.url_checkbox) # Num. records. h_box = gui.hBox(self.controlArea) label = gui.label(h_box, self, 'Retrieve') label.setMaximumSize(label.sizeHint()) self.num_records_input = gui.spin(h_box, self, 'num_records', minv=1, maxv=10000) self.max_records_label = gui.label(h_box, self, 'records from /.') self.max_records_label.setMaximumSize( self.max_records_label.sizeHint()) self.pubmed_controls.append(self.num_records_input) # Download articles. # Search for records button. self.retrieve_records_button = gui.button( self.controlArea, self, 'Retrieve records', callback=self.retrieve_records, tooltip='Retrieves the specified documents.') self.pubmed_controls.append(self.retrieve_records_button) # Num. retrieved records info label. self.retrieval_info_label = gui.label( self.controlArea, self, 'Number of records retrieved: /') # Load the most recent emails. self.set_email_list() # Load the most recent queries. self.set_keyword_list() # Check the email and enable controls accordingly. if self.recent_emails: email = self.recent_emails[0] self.email_is_valid = validate_email(email) self.enable_controls() def sync_email(self): email = self.email_combo.currentText() self.email_is_valid = validate_email(email) self.enable_controls() def enable_controls(self): # Enable/disable controls accordingly. for control in self.pubmed_controls: control.setEnabled(self.email_is_valid) if self.pubmed_api is None or self.pubmed_api.search_record_count == 0: self.retrieve_records_button.setEnabled(False) if not self.email_is_valid: self.email_combo.setFocus() def run_search(self): self.Error.clear() self.Warning.clear() self.run_search_button.setEnabled(False) self.retrieve_records_button.setEnabled(False) # Add the email to history. email = self.email_combo.currentText() if email not in self.recent_emails: self.recent_emails.insert(0, email) # Check if the PubMed object is present. if self.pubmed_api is None: self.pubmed_api = Pubmed( email=email, progress_callback=self.api_progress_callback, error_callback=self.api_error_callback, ) if self.search_tabs.currentIndex() == 0: # Get query parameters. terms = self.keyword_combo.currentText().split() authors = self.author_input.text().split() error = self.pubmed_api._search_for_records( terms, authors, self.pub_date_from, self.pub_date_to) if error is not None: self.Error.api_error(str(error)) return if self.keyword_combo.currentText() not in self.recent_keywords: self.recent_keywords.insert(0, self.keyword_combo.currentText()) else: query = self.advanced_query_input.toPlainText() if not query: self.Warning.no_query() self.run_search_button.setEnabled(True) self.retrieve_records_button.setEnabled(True) return error = self.pubmed_api._search_for_records(advanced_query=query) if error is not None: self.Error.api_error(str(error)) return self.last_advanced_query = query self.enable_controls() self.update_search_info() def retrieve_records(self): self.Warning.clear() self.Error.clear() if self.pubmed_api is None: return if self.download_running: self.download_running = False self.run_search_button.setEnabled(True) self.retrieve_records_button.setText('Retrieve records') self.pubmed_api.stop_retrieving() return self.download_running = True self.run_search_button.setEnabled(False) self.output_corpus = None # Clear the old records. # Change the button label. self.retrieve_records_button.setText('Stop retrieving') # Text fields. text_includes_params = [ self.includes_authors, self.includes_title, self.includes_mesh, self.includes_abstract, self.includes_url, True, # Publication date field; included always. ] required_text_fields = [ field for field_name, field in zip( text_includes_params, PUBMED_TEXT_FIELDS) if field_name ] batch_size = min(Pubmed.MAX_BATCH_SIZE, self.num_records) + 1 with self.progressBar(self.num_records / batch_size) as progress: self.progress = progress self.output_corpus = self.pubmed_api._retrieve_records( self.num_records, required_text_fields) self.retrieve_records_button.setText('Retrieve records') self.download_running = False self.send(Output.CORPUS, self.output_corpus) self.update_retrieval_info() self.run_search_button.setEnabled(True) def api_progress_callback(self, start_at=None): if start_at is not None: self.progress.count = start_at else: self.progress.advance() def api_error_callback(self, error): self.Error.api_error(str(error)) if self.progress is not None: self.progress.finish() def update_search_info(self): max_records_count = min(self.pubmed_api.MAX_RECORDS, self.pubmed_api.search_record_count) self.search_info_label.setText( 'Number of retrievable records for ' 'this search query: {} '.format(max_records_count)) self.max_records_label.setText( 'records from {}.'.format(max_records_count)) self.max_records_label.setMaximumSize( self.max_records_label.sizeHint()) self.num_records_input.setMaximum(max_records_count) self.retrieve_records_button.setFocus() def update_retrieval_info(self): document_count = 0 if self.output_corpus is not None: document_count = len(self.output_corpus) self.retrieval_info_label.setText( 'Number of records retrieved: {} '.format(document_count)) self.retrieval_info_label.setMaximumSize( self.retrieval_info_label.sizeHint()) def select_email(self, n): if n < len(self.recent_emails): email = self.recent_emails[n] del self.recent_emails[n] self.recent_emails.insert(0, email) if len(self.recent_emails) > 0: self.set_email_list() def set_email_list(self): self.email_combo.clear() for email in self.recent_emails: self.email_combo.addItem(email) def select_keywords(self, n): if n < len(self.recent_keywords): keywords = self.recent_keywords[n] del self.recent_keywords[n] self.recent_keywords.insert(0, keywords) if len(self.recent_keywords) > 0: self.set_keyword_list() def set_keyword_list(self): self.keyword_combo.clear() if not self.recent_keywords: # Sample queries. self.recent_keywords.append('orchid') self.recent_keywords.append('hypertension') self.recent_keywords.append('blood pressure') self.recent_keywords.append('radiology') for keywords in self.recent_keywords: self.keyword_combo.addItem(keywords) def open_calendar(self, widget): cal_dlg = CalendarDialog(self, 'Date picker') if cal_dlg.exec_(): widget.setText(cal_dlg.picked_date) def send_report(self): if not self.pubmed_api: return max_records_count = min(self.pubmed_api.MAX_RECORDS, self.pubmed_api.search_record_count) if self.search_tabs.currentIndex() == 0: terms = self.keyword_combo.currentText() authors = self.author_input.text() self.report_items( (('Query', terms if terms else None), ('Authors', authors if authors else None), ('Date', 'from {} to {}'.format(self.pub_date_from, self.pub_date_to)), ('Number of records retrieved', '{}/{}'.format( len(self.output_corpus) if self.output_corpus else 0, max_records_count)))) else: query = self.advanced_query_input.toPlainText() self.report_items( (('Query', query if query else None), ('Number of records retrieved', '{}/{}'.format( len(self.output_corpus) if self.output_corpus else 0, max_records_count))))
class SchemeInfoEdit(QWidget): """Scheme info editor widget. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.scheme = None self.__setupUi() def __setupUi(self): layout = QFormLayout() layout.setRowWrapPolicy(QFormLayout.WrapAllRows) layout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow) self.name_edit = LineEdit(self) self.name_edit.setPlaceholderText(self.tr("untitled")) self.name_edit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) self.desc_edit = QTextEdit(self) self.desc_edit.setTabChangesFocus(True) layout.addRow(self.tr("Title"), self.name_edit) layout.addRow(self.tr("Description"), self.desc_edit) self.__schemeIsUntitled = True self.setLayout(layout) def setScheme(self, scheme): """Set the scheme to display/edit """ self.scheme = scheme if not scheme.title: self.name_edit.setText(self.tr("untitled")) self.name_edit.selectAll() self.__schemeIsUntitled = True else: self.name_edit.setText(scheme.title) self.__schemeIsUntitled = False self.desc_edit.setPlainText(scheme.description or "") def commit(self): """Commit the current contents of the editor widgets back to the scheme. """ if self.__schemeIsUntitled and \ self.name_edit.text() == self.tr("untitled"): # 'untitled' text was not changed name = "" else: name = self.name_edit.text().strip() description = self.desc_edit.toPlainText().strip() self.scheme.title = name self.scheme.description = description def paintEvent(self, event): return StyledWidget_paintEvent(self, event) def title(self): return self.name_edit.text().strip() def description(self): return self.desc_edit.toPlainText().strip()
class SchemeInfoEdit(QWidget): """Scheme info editor widget. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.scheme = None # type: Optional[Scheme] self.__schemeIsUntitled = True self.__setupUi() def __setupUi(self): layout = QFormLayout() layout.setRowWrapPolicy(QFormLayout.WrapAllRows) layout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow) self.name_edit = LineEdit(self) self.name_edit.setPlaceholderText(self.tr("untitled")) self.name_edit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) self.desc_edit = QTextEdit(self) self.desc_edit.setTabChangesFocus(True) layout.addRow(self.tr("Title"), self.name_edit) layout.addRow(self.tr("Description"), self.desc_edit) self.setLayout(layout) def setScheme(self, scheme): # type: (Scheme) -> None """Set the scheme to display/edit """ self.scheme = scheme if not scheme.title: self.name_edit.setText(self.tr("untitled")) self.name_edit.selectAll() self.__schemeIsUntitled = True else: self.name_edit.setText(scheme.title) self.__schemeIsUntitled = False self.desc_edit.setPlainText(scheme.description or "") def commit(self): # type: () -> None """ Commit the current contents of the editor widgets back to the scheme. """ if self.scheme is None: return if self.__schemeIsUntitled and \ self.name_edit.text() == self.tr("untitled"): # 'untitled' text was not changed name = "" else: name = self.name_edit.text().strip() description = self.desc_edit.toPlainText().strip() self.scheme.title = name self.scheme.description = description def paintEvent(self, event): return StyledWidget_paintEvent(self, event) def title(self): # type: () -> str return self.name_edit.text().strip() def description(self): # type: () -> str return self.desc_edit.toPlainText().strip()
class OWGrep(OWWidget, RecentPathsWComboMixin): name = "Grep" description = "Greps data from text, e.g. log files" icon = "icons/Grep.svg" priority = 1102 class Outputs: data = Output("Data", Table, default=True) settingsHandler = NameContextHandler() last_dir = Setting(os.path.expanduser("~")) pattern = ContextSetting("") case_sensitive = ContextSetting(True) regular_expression = ContextSetting(False) skip_lines = ContextSetting(0) block_length = ContextSetting(1) has_header_row = ContextSetting(False) auto_send = Setting(True) out_css = """ <style> div { /* Spaces in monospace are narrower than other characters! */ font-family: Consolas, Courier, monospace; font-size: 11pt; line-height: 120%; white-space: pre } .header, .add-header { font-weight: 900; } .add-header { color: gray; } </style> """ class Warning(OWWidget.Warning): no_lines = Msg("Pattern not found") no_header_row = Msg("Blocks do not appear to have headers.") class Error(OWWidget.Error): unreadable = Msg("Data is not readable.\n{}") file_not_found = Msg("File not found") block_too_short = Msg("Block with headers must more than 1 line.") def __init__(self): super().__init__() RecentPathsWComboMixin.__init__(self) self.current_file = None self.find_text = "" self.selected_lines = [] box = gui.widgetBox( self.controlArea, box="File", orientation=QGridLayout()) box.layout().addWidget(self.file_combo, 0, 0, 1, 2) self.file_combo.activated[int].connect(self.select_file) box.layout().addWidget( gui.button( None, self, 'Open', callback=self.browse_file, autoDefault=False, icon=self.style().standardIcon(QStyle.SP_DirOpenIcon)), 1, 0) box.layout().addWidget( gui.button( None, self, "Reload", callback=self.open_file, autoDefault=False, icon=self.style().standardIcon(QStyle.SP_BrowserReload)), 1, 1) box = gui.widgetBox(self.controlArea, box="Pattern") lineedit = gui.lineEdit(box, self, "pattern") lineedit.returnPressed.connect(self.grep_lines) gui.checkBox( box, self, "case_sensitive", label="Case sensitive", callback=self.grep_lines) gui.checkBox( box, self, "regular_expression", label="Regular expression", callback=self.grep_lines) box = gui.widgetBox(self.controlArea, box="Format") gui.spin( box, self, "skip_lines", 0, 10, label="Skipped lines: ", callback=self.grep_lines) gui.spin( box, self, "block_length", 1, 100000, label="Block length: ", callback=self.grep_lines) gui.checkBox( box, self, "has_header_row", label="Block(s) start with header row", tooltip="Only the header at the first block will be used.", callback=self.has_header_changed) gui.rubber(self.controlArea) gui.auto_commit(self.controlArea, self, "auto_send", "Send") box = gui.hBox(self.mainArea) gui.widgetLabel(box, "Input text") gui.rubber(box) find_line = gui.lineEdit( box, self, "find_text", label="Find: ", orientation=Qt.Horizontal, callback=self.find_changed, callbackOnType=True) find_line.returnPressed.connect(self.find_changed) self.in_view = QTextEdit(readOnly=True) self.mainArea.layout().addWidget(self.in_view) gui.widgetLabel(self.mainArea, "Used lines") self.out_view = QTextEdit(readOnly=True) self.mainArea.layout().addWidget(self.out_view) self.set_file_list() # Must not call select_file from within __init__ to avoid reentering # the event loop by a progress bar, when we have it if self.recent_paths: QTimer.singleShot(0, self.select_file) def sizeHint(self): # pragma: no cover size = super().sizeHint() size.setWidth(850) return size def select_file(self, n=0): """ Pull the file with the given index in the list to the top and open it. """ assert n < len(self.recent_paths) super().select_file(n) if self.recent_paths: self.closeContext() self.openContext(self.last_path()) self.open_file() self.set_file_list() def browse_file(self): """ Open the file dialog and open the chosen file. """ start_path = self.last_path() or os.path.expanduser("~") filename, _ = QFileDialog.getOpenFileName( None, "Open File", start_path, 'All Files (*.*)') if not filename: return self.closeContext() self.add_path(filename) self.openContext(filename) self.open_file() def open_file(self): """ Open the file returned by `last_path`. Sets `current_file`, shows its contents in `in_view` and calls `grep_lines`. This happens even if `current_file` is empty, in which case everything is cleared. """ self.Error.file_not_found.clear() self.current_file = self.last_path() text = "" if self.current_file: if not os.path.exists(self.current_file): self.Error.file_not_found() self.current_file = None else: with open(self.current_file) as f: text = f.read() self.in_view.setHtml(self.out_css + "<div>{}</div>".format(text)) self.grep_lines() def grep_lines(self): """ Grep the lines from `current_file` into `selected_lines`. If there is no current file, `selected_lines` is set to `[]`. Finally, it calls `set_out_view` and `commit`. Depends on `current_file` and all settings except `has_header_row`. """ def prepare_re(): pattern = self.pattern if not self.regular_expression: pattern = re.escape(pattern) flags = re.IGNORECASE if not self.case_sensitive else 0 return re.compile(pattern, flags) self.Warning.no_lines.clear() self.selected_lines = [] if self.pattern and self.current_file: pattern = prepare_re() with open(self.current_file) as f: file_lines = iter(f) try: line = next(file_lines) while True: if pattern.search(line): for _ in range(self.skip_lines): line = next(file_lines) for _ in range(self.block_length): self.selected_lines.append(line.strip()) line = next(file_lines) else: line = next(file_lines) except StopIteration: pass self.Warning.no_lines(shown=not self.selected_lines) self.set_out_view() self.commit() def set_out_view(self): """ Show the grepped lines in the output view. Directly uses `selected_lines`, `has_header_row` and `block_length`; depends on other settings through `selected_lines`. """ if self.has_header_row: text = "\n".join( "\n".join( ['<span class="{}">{}</span>'.format( "header" if i == 0 else "add-header", self.selected_lines[i])] + self.selected_lines[i + 1: i + self.block_length]) for i in range(0, len(self.selected_lines), self.block_length)) else: text = "\n".join(self.selected_lines) self.out_view.setHtml(self.out_css + "<div>{}</div>".format(text)) def has_header_changed(self): """ Callback handler for the header checkbox. Calls `set_out_view` to update the view (headers are shown in bold) and `commit`.""" self.set_out_view() self.commit() def commit(self): """Output the data table. All functionality is in a separate function for simpler control flow. """ self.Outputs.data.send(self._get_data()) def _get_data(self): """ Return a `Orange.data.Table` with the data from selected lines. If the data has a `header_row` but the reader does not recognize it, the function adds a header and rereads it, and shows a warning. If `selected_lines` are empty, the function returns `None`. """ self.Warning.no_header_row.clear() self.Error.block_too_short.clear() self.Error.unreadable.clear() if not self.selected_lines: return None if self.has_header_row: if self.block_length == 1: self.Error.block_too_short() return None out_data = self._construct_table(with_header_row=True) if out_data is None: return None if all(var.name == "Feature {}".format(i + 1) for i, var in enumerate(out_data.domain.variables)): self.Warning.no_header_row() return self._construct_table(with_header_row=False) else: return out_data else: return self._construct_table(with_header_row=False) # pylint: disable=broad-except def _construct_table(self, with_header_row): """ Write the `selected_lines` into a temporary file and read it. If `with_header_row` is `True`, the first line in every block is skipped, except for the first block. If `with_header_row` is `False`, a header row is added. """ assert self.selected_lines tempf = NamedTemporaryFile("w", suffix=".csv", delete=False) if with_header_row: data = self.selected_lines[0] + "\n" + \ "\n".join( "\n".join(self.selected_lines[i + 1:i + self.block_length]) for i in range(0, len(self.selected_lines), self.block_length)) else: n_cols = len(self.selected_lines[0].split()) data = ( " ".join("var{:03}".format(i + 1) for i in range(n_cols)) + "\n" + "\n".join(self.selected_lines)) tempf.write(re.sub(" +", " ", data)) tempf.close() try: return Table.from_file(tempf.name) except Exception as err: self.Error.unreadable(str(err).replace(" " + tempf.name, "")) finally: os.remove(tempf.name) def find_changed(self): """Callback for searchin within the file""" if not self.find_text: return in_view = self.in_view cursor = in_view.document().find( self.find_text, in_view.textCursor().position() + 1) if cursor.position() == -1: cursor = in_view.document().find(self.find_text, 0) if cursor.position() >= 0: in_view.setTextCursor(cursor) in_view.ensureCursorVisible() in_view.verticalScrollBar().setValue( in_view.verticalScrollBar().value() + in_view.cursorRect(cursor).y() - 20) def copy_to_clipboard(self): """Overloaded from OWWidget to copy the text from the file""" self.in_view.copy()