|
3 | 3 | from typing import Any, Callable, Optional |
4 | 4 |
|
5 | 5 | from AnyQt.QtCore import Qt, QRectF, QSize, QPointF, QSizeF, QModelIndex, \ |
6 | | - QItemSelection, QItemSelectionModel, QT_VERSION |
| 6 | + QItemSelection, QItemSelectionModel, QT_VERSION, QByteArray, QBuffer, \ |
| 7 | + QIODevice |
7 | 8 | from AnyQt.QtGui import QPainter, QPen, QColor, QBrush, QMouseEvent |
8 | 9 | from AnyQt.QtWidgets import QSizePolicy, QGraphicsScene, QLabel, QSlider, \ |
9 | 10 | QListView, QStyledItemDelegate, QStyleOptionViewItem, QStyle |
10 | 11 |
|
| 12 | +from orangewidget.io import PngFormat |
11 | 13 | from Orange.base import RandomForestModel, TreeModel |
12 | 14 | from Orange.data import Table |
13 | 15 | from Orange.widgets import gui, settings |
|
23 | 25 | from Orange.widgets.widget import OWWidget |
24 | 26 |
|
25 | 27 |
|
| 28 | +REPORT_STYLE = """ |
| 29 | +<style> |
| 30 | +* { |
| 31 | + box-sizing: border-box; |
| 32 | +} |
| 33 | +
|
| 34 | +.forest_model_row { |
| 35 | + display: flex; |
| 36 | + flex-wrap: wrap; |
| 37 | + padding: 0 4px; |
| 38 | +} |
| 39 | +
|
| 40 | +.forest_model_col { |
| 41 | + flex: 10%; |
| 42 | + max-width: 10%; |
| 43 | + padding: 0 4px; |
| 44 | +} |
| 45 | +
|
| 46 | +.forest_model_col img { |
| 47 | + margin-top: 8px; |
| 48 | + vertical-align: middle; |
| 49 | +} |
| 50 | +
|
| 51 | +@media screen and (max-width: 2200px) { |
| 52 | + .forest_model_col { |
| 53 | + flex: 25%; |
| 54 | + max-width: 25%; |
| 55 | + } |
| 56 | +} |
| 57 | +
|
| 58 | +@media screen and (max-width: 1200px) { |
| 59 | + .forest_model_col { |
| 60 | + flex: 50%; |
| 61 | + max-width: 50%; |
| 62 | + } |
| 63 | +} |
| 64 | +
|
| 65 | +@media screen and (max-width: 600px) { |
| 66 | + .forest_model_col { |
| 67 | + flex: 100%; |
| 68 | + max-width: 100%; |
| 69 | + } |
| 70 | +} |
| 71 | +</style> |
| 72 | +""" |
| 73 | + |
| 74 | + |
26 | 75 | class PythagoreanForestModel(PyListModel): |
27 | 76 | def __init__(self, *args, **kwargs): |
28 | 77 | super().__init__(*args, **kwargs) |
@@ -377,7 +426,30 @@ def commit(self, selection: QItemSelection) -> None: |
377 | 426 |
|
378 | 427 | def send_report(self): |
379 | 428 | """Send report.""" |
380 | | - self.report_plot() |
| 429 | + model = self.forest_model |
| 430 | + max_rows = 30 |
| 431 | + |
| 432 | + def item_html(row): |
| 433 | + img_data = model.data(model.index(row)) |
| 434 | + byte_array = QByteArray() |
| 435 | + filename = QBuffer(byte_array) |
| 436 | + filename.open(QIODevice.WriteOnly) |
| 437 | + PngFormat.write(filename, img_data) |
| 438 | + img_encoded = byte_array.toBase64().data().decode("utf-8") |
| 439 | + return f'<img style="width:100%" ' \ |
| 440 | + f'src="data:image/png;base64,{img_encoded}"/>' |
| 441 | + |
| 442 | + html = ["<div class='forest_model_row'>"] |
| 443 | + for i in range(model.rowCount())[:max_rows]: |
| 444 | + html.append("<div class='forest_model_col'>") |
| 445 | + html.extend(item_html(i)) |
| 446 | + html.append("</div>") |
| 447 | + html.append("</div>") |
| 448 | + |
| 449 | + html = REPORT_STYLE + "".join(html) |
| 450 | + if model.rowCount() > max_rows: |
| 451 | + html += "<p>. . .</p>" |
| 452 | + self.report_raw(html) |
381 | 453 |
|
382 | 454 |
|
383 | 455 | class SklRandomForestAdapter: |
|
0 commit comments