Skip to content

Instantly share code, notes, and snippets.

@Tuhin-thinks
Created December 23, 2021 19:48
Show Gist options
  • Select an option

  • Save Tuhin-thinks/525141266e0b7b60ff7d4a021272aabb to your computer and use it in GitHub Desktop.

Select an option

Save Tuhin-thinks/525141266e0b7b60ff7d4a021272aabb to your computer and use it in GitHub Desktop.
To create filtering on pandas.DataFrame used in PyQt5 QtWiddgets.QTableView.
import pandas as pd
from PyQt5 import QtCore
class SymbolsProxy(QtCore.QSortFilterProxyModel):
def __init__(self, df_symbol_exchange: 'pd.DataFrame', parent=None):
super(SymbolsProxy, self).__init__(parent=parent)
self.check_df = df_symbol_exchange
self.exchange_filter = None
self.symbols_filter: 'QtCore.QRegularExpression' =\
QtCore.QRegularExpression(".*", QtCore.QRegularExpression.CaseInsensitiveOption)
def filterAcceptsRow(self, source_row: int, source_parent: QtCore.QModelIndex) -> bool:
index_0 = self.sourceModel().index(source_row, 0)
symbol = self.sourceModel().data(index_0)
if self.exchange_filter:
possible_matches = self.check_df[self.check_df["exchangeString"] == self.exchange_filter][
"symbolNames"].tolist()
for possible_match in possible_matches:
if self.symbols_filter.match(possible_match).hasMatch():
return True
return False
return self.symbols_filter.match(symbol).hasMatch()
def set_symbols_filter(self, symbols_filter: str):
reg_exp = QtCore.QRegularExpression(symbols_filter, QtCore.QRegularExpression.CaseInsensitiveOption)
self.symbols_filter = reg_exp
def set_exchange_filter(self, exchange_filter: str):
self.exchange_filter = exchange_filter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment