Skip to content

Instantly share code, notes, and snippets.

@inheritedburrp
Forked from jaidevd/tree_widget_example.py
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inheritedburrp/b80de16f1a22fba935e0 to your computer and use it in GitHub Desktop.
Save inheritedburrp/b80de16f1a22fba935e0 to your computer and use it in GitHub Desktop.
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 = "/home/anuj/Dropbox/work/work/Project/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'],
'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 == "Summary":
self.ax.plot(self.x, self.x)
elif kind == "Daily":
self.ax.plot(self.x, np.sin(self.x))
else:
self.ax.plot(self.x, np.sin(self.x))
self.ax.figure.canvas.draw()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
@inheritedburrp
Copy link
Author

whats wrong with this?

@jaidevd
Copy link

jaidevd commented May 2, 2015

What is the error?

@inheritedburrp
Copy link
Author

line 28, in init
self.treeWidget.insertTopLevelItem(str(parent), rowItem)
TypeError: 'PySide.QtGui.QTreeWidget.insertTopLevelItem' called with wrong argument types:
PySide.QtGui.QTreeWidget.insertTopLevelItem(str, PySide.QtGui.QTreeWidgetItem)
Supported signatures:
PySide.QtGui.QTreeWidget.insertTopLevelItem(int, PySide.QtGui.QTreeWidgetItem)

@jaidevd
Copy link

jaidevd commented May 2, 2015

In line 28 the first element should be an integer, index of that element in the tree widget.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment