Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export2html #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/lineprofilergui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def setup_ui(self): # noqa: PLR0915
self.actionShowOutput.setIcon(ICONS["INFO"])
self.actionLoadLprof = QtGui.QAction(self)
self.actionLoadLprof.setIcon(ICONS["READFILE"])
self.actionExport = QtGui.QAction(self)
self.actionExport.setIcon(ICONS["EXPORT2HTML"])
self.actionQuit = QtGui.QAction(self)
self.actionQuit.setIcon(ICONS["ABORT"])
self.actionConfigure = QtGui.QAction(self)
Expand Down Expand Up @@ -93,6 +95,7 @@ def setup_ui(self): # noqa: PLR0915
self.menuProfiling.addAction(self.actionShowOutput)
self.menuProfiling.addSeparator()
self.menuProfiling.addAction(self.actionLoadLprof)
self.menuProfiling.addAction(self.actionExport)
self.menuProfiling.addSeparator()
self.menuProfiling.addAction(self.actionQuit)
self.menubar.addAction(self.menuProfiling.menuAction())
Expand Down Expand Up @@ -164,6 +167,7 @@ def connect_signals(self):
self.actionAbort.triggered.connect(self.kernprof_run.kill)
self.actionShowOutput.toggled.connect(self.dockOutputWidget.setVisible)
self.actionLoadLprof.triggered.connect(self.selectLprof)
self.actionExport.triggered.connect(self.exportLProfile)
self.actionQuit.triggered.connect(QtWidgets.QApplication.instance().quit)
self.actionLine_profiler_documentation.triggered.connect(
lambda: QtGui.QDesktopServices.openUrl(QtCore.QUrl(LINE_PROFILER_DOC_URL))
Expand Down Expand Up @@ -197,6 +201,8 @@ def retranslate_ui(self):
self.actionShowOutput.setShortcut(_("F7"))
self.actionLoadLprof.setText(_("&Load data..."))
self.actionLoadLprof.setShortcut(_("Ctrl+O"))
self.actionExport.setText(_("&Export as HTML..."))
self.actionExport.setShortcut(_("Ctrl+E"))
self.actionQuit.setText(_("&Quit"))
self.actionQuit.setShortcut(_("Ctrl+Q"))
self.actionConfigure.setText(_("&Configuration..."))
Expand Down Expand Up @@ -249,6 +255,18 @@ def selectLprof(self):
if filename:
self.load_lprof(filename)

def exportLProfile(self):
filename, _selfilter = QtWidgets.QFileDialog.getSaveFileName(
self,
_("Export line profiler data as HTML"),
"",
_("HTML") + " (*.html);; " + _("All files") + " (*.*)",
)
if filename:
html = self.resultsTreeWidget.generate_html()
with open(filename, "w") as f:
f.write(html)

@QtCore.Slot()
def configure(self):
UiConfigDialog(self, self.config).exec()
Expand Down
50 changes: 50 additions & 0 deletions src/lineprofilergui/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,56 @@ def updateColonsVisible(self):
col, not settings.value(f"column{col+1}Visible", True, bool)
)

def generate_html(self):
def get_text_color(item: QtWidgets.QTreeWidgetItem):
color = item.foreground(0).color()
return color.name()

def get_background_color(item: QtWidgets.QTreeWidgetItem):
color = item.background(0).color().name(QtGui.QColor.HexArgb)
return color[0] + color[3:] + color[1:3] # Swap alpha and color

html = "<html><head><style>"
html += "ul {list-style-type: none;}"
html += (
"body {background-color: #282A36; color: #F8F8F2; font-family: monospace;}"
)
html += "li {margin: 5px 0;}"
html += "</style></head><body>"

for i in range(self.topLevelItemCount()):
top_item = self.topLevelItem(i)
html += '<ul class="tree">'
html += "<li>"
html += "<details open>"
html += f"<summary>{top_item.text(0)}</summary>"
html += "<table>"
html += "<tr>"
for col in range(self.columnCount()):
if not self.isColumnHidden(col):
html += f"<th>{self.headerItem().text(col)}</th>"
html += "</tr>"
for j in range(top_item.childCount()):
child = top_item.child(j)
bg_color = get_background_color(child)
if bg_color == "#000000ff":
bg_color = "#282A36ff"
text_color = get_text_color(child)
if text_color == "#000000":
text_color = "#F8F8F2"
html += "<tr>"
for col in range(self.columnCount()):
if not self.isColumnHidden(col):
html += f'<td style="background-color:{bg_color}; color:{text_color}; white-space: pre;">{child.text(col)}</td>'
html += "</tr>"
html += "</table>"
html += "</details>"
html += "</li>"
html += "</ul>"

html += "</body></html>"
return html

@QtCore.Slot(QtWidgets.QTreeWidgetItem)
def item_activated(self, item):
# Skip parent lines
Expand Down
1 change: 1 addition & 0 deletions src/lineprofilergui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"SETTINGS": QtWidgets.QStyle.SP_FileDialogListView, # actionSettings
"BLANKFILE": QtWidgets.QStyle.SP_FileIcon, # statsButton
"READFILE": QtWidgets.QStyle.SP_FileDialogContentsView, # scriptButton, kernprofButton
"EXPORT2HTML": QtWidgets.QStyle.SP_DialogSaveButton, # scriptButton, kernprofButton
"DIRECTORY": QtWidgets.QStyle.SP_DirIcon, # wdirButton
"EXPAND": QtWidgets.QStyle.SP_ToolBarVerticalExtensionButton, # actionExpand_all
"COLLAPSE": QtWidgets.QStyle.SP_ToolBarHorizontalExtensionButton, # actionCollapse_all
Expand Down