-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathviperview.py
More file actions
165 lines (146 loc) · 5.91 KB
/
viperview.py
File metadata and controls
165 lines (146 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import sys
import os
import humanize
import pandas as pd
from pathlib import Path
from PyQt5.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QPushButton, QTableWidget,
QTableWidgetItem, QLabel, QFileDialog, QLineEdit, QHeaderView
)
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import Qt
import plotly.express as px
import pkg_resources
def get_package_sizes():
packages = []
for dist in pkg_resources.working_set:
try:
location = Path(dist.location) / dist.project_name.replace("-", "_")
if not location.exists():
location = Path(dist.location) / dist.project_name.lower()
if location.exists():
size = 0
for dirpath, _, filenames in os.walk(location):
for f in filenames:
fp = os.path.join(dirpath, f)
if os.path.isfile(fp):
size += os.path.getsize(fp)
packages.append({
"name": dist.project_name,
"version": dist.version,
"size_bytes": size,
"location": str(location)
})
except Exception as e:
print(f"[!] Error reading {dist.project_name}: {e}")
return packages
class ViperView(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("🐍 ViperView: Python Package Analyzer")
self.resize(1000, 800)
self.setStyleSheet("""
QWidget {
background-color: #1e1e2f;
color: #eee;
font-family: 'Segoe UI', sans-serif;
}
QPushButton {
background-color: #3a3a5c;
color: white;
padding: 8px;
border-radius: 4px;
}
QLineEdit {
padding: 6px;
background: #2b2b3d;
border: 1px solid #555;
border-radius: 4px;
color: #eee;
}
""")
self.data = pd.DataFrame(get_package_sizes())
self.data["size_mb"] = self.data["size_bytes"] / (1024 * 1024)
self.data["pretty_size"] = self.data["size_bytes"].apply(lambda x: humanize.naturalsize(x, binary=True))
self.init_ui()
def init_ui(self):
layout = QVBoxLayout()
self.stats_label = QLabel()
self.stats_label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.stats_label)
self.search_box = QLineEdit()
self.search_box.setPlaceholderText("🔍 Filter packages by name...")
self.search_box.textChanged.connect(self.filter_table)
layout.addWidget(self.search_box)
self.table = QTableWidget()
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(["Package", "Version", "Size", "Location"])
self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
layout.addWidget(self.table)
self.export_btn = QPushButton("📥 Export to CSV")
self.export_btn.clicked.connect(self.export_to_csv)
layout.addWidget(self.export_btn)
self.plot_view = QWebEngineView()
layout.addWidget(self.plot_view)
self.setLayout(layout)
self.populate_table(self.data)
self.update_stats()
self.show_plot()
def populate_table(self, df):
self.table.setRowCount(0)
for index, row in df.iterrows():
self.table.insertRow(index)
self.table.setItem(index, 0, QTableWidgetItem(row["name"]))
self.table.setItem(index, 1, QTableWidgetItem(row["version"]))
self.table.setItem(index, 2, QTableWidgetItem(row["pretty_size"]))
self.table.setItem(index, 3, QTableWidgetItem(row["location"]))
def update_stats(self):
total = humanize.naturalsize(self.data["size_bytes"].sum(), binary=True)
avg = humanize.naturalsize(self.data["size_bytes"].mean(), binary=True)
count = len(self.data)
self.stats_label.setText(f"📦 {count} packages | 💾 Total: {total} | 🧮 Average: {avg}")
def export_to_csv(self):
path, _ = QFileDialog.getSaveFileName(self, "Export CSV", "", "CSV Files (*.csv)")
if path:
self.data.to_csv(path, index=False)
def filter_table(self):
text = self.search_box.text().lower()
filtered = self.data[self.data["name"].str.lower().str.contains(text)]
self.populate_table(filtered)
self.update_plot(filtered)
def show_plot(self):
fig = px.bar(self.data.sort_values("size_mb", ascending=False).head(20),
x="size_mb", y="name", orientation="h",
hover_data=["version", "pretty_size"],
color="size_mb", color_continuous_scale="Plasma")
fig.update_layout(
height=500,
paper_bgcolor="#1e1e2f",
plot_bgcolor="#1e1e2f",
font_color="#eee",
xaxis_title="Size (MB)",
yaxis_title="Package",
title="Top 20 Largest Pip Packages"
)
html = fig.to_html(include_plotlyjs='cdn')
self.plot_view.setHtml(html)
def update_plot(self, df):
fig = px.bar(df.sort_values("size_mb", ascending=False).head(20),
x="size_mb", y="name", orientation="h",
hover_data=["version", "pretty_size"],
color="size_mb", color_continuous_scale="Plasma")
fig.update_layout(
height=500,
paper_bgcolor="#1e1e2f",
plot_bgcolor="#1e1e2f",
font_color="#eee",
xaxis_title="Size (MB)",
yaxis_title="Package"
)
html = fig.to_html(include_plotlyjs='cdn')
self.plot_view.setHtml(html)
if __name__ == "__main__":
app = QApplication(sys.argv)
viewer = ViperView()
viewer.show()
sys.exit(app.exec_())