Last active
August 29, 2015 14:20
-
-
Save jaidevd/b1afeb6d929291ca329d to your computer and use it in GitHub Desktop.
Basic PySide layout example with TabWidget and TreeWidget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PySide import QtGui, QtCore | |
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
import numpy as np | |
import datetime | |
IRIS_PATH = "/tmp/access" | |
treename = { | |
'General Statistics' : ['Summary'], | |
'Activity Statistics': ['Daily', 'By Hour Of Day', 'By Day Of Week', 'By Week', 'By Month'], | |
'Access Statistics' : ['Pages','Files','Images','Others','Search Engines', 'Result'], | |
'Referrers' : ['Reffering Sites', 'Reffering Urls', 'File Types'], | |
'Errors' : [ 'Types', '404 Errors', 'Other Errors', 'Status Codes'], | |
'Tracked Files' : ['Summary'] | |
} | |
class QCanvas(FigureCanvas): | |
def __init__(self, figure, parent): | |
super(QCanvas, self).__init__(figure) | |
self.setParent(parent) | |
def redrawAxes(self): | |
pass | |
class DataFrameModel(QtCore.QAbstractTableModel): | |
def __init__(self, df=None): | |
self.df = df | |
super(DataFrameModel, self).__init__() | |
def data(self, index, role): | |
if role == QtCore.Qt.DisplayRole: | |
return str(self.df.iget_value(index.row(), index.column())) | |
return None | |
def headerData(self, section, orientation, role): | |
if role == QtCore.Qt.DisplayRole: | |
if orientation == QtCore.Qt.Horizontal: | |
return self.df.columns[section] | |
return str(self.df.index[section]) | |
return | |
def rowCount(self, parent): | |
return self.df.shape[0] | |
def columnCount(self, parent): | |
return self.df.shape[1] | |
class MainWindow(QtGui.QMainWindow): | |
def __init__(self, **kwargs): | |
super(MainWindow, self).__init__(**kwargs) | |
centralWidget = QtGui.QSplitter(self) | |
layout = QtGui.QHBoxLayout() | |
data = pd.read_csv(IRIS_PATH, sep=' ', names=['timestamp','duration','ip','result','bytes','reqmthd','url','rfc','hierarchy', 'type']) | |
data['timestamp']= map(datetime.datetime.fromtimestamp, data['timestamp']) | |
data.index=pd.to_datetime(data.pop('timestamp')) | |
self.data=data | |
treeWidget = QtGui.QTreeWidget(self) | |
treeWidget.headerItem().setText(0, "Contents") | |
i=0 | |
for parent, children in treename.iteritems(): | |
rowItem = QtGui.QTreeWidgetItem(treeWidget, str(parent)) | |
rowItem.setText(0, parent) | |
for child in children: | |
childItem= QtGui.QTreeWidgetItem(rowItem) | |
childItem.setText(0, str(child)) | |
rowItem.addChild(childItem) | |
treeWidget.insertTopLevelItem(i, rowItem) | |
i+1 | |
treeWidget.itemSelectionChanged.connect(self.redrawPlot) | |
self.treeWidget = treeWidget | |
layout.addWidget(self.treeWidget) | |
tabbedArea = QtGui.QTabWidget(self) | |
self.fig, self.ax = self.get_example_figure() | |
self.mpl_tab = QCanvas(self.fig, self) | |
self.tableView = QtGui.QTableView(self) | |
self.dataFrameModel = DataFrameModel(self.data) | |
self.tableView.setModel(self.dataFrameModel) | |
tabbedArea.addTab(self.mpl_tab, "Visualization") | |
tabbedArea.addTab(self.tableView, "Data") | |
layout.addWidget(tabbedArea) | |
centralWidget.setLayout(layout) | |
self.setCentralWidget(centralWidget) | |
def get_example_figure(self): | |
x = np.linspace(-2*np.pi, 2*np.pi, 1000) | |
y = np.sin(x) | |
fig = plt.figure() | |
ax = fig.add_subplot(111) | |
ax.hold(False) | |
ax.plot(x, y) | |
self.x = x | |
return fig, ax | |
def redrawPlot(self): | |
selection = self.treeWidget.selectedItems() | |
kind = selection[0].text(0) | |
if kind.lower() == "result": | |
results = self.data[kind.lower()] | |
vcs = results.value_counts() | |
vcs.plot(kind='bar', ax=self.ax) | |
self.ax.figure.canvas.draw() | |
newDF = pd.DataFrame(results, columns=[kind.lower()]) | |
self.dataFrameModel = DataFrameModel(newDF) | |
self.tableView.setModel(self.dataFrameModel) | |
if __name__ == "__main__": | |
import sys | |
app = QtGui.QApplication(sys.argv) | |
window = MainWindow() | |
window.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment